Class: Forester::TreeFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/forester/tree_factory.rb

Class Method Summary collapse

Class Method Details

.from_hash_with_root_key(hash, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/forester/tree_factory.rb', line 14

def from_hash_with_root_key(hash, options = {})
  default_options = {
    max_level:    :last,
    children_key: :children,
    root_key:     :root
  }
  options = default_options.merge(options)

  dummy_root = TreeNode.new('<TEMP>')
  real_root  = fetch_indifferently(hash, options[:root_key])

  max_level = options[:max_level]
  max_level = -2 if max_level == :last

  tree = with_children(dummy_root, [real_root], options[:children_key], max_level + 1).first_child
  tree.detached_subtree_copy
end

.from_root_hash(hash, options = {}) ⇒ Object



10
11
12
# File 'lib/forester/tree_factory.rb', line 10

def from_root_hash(hash, options = {})
  from_hash_with_root_key({ root: hash }, options)
end

.from_yaml_file(file, options = {}) ⇒ Object



6
7
8
# File 'lib/forester/tree_factory.rb', line 6

def from_yaml_file(file, options = {})
  from_hash_with_root_key(YAML.load_file(file), options)
end