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) ⇒ 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
# 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
)
  @data_hash = data_hash.dup.freeze if save_initial_data_hash
  @guid_to_standard = {} # a hash of guids to standards
  @trees = []
  process_items(data_hash)

  # upgrade the hash data to a StandardsTree object
  @trees.map! do |item|
    StandardsTree.new(item, build_item_hash: save_guid_to_standard_hash)
  end

  unless save_guid_to_standard_hash
    remove_instance_variable('@guid_to_standard')
  end
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

#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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/academic_benchmarks/standards/standards_forest.rb', line 33

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.to_s}"
    )
  end
end

#empty?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/academic_benchmarks/standards/standards_forest.rb', line 52

def empty?
  @trees.empty?
end

#single_tree?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/academic_benchmarks/standards/standards_forest.rb', line 48

def single_tree?
  @trees.count == 1
end