Class: AttrJson::NestedAttributes::Builder

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

Overview

Implementation of build_ methods, called by the build_ methods AttrJson::NestedAttributes adds.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, attr_name) ⇒ Builder

Returns a new instance of Builder.



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

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

Instance Attribute Details

#attr_defObject (readonly)

Returns the value of attribute attr_def.



8
9
10
# File 'lib/attr_json/nested_attributes/builder.rb', line 8

def attr_def
  @attr_def
end

#attr_nameObject (readonly)

Returns the value of attribute attr_name.



8
9
10
# File 'lib/attr_json/nested_attributes/builder.rb', line 8

def attr_name
  @attr_name
end

#modelObject (readonly)

Returns the value of attribute model.



8
9
10
# File 'lib/attr_json/nested_attributes/builder.rb', line 8

def model
  @model
end

Instance Method Details

#build(params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/attr_json/nested_attributes/builder.rb', line 15

def build(params = {})
  if attr_def.array_type?
    model.send("#{attr_name}=", (model.send(attr_name) || []) + [params])
    return model.send("#{attr_name}").last
  else
    model.send("#{attr_name}=", params)
    return model.send("#{attr_name}")
  end
end