Class: Strict::Declaration
- Inherits:
-
Object
- Object
- Strict::Declaration
- Defined in:
- lib/strict/declaration.rb
Constant Summary collapse
- NOT_PROVIDED =
::Object.new.freeze
Instance Attribute Summary collapse
-
#coercer ⇒ Object
readonly
Returns the value of attribute coercer.
-
#default_generator ⇒ Object
readonly
Returns the value of attribute default_generator.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#validator ⇒ Object
readonly
Returns the value of attribute validator.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(name:, validator:, default_generator:, coercer:) ⇒ Declaration
constructor
A new instance of Declaration.
- #optional? ⇒ Boolean
- #valid?(value, configuration = Strict.configuration) ⇒ Boolean
- #violations(value, configuration = Strict.configuration) ⇒ Object
Constructor Details
#initialize(name:, validator:, default_generator:, coercer:) ⇒ Declaration
Returns a new instance of Declaration.
84 85 86 87 88 89 90 91 |
# File 'lib/strict/declaration.rb', line 84 def initialize(name:, validator:, default_generator:, coercer:) @name = name.to_sym @validator = validator @default_generator = default_generator @coercer = coercer @optional = !default_generator.equal?(NOT_PROVIDED) @detailed_validator = DetailedValidator === validator end |
Instance Attribute Details
#coercer ⇒ Object (readonly)
Returns the value of attribute coercer.
82 83 84 |
# File 'lib/strict/declaration.rb', line 82 def coercer @coercer end |
#default_generator ⇒ Object (readonly)
Returns the value of attribute default_generator.
82 83 84 |
# File 'lib/strict/declaration.rb', line 82 def default_generator @default_generator end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
82 83 84 |
# File 'lib/strict/declaration.rb', line 82 def name @name end |
#validator ⇒ Object (readonly)
Returns the value of attribute validator.
82 83 84 |
# File 'lib/strict/declaration.rb', line 82 def validator @validator end |
Class Method Details
.make(name, validator = Validators::Anything.instance, coerce: validator.respond_to?(:coercer) ? validator.coercer : false, **defaults) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/strict/declaration.rb', line 10 def make( name, validator = Validators::Anything.instance, coerce: validator.respond_to?(:coercer) ? validator.coercer : false, **defaults ) validate_name!(name) validate_defaults!(**defaults) validate_coercer!(coerce) new( name: name.to_sym, validator: validator, default_generator: make_default_generator(**defaults), coercer: coerce ) end |
Instance Method Details
#optional? ⇒ Boolean
93 94 95 |
# File 'lib/strict/declaration.rb', line 93 def optional? @optional end |
#valid?(value, configuration = Strict.configuration) ⇒ Boolean
97 98 99 |
# File 'lib/strict/declaration.rb', line 97 def valid?(value, configuration = Strict.configuration) violations(value, configuration).empty? end |
#violations(value, configuration = Strict.configuration) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/strict/declaration.rb', line 101 def violations(value, configuration = Strict.configuration) return Validation::NONE unless configuration.validate? return validator.violations(value) if @detailed_validator return Validation::NONE if validator === value Validation.invalid(validator, value) end |