Class: FormObj::Mappable::ModelAttribute
- Inherits:
-
Object
- Object
- FormObj::Mappable::ModelAttribute
- Defined in:
- lib/form_obj/mappable/model_attribute.rb,
lib/form_obj/mappable/model_attribute/item.rb
Defined Under Namespace
Classes: Item
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #create_model ⇒ Object
- #hash_item=(value) ⇒ Object
-
#initialize(names:, classes:, default_name:, array:, hash:, subform:, model:) ⇒ ModelAttribute
constructor
A new instance of ModelAttribute.
- #last_name ⇒ Object
- #read_errors_from_model(model) ⇒ Object
- #read_errors_from_models(models) ⇒ Object
- #read_from_model(model, create_nested_model_if_nil: false) ⇒ Object
- #read_from_model? ⇒ Boolean
- #read_from_models(models, create_nested_model_if_nil: false) ⇒ Object
- #to_model_hash(value) ⇒ Object
- #validate_primary_key! ⇒ Object
- #write_to_model(model, value) ⇒ Object
- #write_to_model? ⇒ Boolean
- #write_to_models(models, value) ⇒ Object
Constructor Details
#initialize(names:, classes:, default_name:, array:, hash:, subform:, model:) ⇒ ModelAttribute
Returns a new instance of ModelAttribute.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/form_obj/mappable/model_attribute.rb', line 9 def initialize(names:, classes:, default_name:, array:, hash:, subform:, model:) @read_from_model = @write_to_model = !(names === false) @model = model @array = array names = (names || default_name).to_s.split('.') unless names.is_a? ::Enumerable classes = classes.nil? ? [] : [classes] unless classes.is_a? ::Enumerable if classes.size > 0 if (subform && (names.size != classes.size)) || (!subform && (names.size != classes.size + 1)) raise "Since the :model_attribute size is #{names.size} the :model_class size should be #{names.size - subform ? 0 : 1} in terms of nested items but it was #{classes.size}" unless names.size == classes.size end end @items = names.zip(classes, [hash], names[0..-2].map{nil} + [array]).map { |item| Item.new(name: item[0], klass: item[1], hash: item[2], array: item[3]) } @items.inject do |prev, item| prev.hash = true if item.hash_item item end end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
7 8 9 |
# File 'lib/form_obj/mappable/model_attribute.rb', line 7 def model @model end |
Instance Method Details
#create_model ⇒ Object
40 41 42 43 |
# File 'lib/form_obj/mappable/model_attribute.rb', line 40 def create_model raise 'Creation available only for array attributes' unless @array @items.last.create_model end |
#hash_item=(value) ⇒ Object
36 37 38 |
# File 'lib/form_obj/mappable/model_attribute.rb', line 36 def hash_item=(value) @items[0].hash_item = value end |
#last_name ⇒ Object
32 33 34 |
# File 'lib/form_obj/mappable/model_attribute.rb', line 32 def last_name @items.last.name end |
#read_errors_from_model(model) ⇒ Object
82 83 84 85 86 |
# File 'lib/form_obj/mappable/model_attribute.rb', line 82 def read_errors_from_model(model) @items.last.try(:read_errors_from_model, @items[0..-2].reduce(model) { |last_model, item| item.read_from_model(last_model, create_nested_model_if_nil: false) } ) end |
#read_errors_from_models(models) ⇒ Object
88 89 90 |
# File 'lib/form_obj/mappable/model_attribute.rb', line 88 def read_errors_from_models(models) read_errors_from_model(models[@model]) end |
#read_from_model(model, create_nested_model_if_nil: false) ⇒ Object
49 50 51 |
# File 'lib/form_obj/mappable/model_attribute.rb', line 49 def read_from_model(model, create_nested_model_if_nil: false) @items.reduce(model) { |last_model, item| item.read_from_model(last_model, create_nested_model_if_nil: create_nested_model_if_nil) } end |
#read_from_model? ⇒ Boolean
45 46 47 |
# File 'lib/form_obj/mappable/model_attribute.rb', line 45 def read_from_model? @read_from_model end |
#read_from_models(models, create_nested_model_if_nil: false) ⇒ Object
53 54 55 |
# File 'lib/form_obj/mappable/model_attribute.rb', line 53 def read_from_models(models, create_nested_model_if_nil: false) read_from_model(models[@model], create_nested_model_if_nil: create_nested_model_if_nil) end |
#to_model_hash(value) ⇒ Object
78 79 80 |
# File 'lib/form_obj/mappable/model_attribute.rb', line 78 def to_model_hash(value) @items.reverse.reduce(value) { |value, item| item.to_hash(value) } end |
#validate_primary_key! ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/form_obj/mappable/model_attribute.rb', line 70 def validate_primary_key! if @items.size > 1 raise PrimaryKeyMappingError.new('Primary key should not be mapped to nested model') elsif @items.size == 0 raise PrimaryKeyMappingError.new('Primary key should not be mapped to non-mapped attribute') end end |
#write_to_model(model, value) ⇒ Object
61 62 63 64 |
# File 'lib/form_obj/mappable/model_attribute.rb', line 61 def write_to_model(model, value) model = @items[0..-2].reduce(model) { |last_model, item| item.read_from_model(last_model, create_nested_model_if_nil: true) } if @items.size > 1 @items.last.write_to_model(model, value) end |
#write_to_model? ⇒ Boolean
57 58 59 |
# File 'lib/form_obj/mappable/model_attribute.rb', line 57 def write_to_model? @write_to_model end |
#write_to_models(models, value) ⇒ Object
66 67 68 |
# File 'lib/form_obj/mappable/model_attribute.rb', line 66 def write_to_models(models, value) write_to_model(models[@model], value) end |