Class: Hashematics::Group
- Inherits:
-
Object
- Object
- Hashematics::Group
- Defined in:
- lib/hashematics/group.rb
Overview
A group is a node in a tree structure connected to other groups through the children attribute. A group essentially represents an object within the object graph and its:
-
Category (index) for the parent to use as a lookup
-
Type that describes the object properties, field mapping, etc.
Instance Attribute Summary collapse
-
#category ⇒ Object
readonly
Returns the value of attribute category.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #add(record) ⇒ Object
- #children ⇒ Object
-
#initialize(category:, children:, name:, type:) ⇒ Group
constructor
A new instance of Group.
- #visit(parent_record = nil) ⇒ Object
- #visit_children(name, parent_record = nil) ⇒ Object
Constructor Details
#initialize(category:, children:, name:, type:) ⇒ Group
Returns a new instance of Group.
18 19 20 21 22 23 24 25 |
# File 'lib/hashematics/group.rb', line 18 def initialize(category:, children:, name:, type:) @category = category @child_dictionary = Dictionary.new.add(children, &:name) @name = name @type = type freeze end |
Instance Attribute Details
#category ⇒ Object (readonly)
Returns the value of attribute category.
16 17 18 |
# File 'lib/hashematics/group.rb', line 16 def category @category end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
16 17 18 |
# File 'lib/hashematics/group.rb', line 16 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
16 17 18 |
# File 'lib/hashematics/group.rb', line 16 def type @type end |
Instance Method Details
#add(record) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/hashematics/group.rb', line 27 def add(record) category.add(record) child_dictionary.each { |c| c.add(record) } self end |
#children ⇒ Object
35 36 37 |
# File 'lib/hashematics/group.rb', line 35 def children child_dictionary.map(&:name) end |
#visit(parent_record = nil) ⇒ Object
39 40 41 42 43 |
# File 'lib/hashematics/group.rb', line 39 def visit(parent_record = nil) category.records(parent_record).map do |record| Visitor.new(group: self, record: record) end end |
#visit_children(name, parent_record = nil) ⇒ Object
45 46 47 |
# File 'lib/hashematics/group.rb', line 45 def visit_children(name, parent_record = nil) child_group(name)&.visit(parent_record) || [] end |