lib/chop/create.rb in chop-0.15.0 vs lib/chop/create.rb in chop-0.16.0

- old
+ new

@@ -1,12 +1,13 @@ require "active_support/core_ext/string/inflections" require "active_support/core_ext/object/blank" +require "active_support/hash_with_indifferent_access" module Chop - class Create < Struct.new(:table, :klass, :block) - def self.create! table, klass, &block - new(table, klass, block).create! + class Create < Struct.new(:klass, :table, :block) + def self.create! klass, table, &block + new(klass, table, block).create! end attr_accessor :transformations def initialize(*, &other_block) @@ -16,11 +17,14 @@ instance_eval &other_block if block_given? end def create! cucumber_table = table cucumber_table.hashes.map do |attributes| - transformations.each { |transformation| transformation.call(attributes) } + attributes = HashWithIndifferentAccess.new(attributes) + attributes = transformations.reduce(attributes) do |attrs, transformation| + transformation.call(attrs) + end if klass.is_a?(Hash) if factory = klass[:factory_girl] FactoryGirl.create factory, attributes else raise "Unknown building strategy" @@ -35,31 +39,31 @@ transformations << block end def rename mappings transformation do |attributes| - mappings.each do |old, new| - attributes[new.to_s] = attributes.delete(old.to_s) if attributes.key?(old.to_s) + mappings.reduce(attributes) do |attrs, (old, new)| + attrs[new] = attrs.delete(old) if attrs.key?(old) + attrs end end end def field attribute, default: "" if attribute.is_a?(Hash) rename attribute attribute = attribute.values.first end transformation do |attributes| - attributes[attribute.to_s] = yield(attributes.fetch(attribute.to_s, default)) + attributes.merge attribute => yield(attributes.fetch(attribute, default)) end end def underscore_keys transformation do |attributes| - new_attributes = attributes.inject({}) do |hash, (key, value)| + attributes.reduce(HashWithIndifferentAccess.new) do |hash, (key, value)| hash.merge key.parameterize.underscore => value end - attributes.replace new_attributes end end def file *keys options = extract_options!(keys)