Class: Hashematics::Graph

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hashematics/graph.rb

Overview

Graph serves as the main point of entry for this system. Basic use:

  1. Initialize a Graph by passing in an array of groups (tree structures)

  2. Feed in objects into the graph using the #add method

  3. Use the #groups, #records, and #objects methods to interact with the generated object graph.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(groups = []) ⇒ Graph

Returns a new instance of Graph.



23
24
25
26
27
28
# File 'lib/hashematics/graph.rb', line 23

def initialize(groups = [])
  @group_dictionary = Dictionary.new.add(groups, &:name)
  @record_set       = RecordSet.new

  freeze
end

Instance Attribute Details

#group_dictionaryObject (readonly)

Returns the value of attribute group_dictionary.



19
20
21
# File 'lib/hashematics/graph.rb', line 19

def group_dictionary
  @group_dictionary
end

#record_setObject (readonly)

Returns the value of attribute record_set.



19
20
21
# File 'lib/hashematics/graph.rb', line 19

def record_set
  @record_set
end

Instance Method Details

#add(enumerable) ⇒ Object



30
31
32
33
34
# File 'lib/hashematics/graph.rb', line 30

def add(enumerable)
  enumerable.each { |object| add_one(object) }

  self
end

#childrenObject



36
37
38
# File 'lib/hashematics/graph.rb', line 36

def children
  group_dictionary.map(&:name)
end

#data(name) ⇒ Object



44
45
46
# File 'lib/hashematics/graph.rb', line 44

def data(name)
  visit(name).map { |v| v.data(true) }
end

#visit(name) ⇒ Object



40
41
42
# File 'lib/hashematics/graph.rb', line 40

def visit(name)
  group(name)&.visit || []
end