Sha256: 8e3157580c9dc297a75e22765b2f56127f4be80feeeefb5da3e187b47e1f088d

Contents?: true

Size: 808 Bytes

Versions: 1

Compression:

Stored size: 808 Bytes

Contents

class DeepTree
  
  Error            = Class.new(StandardError)
  InvalidTreeError = Class.new(Error)
  
  attr_accessor :tree
  
  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
  
  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
  
  def get_leaf(*args, &block)
    DeepTree.get_leaf( tree, *args, &block )
  end
  
  def method_missing(*args)
    self.tree(args)
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
deep_tree-0.0.3 lib/deep_tree/deep_tree.rb