Class: Hashformer::Generate::Path
- Inherits:
-
Object
- Object
- Hashformer::Generate::Path
- 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
-
#[](path_item) ⇒ Object
Adds a path item to the end of the saved path.
-
#call(input_hash) ⇒ Object
Called to dereference the path on the given
input_hash
. -
#initialize ⇒ Path
constructor
A new instance of Path.
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ Path
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_s ⇒ Object
57 58 59 |
# File 'lib/hashformer/generate.rb', line 57 def to_s @pathlist.map{|p| "[#{p.inspect}]"}.join end |