Class: Hashformer::Generate::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/hashformer/generate.rb

Overview

Internal representation of a path to a nested key/value. Do not instantiate directly; call Hashformer::Generate.path (or HF::G.path) instead.

Instance Method Summary collapse

Constructor Details

#initializePath

Returns a new instance of Path.



34
35
36
# File 'lib/hashformer/generate.rb', line 34

def initialize
  @pathlist = []
end

Instance Method Details

#[](path_item) ⇒ Object

Adds a path item to the end of the saved path.



52
53
54
55
# File 'lib/hashformer/generate.rb', line 52

def [](path_item)
  @pathlist << path_item
  self
end

#call(input_hash) ⇒ Object

Called to dereference the path on the given input_hash.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/hashformer/generate.rb', line 39

def call(input_hash)
  begin
    value = input_hash
    @pathlist.each do |path_item|
      value = value && value[path_item]
    end
    value
  rescue => e
    raise "Error dereferencing path #{self}: #{e}"
  end
end

#to_sObject



57
58
59
# File 'lib/hashformer/generate.rb', line 57

def to_s
  @pathlist.map{|p| "[#{p.inspect}]"}.join
end