Class: Forester::TreeFactory
- Inherits:
-
Object
- Object
- Forester::TreeFactory
- Defined in:
- lib/forester/tree_factory.rb
Class Method Summary collapse
- .from_hash_with_root_key(hash, options = {}) ⇒ Object
- .from_root_hash(hash, options = {}) ⇒ Object
- .from_yaml_file(file, options = {}) ⇒ Object
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, = {}) = { max_level: :last, children_key: :children, root_key: :root } = .merge() dummy_root = TreeNode.new('<TEMP>') real_root = fetch_indifferently(hash, [:root_key]) max_level = [:max_level] max_level = -2 if max_level == :last tree = with_children(dummy_root, [real_root], [: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, = {}) = { max_level: :last, children_key: :children } = .merge() from_hash_with_root_key({ root: hash }, ) 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, = {}) = { max_level: :last } = .merge() from_hash_with_root_key(YAML.load_file(file)) end |