lib/hashformer.rb in hashformer-0.2.1 vs lib/hashformer.rb in hashformer-0.2.2
- old
+ new
@@ -7,24 +7,25 @@
require 'hashformer/version'
require 'hashformer/generate'
-# This module contains the Hashformer methods for transforming Ruby Hash objects
-# from one form to another.
+# This module contains the Hashformer methods for transforming Ruby Hash
+# objects from one form to another.
#
# See README.md for examples.
module Hashformer
# Transforms +data+ according to the specification in +xform+. The
# transformation specification in +xform+ is a Hash specifying an input key
- # name (e.g. a String or Symbol) or transforming lambda for each output key
- # name. If +validate+ is true, then ClassyHash::validate will be used to
- # validate the input and output data formats against the :@__in_schema and
- # :@__out_schema keys within +xform+, if specified.
+ # name (e.g. a String or Symbol), generator, or transforming lambda for each
+ # output key name. If +validate+ is true, then ClassyHash::validate will be
+ # used to validate the input and output data formats against the
+ # :@__in_schema and :@__out_schema keys within +xform+, if specified.
#
- # Nested transformations can be specified by calling Hashformer::transform
- # again inside of a lambda.
+ # Nested transformations can be specified by using a Hash as the
+ # transformation value, or by calling Hashformer.transform again inside of a
+ # lambda.
#
# If a value in +xform+ is a Proc, the Proc will be called with the input
# Hash, and the return value of the Proc used as the output value.
#
# If a key in +xform+ is a Proc, the Proc will be called with the exact
@@ -44,30 +45,33 @@
out = {}
xform.each do |key, value|
next if key == :__in_schema || key == :__out_schema
key = key.call(value, data) if key.respond_to?(:call)
- out[key] = self.get_value(data, value)
+ out[key] = self.get_value(data, value, xform)
end
validate(out, xform[:__out_schema], 'output') if validate
out
end
# Returns a value for the given +key+, method chain, or callable on the given
- # +input_hash+.
- def self.get_value(input_hash, key)
+ # +input_hash+. If +xform+ is not nil, then Hashe keys will be processed
+ # with Hashformer.transform.
+ def self.get_value(input_hash, key, xform = nil)
if Hashformer::Generate::Chain::Receiver === key
# Had to special case chains to allow chaining .call
key.__chain.call(input_hash)
+ elsif Hashformer::Generate::Constant === key
+ key.value
elsif key.respond_to?(:call)
key.call(input_hash)
+ elsif key.is_a?(Hash)
+ transform(input_hash, key)
else
input_hash[key]
end
-
- # TODO: add support for nested output hashes
end
private
# Validates the given data against the given schema, at the given step.
def self.validate(data, schema, step)