lib/differential/parser/reader.rb in differential-1.0.6 vs lib/differential/parser/reader.rb in differential-1.1.0

- old
+ new

@@ -29,34 +29,25 @@ @record_id_key = record_id_key @value_key = value_key @group_id_key = group_id_key end - def each(hashes) - return enum_for(:each) unless block_given? - - array(hashes).each do |hash| - next unless hash - - record = read(hash) - yield record - end - end - - private - def read(hash) id = make_record_id(hash) group_id = make_group_id(hash) value = hash[value_key] - ::Differential::Parser::Record.new(id: id, - group_id: group_id, - value: value, - data: hash) + ::Differential::Parser::Record.new( + id: id, + group_id: group_id, + value: value, + data: hash + ) end + private + def make_record_id(hash) record_id_key_array.map { |k| hash[k] } end def make_group_id(hash) @@ -72,11 +63,15 @@ end def array(hashes) if hashes.is_a?(Hash) [hashes] - else + # If the consumer passed in a value that responds to each (is enumerable-like) then + # we can accept that as is. + elsif !hashes.respond_to?(:each) Array(hashes) + else + hashes end end end end end