Class: AcademicBenchmarks::Standards::StandardsTree

Inherits:
Object
  • Object
show all
Defined in:
lib/academic_benchmarks/standards/standards_tree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_standard, build_item_hash: true) ⇒ StandardsTree

The item hash can optionally be built to permit the speedy addition of standards to the tree. since the tree is unordered, adding to it can be expensive without this



10
11
12
13
14
15
16
# File 'lib/academic_benchmarks/standards/standards_tree.rb', line 10

def initialize(root_standard, build_item_hash: true)
  @root_standard = root_standard
  if build_item_hash
    @item_hash = {}
    go_ahead_and_build_item_hash
  end
end

Instance Attribute Details

#root_standardObject (readonly)

Returns the value of attribute root_standard.



4
5
6
# File 'lib/academic_benchmarks/standards/standards_tree.rb', line 4

def root_standard
  @root_standard
end

Instance Method Details

#add_standard(standard) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/academic_benchmarks/standards/standards_tree.rb', line 18

def add_standard(standard)
  if standard.is_a?(Standard)
    parent = @item_hash ? @item_hash[standard.parent_guid] : find_parent(standard)
    unless parent
      raise StandardError.new(
        "Parent of standard not found in tree. Parent guid is " \
        "'#{standard.parent_guid}' and child guid is '#{standard.guid}'"
      )
    end
    parent.add_child(standard)
    standard.parent = parent
  elsif standard.is_a?(Hash)
    add_standard(Standard.new(standard))
  else
    raise ArgumentError.new(
      "standard must be an 'AcademicBenchmarks::Standards::Standard' " \
      "or a 'Hash' but was a #{standard.class.to_s}"
    )
  end
end