Class: AcademicBenchmarks::Standards::StandardsForest

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_hash, save_guid_to_standard_hash: true, save_initial_data_hash: false, include_obsoletes: true) ⇒ StandardsForest

The guid to standard hash can optionally be saved to permit speedily adding standards to the tree (since the tree is unordered, this would otherwise be an expensive operation).

The initial data hash can also be optionally saved to permit testing and internal consistency checks



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/academic_benchmarks/standards/standards_forest.rb', line 13

def initialize(
  data_hash,
  save_guid_to_standard_hash: true,
  save_initial_data_hash: false,
  include_obsoletes: true
)
  @data_hash = data_hash.dup.freeze if save_initial_data_hash
  @guid_to_standard = {} # a hash of guids to standards
  @trees = []
  @orphans = []
  process_items(data_hash)

  @trees.delete_if(&:obsolete?) unless include_obsoletes

  # upgrade the hash data to a StandardsTree object
  @trees.map! do |item|
    StandardsTree.new(
      item,
      build_item_hash: save_guid_to_standard_hash,
      include_obsoletes: include_obsoletes
    )
  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

Instance Attribute Details

#data_hashObject (readonly)

Returns the value of attribute data_hash.



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

def data_hash
  @data_hash
end

#orphansObject (readonly)

Returns the value of attribute orphans.



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

def orphans
  @orphans
end

#treesObject (readonly)

Returns the value of attribute trees.



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

def trees
  @trees
end

Instance Method Details

#add_standard(standard) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/academic_benchmarks/standards/standards_forest.rb', line 50

def add_standard(standard)
  if standard.is_a?(Standard)
    raise StandardError.new(
      "adding standards is not currently implemented"
    )
  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}"
    )
  end
end

#consolidate_under_root(root) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/academic_benchmarks/standards/standards_forest.rb', line 41

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?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/academic_benchmarks/standards/standards_forest.rb', line 69

def empty?
  @trees.empty?
end

#has_orphans?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/academic_benchmarks/standards/standards_forest.rb', line 73

def has_orphans?
  @orphans.count > 0
end

#single_tree?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/academic_benchmarks/standards/standards_forest.rb', line 65

def single_tree?
  @trees.count == 1
end

#to_hObject



81
82
83
# File 'lib/academic_benchmarks/standards/standards_forest.rb', line 81

def to_h
  trees.map(&:to_h)
end

#to_jsonObject



85
86
87
# File 'lib/academic_benchmarks/standards/standards_forest.rb', line 85

def to_json
  trees.map(&:to_h).to_json
end

#to_sObject



77
78
79
# File 'lib/academic_benchmarks/standards/standards_forest.rb', line 77

def to_s
  trees.map(&:to_s)
end