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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/forester/tree_factory.rb', line 25

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



15
16
17
18
19
20
21
22
23
# File 'lib/forester/tree_factory.rb', line 15

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

  from_hash_with_root_key({ root: hash }, options)
end

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



6
7
8
9
10
11
12
13
# File 'lib/forester/tree_factory.rb', line 6

def from_yaml_file(file, options = {})
  default_options = {
    max_level: :last
  }
  options = default_options.merge(options)

  from_hash_with_root_key(YAML.load_file(file))
end