lib/key_mapable/mapper.rb in key_mapable-0.3.0 vs lib/key_mapable/mapper.rb in key_mapable-0.4.0
- old
+ new
@@ -17,31 +17,28 @@
child_mapper = self.class.new(subject)
child_mapper.instance_eval(&block)
@structure[name] = child_mapper.resolve
end
- def key_map(original_key, new_key, &block)
+ def key_map(original_key, new_key, transform = ->(val) { val }, &block)
original_subject = subject.public_send(original_key)
+ transformed_subject = transform.call(original_subject)
if block_given?
- child_mapper = self.class.new(original_subject)
+ child_mapper = self.class.new(transformed_subject)
child_mapper.instance_eval &block
@structure[new_key] = child_mapper.resolve
else
- @structure[new_key] = original_subject
+ @structure[new_key] = transformed_subject
end
end
def array_key_map(original_key, new_key, &block)
original_subject = subject.public_send(original_key)
@structure[new_key] = original_subject.map do |item|
child_mapper = self.class.new(item)
child_mapper.instance_eval(&block)
child_mapper.resolve
end
- end
-
- def transform
- @structure = yield(@subject)
end
def resolve
@structure
end