Module: Hashformer::Generate
- Defined in:
- lib/hashformer/generate.rb
Overview
This module contains simple methods for generating complex transformations for Hashformer.
Defined Under Namespace
Classes: Chain, Constant, Map, Path
Class Method Summary collapse
-
.chain ⇒ Object
Generates a method call chain to apply to the input hash given to a transformation.
-
.const(value) ⇒ Object
Generates a transformation that always returns a constant value.
-
.map(*keys_or_paths, &block) ⇒ Object
Generates a transformation that passes one or more values from the input Hash (denoted by key names or paths (see Hashformer::Generate.path) to the block. If the block is not given, then the values are placed in an array in the order in which their keys were given as parameters.
-
.path ⇒ Object
Generates a path reference (via Path#[]) that grabs a nested value for use directly or in other transformations.
Class Method Details
.chain ⇒ Object
Generates a method call chain to apply to the input hash given to a transformation. This allows path references (as with HF::G.path) and method calls to be stored and applied later.
See Hashformer::Generate::Chain.enable_debugging if you run into issues.
Example:
data = { in1: { in2: [1, 2, 3, [4, 5, 6, 7]] } }
xform = { out1: HF::G.chain[:in1][:in2][3].reduce(&:+) }
Hashformer.transform(data, xform) # Returns { out1: 22 }
285 286 287 |
# File 'lib/hashformer/generate.rb', line 285 def self.chain Chain.new.receiver end |
.const(value) ⇒ Object
236 237 238 |
# File 'lib/hashformer/generate.rb', line 236 def self.const(value) Constant.new(value) end |
.map(*keys_or_paths, &block) ⇒ Object
Generates a transformation that passes one or more values from the input Hash (denoted by key names or paths (see Hashformer::Generate.path) to the block. If the block is not given, then the values are placed in an array in the order in which their keys were given as parameters.
You can also pass a Hashformer transformation Hash as one or more keys.
Examples:
HF::G.map(:first, :last) do |f, l| "#{f} #{l}".strip end
HF::G.map(:a1, :a2) # Turns {a1: 1, a2: 2} into [1, 2]
HF::G.map(HF::G.path[:address][:line1], HF::G.path[:address][:line2])
251 252 253 |
# File 'lib/hashformer/generate.rb', line 251 def self.map(*keys_or_paths, &block) Map.new(*keys_or_paths, &block) end |
.path ⇒ Object
Generates a path reference (via Path#[]) that grabs a nested value for use directly or in other transformations. If no path is specified, the transformation will use the input hash.
When the path is dereferenced, if any of the parent elements referred to by the path are nil, then nil will be returned. If any path elements do not respond to [], or otherwise raise an exception, then an exception will be raised by the transformation.
The major difference between .path and .chain is that .path will return nil if a nonexistent key is referenced (even multiple times), while .chain will raise an exception.
Examples:
HF::G.path[:user][:address][:line1]
HF::G.path[:lines][5]
271 272 273 |
# File 'lib/hashformer/generate.rb', line 271 def self.path Path.new end |