Class: AttrJson::NestedAttributes::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/attr_json/nested_attributes/writer.rb

Overview

Implementation of assign_nested_attributes methods, called by the model method of that name that AttrJson::NestedAttributes adds.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, attr_name) ⇒ Writer

Returns a new instance of Writer.



12
13
14
15
# File 'lib/attr_json/nested_attributes/writer.rb', line 12

def initialize(model, attr_name)
  @model, @attr_name = model, attr_name
  @attr_def = model.class.attr_json_registry.fetch(attr_name)
end

Instance Attribute Details

#attr_defObject (readonly)

Returns the value of attribute attr_def.



10
11
12
# File 'lib/attr_json/nested_attributes/writer.rb', line 10

def attr_def
  @attr_def
end

#attr_nameObject (readonly)

Returns the value of attribute attr_name.



10
11
12
# File 'lib/attr_json/nested_attributes/writer.rb', line 10

def attr_name
  @attr_name
end

#modelObject (readonly)

Returns the value of attribute model.



10
11
12
# File 'lib/attr_json/nested_attributes/writer.rb', line 10

def model
  @model
end

Instance Method Details

#assign_nested_attributes(attributes) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/attr_json/nested_attributes/writer.rb', line 19

def assign_nested_attributes(attributes)
  if attr_def.array_of_primitive_type?
    assign_nested_attributes_for_primitive_array(attributes)
  elsif attr_def.array_type?
    assign_nested_attributes_for_model_array(attributes)
  else
    assign_nested_attributes_for_single_model(attributes)
  end
end