From 9df2c1dc774facb6fa23b5b240118c12787e757a Mon Sep 17 00:00:00 2001 From: Ryan Wallace Date: Thu, 12 Feb 2015 10:47:54 -0800 Subject: [PATCH] Always return nil when named column isn't found. --- lib/conformist/column.rb | 8 ++++++-- test/unit/integration_test.rb | 9 +++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/conformist/column.rb b/lib/conformist/column.rb index eb1f9a8..c9652c9 100644 --- a/lib/conformist/column.rb +++ b/lib/conformist/column.rb @@ -18,11 +18,15 @@ def calculate_indices!(headers) else source end - end.compact + end end def values_in enumerable - values = Array(enumerable).values_at(*indexes).map do |value| + enumerable = Array(enumerable) + + values = Array(indexes).map do |index| + value = enumerable.at(index) if index + if value.respond_to? :strip value.strip else diff --git a/test/unit/integration_test.rb b/test/unit/integration_test.rb index 37b12f8..9c82974 100644 --- a/test/unit/integration_test.rb +++ b/test/unit/integration_test.rb @@ -100,4 +100,13 @@ def test_named_columns first = enumerable.to_a.first assert_equal HashStruct.new(:name => 'Aaron', :age => '21', :gender => 'Male'), first end + + def test_missing_named_column + schema = Conformist.new do + column :state, 'State' + end + enumerable = schema.conform open_csv('citizens.csv'), :skip_first => true + first = enumerable.to_a.first + assert_equal HashStruct.new(:state => nil), first + end end