Class: DeepTree

Inherits:
Object
  • Object
show all
Defined in:
lib/deep_tree/version.rb,
lib/deep_tree/deep_tree.rb

Constant Summary collapse

VERSION =
"0.0.3"
Error =
Class.new(StandardError)
InvalidTreeError =
Class.new(Error)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree) ⇒ DeepTree

Returns a new instance of DeepTree.



21
22
23
24
25
26
27
# File 'lib/deep_tree/deep_tree.rb', line 21

def initialize(tree)
  if tree.kind_of?(Hash)
    self.tree = tree
  else
    raise InvalidTreeError, "Expected a kind of Hash but got an instance of #{tree.class}"
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



33
34
35
# File 'lib/deep_tree/deep_tree.rb', line 33

def method_missing(*args)
  self.tree(args)
end

Instance Attribute Details

#treeObject

Returns the value of attribute tree.



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

def tree
  @tree
end

Class Method Details

.get_leaf(parent, *args, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/deep_tree/deep_tree.rb', line 8

def self.get_leaf(parent, *args, &block)
  args.each_with_index do |key, index|
    result = case
    when index == (args.length - 1)
      return parent[key].nil? && block ? yield : parent[key]
    when parent[key].is_a?(Hash)
      parent = parent[key]
    else
      return block ? yield : nil
    end
  end
end

Instance Method Details

#get_leaf(*args, &block) ⇒ Object



29
30
31
# File 'lib/deep_tree/deep_tree.rb', line 29

def get_leaf(*args, &block)
  DeepTree.get_leaf( tree, *args, &block )
end