Class: Forester::TreeFactory

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

Class Method Summary collapse

Class Method Details

.from_hash(hash, children_key, uid = SecureRandom.uuid) ⇒ Object



22
23
24
25
26
# File 'lib/forester/tree_factory.rb', line 22

def from_hash(hash, children_key, uid = SecureRandom.uuid)
  name = uid
  content = NodeContent::Factory.from_hash(hash, children_key)
  TreeNode.new(name, content)
end

.from_hash_with_root_key(hash, children_key = :children, root_key = :root) ⇒ Object



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

def from_hash_with_root_key(hash, children_key = :children, root_key = :root)
  dummy_root = TreeNode.new('<TEMP>')
  real_root  = fetch_indifferently(hash, root_key)

  tree = with_children(dummy_root, [real_root], children_key).first_child
  tree.detached_subtree_copy
end

.from_root_hash(hash, children_key = :children) ⇒ Object



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

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

.from_yaml_file(file) ⇒ Object



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

def from_yaml_file(file)
  from_hash_with_root_key(YAML.load_file(file))
end