Class: TypedModel::AttributeDefinition
- Inherits:
-
Object
- Object
- TypedModel::AttributeDefinition
- Defined in:
- lib/typed_model/attribute_definition.rb
Instance Attribute Summary collapse
-
#mapping_key ⇒ Object
readonly
Returns the value of attribute mapping_key.
-
#model_validations ⇒ Object
readonly
Returns the value of attribute model_validations.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#spec ⇒ Object
readonly
Returns the value of attribute spec.
Instance Method Summary collapse
- #attr_type ⇒ Object
- #attr_value(model) ⇒ Object
-
#initialize(name:, type: nil, seq_of: nil, map_of: nil, validations: [], mapping_key: nil) ⇒ AttributeDefinition
constructor
A new instance of AttributeDefinition.
- #to_data(model) ⇒ Object
- #typecast_value(v) ⇒ Object
- #validate(model) ⇒ Object
Constructor Details
#initialize(name:, type: nil, seq_of: nil, map_of: nil, validations: [], mapping_key: nil) ⇒ AttributeDefinition
Returns a new instance of AttributeDefinition.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/typed_model/attribute_definition.rb', line 9 def initialize(name:, type: nil, seq_of: nil, map_of: nil, validations: [], mapping_key: nil) @name = name @mapping_key = mapping_key @model_validations = [] spec_validations = [] validations.each do |v| if Validators.recognized?(v) || v.respond_to?(:validate) spec_validations << v else model_validations << v end end @spec = TypeDef.build(type: type, seq_of: seq_of, map_of: map_of, validations: spec_validations) end |
Instance Attribute Details
#mapping_key ⇒ Object (readonly)
Returns the value of attribute mapping_key.
7 8 9 |
# File 'lib/typed_model/attribute_definition.rb', line 7 def mapping_key @mapping_key end |
#model_validations ⇒ Object (readonly)
Returns the value of attribute model_validations.
7 8 9 |
# File 'lib/typed_model/attribute_definition.rb', line 7 def model_validations @model_validations end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/typed_model/attribute_definition.rb', line 7 def name @name end |
#spec ⇒ Object (readonly)
Returns the value of attribute spec.
7 8 9 |
# File 'lib/typed_model/attribute_definition.rb', line 7 def spec @spec end |
Instance Method Details
#attr_type ⇒ Object
24 25 26 |
# File 'lib/typed_model/attribute_definition.rb', line 24 def attr_type spec.value_type end |
#attr_value(model) ⇒ Object
51 52 53 |
# File 'lib/typed_model/attribute_definition.rb', line 51 def attr_value(model) model.send(name) end |
#to_data(model) ⇒ Object
42 43 44 45 |
# File 'lib/typed_model/attribute_definition.rb', line 42 def to_data(model) value = attr_value(model) spec.to_data(value) unless value.nil? end |
#typecast_value(v) ⇒ Object
47 48 49 |
# File 'lib/typed_model/attribute_definition.rb', line 47 def typecast_value(v) spec.typecast_value(v) end |
#validate(model) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/typed_model/attribute_definition.rb', line 32 def validate(model) value = attr_value(model) spec.validate(value, model.errors, name) model_validations.each do |v| method_name = :"assert_#{v}" raise "Unrecognized validation '#{v}'" unless model.respond_to?(method_name) model.send(method_name, name) end end |