Skip to content
Browse files

Builder coerces any enumerable into an Array

  • Loading branch information...
1 parent e8e127d commit 1a195ce1b2ef87577ef30577682dd6254452b538 @tatey committed Jan 19, 2012
View
1 conformist.gemspec
@@ -27,6 +27,7 @@ EOS
s.add_development_dependency 'rake'
s.add_development_dependency 'minitest'
+ s.add_development_dependency 'spreadsheet'
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
View
4 lib/conformist/column.rb
@@ -8,8 +8,8 @@ def initialize name, *indexes, &preprocessor
self.preprocessor = preprocessor
end
- def values_in array
- values = array.values_at(*indexes).map do |value|
+ def values_in enumerable
+ values = Array(enumerable).values_at(*indexes).map do |value|
if value.respond_to? :strip
value.strip
else
View
BIN test/fixtures/states.xls
Binary file not shown.
View
9 test/unit/conformist/column_test.rb
@@ -40,6 +40,15 @@ def test_strip_whitespace
assert_equal 'a', column.values_in([' a '])
end
+ def test_array
+ mock = MiniTest::Mock.new
+ mock.expect :to_a, ['a']
+ column = Conformist::Column.new :foo, 0
+ assert_equal 'a', column.values_in(['a'])
+ assert_equal 'a', column.values_in('a')
+ assert_equal 'a', column.values_in(mock)
+ end
+
def test_nil
column = Conformist::Column.new(:foo, 0)
assert_nil column.values_in([])
View
19 test/unit/integration_test.rb
@@ -2,14 +2,18 @@
require 'helper'
require 'schemas/acma'
require 'schemas/fcc'
+require 'spreadsheet'
class IntegrationTest < MiniTest::Unit::TestCase
+ def fixture filename
+ File.expand_path "../../fixtures/#{filename}", __FILE__
+ end
+
def open_csv filename, options = {}
- path = File.expand_path "../../fixtures/#{filename}", __FILE__
if CSV.method(:open).arity == -3 # 1.8 CSV
- CSV.open path, 'r', options[:col_sep]
+ CSV.open fixture(filename), 'r', options[:col_sep]
else
- CSV.open path, options
+ CSV.open fixture(filename), options
end
end
@@ -31,6 +35,15 @@ def test_class_with_psv
assert_equal HashStruct.new(:name => 'LOS ANGELES, CA', :callsign => 'KVTU-LP', :latitude => '34 13 38.00 N', :signtal_type => 'digital'), last
end
+ def test_instance_with_spreadsheet
+ book = Spreadsheet.open fixture('states.xls')
+ sheet = book.worksheet 0
+ schema = Conformist.new { column :state, 0 }
+ enumerable = schema.conform sheet
+ last = enumerable.to_a.last
+ assert_equal HashStruct.new(:state => 'QLD'), last
+ end
+
def test_instance_with_array_of_arrays
data = Array.new.tap do |d|
d << ['NSW', 'New South Wales', 'Sydney']

0 comments on commit 1a195ce

Please sign in to comment.
Something went wrong with that request. Please try again.