Class: MongoidEmbedFinder::NestedBuilder
- Inherits:
-
Object
- Object
- MongoidEmbedFinder::NestedBuilder
- Defined in:
- lib/mongoid_embed_finder/nested_builder.rb
Instance Method Summary collapse
- #build_child ⇒ Object
- #build_parent ⇒ Object
- #children_attributes ⇒ Object
-
#initialize(nested_attrs, relations) ⇒ NestedBuilder
constructor
A new instance of NestedBuilder.
- #parent_attributes ⇒ Object
Constructor Details
#initialize(nested_attrs, relations) ⇒ NestedBuilder
Returns a new instance of NestedBuilder.
3 4 5 6 |
# File 'lib/mongoid_embed_finder/nested_builder.rb', line 3 def initialize(nested_attrs, relations) @nested_attrs = nested_attrs @relations = relations end |
Instance Method Details
#build_child ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/mongoid_embed_finder/nested_builder.rb', line 8 def build_child return nil if children_attributes.empty? @relations.child_class.new(children_attributes.first).tap do |child| child.public_send(@relations.parent.setter, build_parent) end end |
#build_parent ⇒ Object
16 17 18 |
# File 'lib/mongoid_embed_finder/nested_builder.rb', line 16 def build_parent @relations.parent_class.new(parent_attributes) end |
#children_attributes ⇒ Object
20 21 22 |
# File 'lib/mongoid_embed_finder/nested_builder.rb', line 20 def children_attributes @children_attributes ||= @nested_attrs.fetch(@relations.children.key) end |
#parent_attributes ⇒ Object
24 25 26 |
# File 'lib/mongoid_embed_finder/nested_builder.rb', line 24 def parent_attributes @parent_attributes ||= @nested_attrs.except(@relations.children.key) end |