Class: AcademicBenchmarks::Standards::StandardsTree
- Inherits:
-
Object
- Object
- AcademicBenchmarks::Standards::StandardsTree
- Defined in:
- lib/academic_benchmarks/standards/standards_tree.rb
Instance Attribute Summary collapse
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #add_standard(standard) ⇒ Object
-
#initialize(root, build_item_hash: true) ⇒ StandardsTree
constructor
The item hash can optionally be built to permit the speedy addition of standards to the tree.
Constructor Details
#initialize(root, 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
13 14 15 16 17 18 19 |
# File 'lib/academic_benchmarks/standards/standards_tree.rb', line 13 def initialize(root, build_item_hash: true) @root = root if build_item_hash @item_hash = {} go_ahead_and_build_item_hash end end |
Instance Attribute Details
#root ⇒ Object (readonly)
Returns the value of attribute root.
6 7 8 |
# File 'lib/academic_benchmarks/standards/standards_tree.rb', line 6 def root @root end |
Instance Method Details
#add_standard(standard) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/academic_benchmarks/standards/standards_tree.rb', line 21 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 |