class AcademicBenchmarks::Standards::StandardsForest
Attributes
orphans[R]
trees[R]
Public Class Methods
new(data_hash)
click to toggle source
# File lib/academic_benchmarks/standards/standards_forest.rb, line 6 def initialize(data_hash) @guid_to_standard = {} # a hash of guids to standards @trees = [] @orphans = [] process_items(data_hash) # upgrade the hash data to a StandardsTree object @trees.map! do |item| StandardsTree.new(item) end # We will still have the guid-to-standards saved at the Tree level, # so we can safely remove this variable and let the GC free the memory remove_instance_variable('@guid_to_standard') end
Public Instance Methods
consolidate_under_root(root)
click to toggle source
# File lib/academic_benchmarks/standards/standards_forest.rb, line 22 def consolidate_under_root(root) trees.each do |tree| tree.root.parent = root tree.root.parent_guid = root.guid root.children.push(tree.root) end StandardsTree.new(root).tap{ |st| st.add_orphans(@orphans) } end
empty?()
click to toggle source
# File lib/academic_benchmarks/standards/standards_forest.rb, line 35 def empty? @trees.empty? end
has_orphans?()
click to toggle source
# File lib/academic_benchmarks/standards/standards_forest.rb, line 39 def has_orphans? @orphans.count > 0 end
single_tree?()
click to toggle source
# File lib/academic_benchmarks/standards/standards_forest.rb, line 31 def single_tree? @trees.count == 1 end
to_h()
click to toggle source
# File lib/academic_benchmarks/standards/standards_forest.rb, line 47 def to_h trees.map(&:to_h) end
to_json()
click to toggle source
# File lib/academic_benchmarks/standards/standards_forest.rb, line 51 def to_json trees.map(&:to_h).to_json end
to_s()
click to toggle source
# File lib/academic_benchmarks/standards/standards_forest.rb, line 43 def to_s trees.map(&:to_s) end
Private Instance Methods
build_guid_to_standard_hash(data_hash)
click to toggle source
# File lib/academic_benchmarks/standards/standards_forest.rb, line 67 def build_guid_to_standard_hash(data_hash) data_hash.each do |item| item = to_standard(item) @guid_to_standard[item.guid] = item end end
link_parent_and_children()
click to toggle source
# File lib/academic_benchmarks/standards/standards_forest.rb, line 74 def link_parent_and_children @guid_to_standard.values.each do |child| if child.parent_guid parent_in_hash?(child.parent_guid) ? set_parent_and_child(child) : @orphans.push(child) else @trees.push(child) end end end
parent_in_hash?(guid)
click to toggle source
# File lib/academic_benchmarks/standards/standards_forest.rb, line 90 def parent_in_hash?(guid) @guid_to_standard.key?(guid) end
process_items(data_hash)
click to toggle source
# File lib/academic_benchmarks/standards/standards_forest.rb, line 62 def process_items(data_hash) build_guid_to_standard_hash(data_hash) link_parent_and_children end
set_parent_and_child(child)
click to toggle source
# File lib/academic_benchmarks/standards/standards_forest.rb, line 84 def set_parent_and_child(child) parent = @guid_to_standard[child.parent_guid] parent.add_child(child) child.parent = parent end
to_standard(item)
click to toggle source
# File lib/academic_benchmarks/standards/standards_forest.rb, line 57 def to_standard(item) return item if item.is_a?(Standard) Standard.new(item) end