Class: Hashematics::Group

Inherits:
Object
  • Object
show all
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:

  1. Category (index) for the parent to use as a lookup

  2. Type that describes the object properties, field mapping, etc.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#categoryObject (readonly)

Returns the value of attribute category.



16
17
18
# File 'lib/hashematics/group.rb', line 16

def category
  @category
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/hashematics/group.rb', line 16

def name
  @name
end

#typeObject (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

#childrenObject



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