Class: Alchemrest::HashPath
- Inherits:
-
Object
- Object
- Alchemrest::HashPath
- Defined in:
- lib/alchemrest/hash_path.rb
Overview
A utility for executing transformations against hashes scoped to specific parts of the hash defined by the ‘segments` of the path. The `Alchemrest::HashPath` class has one method `#walk` which will “walk” the path defined by the segments, yielding to a block provided by the calling code at each node it stops at. In that block, the calling code can transform the hash as it likes
Class Method Summary collapse
-
.build_collection(definition) ⇒ Object
A helper method to quickly build a collection of hash paths from a rails style “StrongParams” hash arg.
Instance Method Summary collapse
-
#walk(input, &block) ⇒ Object
“Walks” the path defined by the segments of the HashPath, including individual items in nested collections.
Class Method Details
.build_collection(definition) ⇒ Object
A helper method to quickly build a collection of hash paths from a rails style “StrongParams” hash arg.
28 29 30 31 32 33 34 |
# File 'lib/alchemrest/hash_path.rb', line 28 def self.build_collection(definition) paths = [] visit_leaves(definition) do |value, path| paths << (path + [value]) end paths.map { |path| new(path) } end |
Instance Method Details
#walk(input, &block) ⇒ Object
“Walks” the path defined by the segments of the HashPath, including individual items in nested collections. For each items it walks, it yields to the provided block, passing the current full path to that item, it’s value and the remaining segments of the path it has to walk. If it any point the input does not have a value for one of the segments, this method will stop traversing that portion of the hash. Note actions taken within the block are mutative, and will modify the original hash
41 42 43 |
# File 'lib/alchemrest/hash_path.rb', line 41 def walk(input, &block) traverse(input, segments, [], &block) end |