-
-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix; new features; value_converters; 1.1.0
- Loading branch information
Showing
8 changed files
with
165 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module SmarterCSV | ||
VERSION = "1.0.19" | ||
VERSION = "1.1.0" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
item,price | ||
Book,$9.99 | ||
Mug,$14.99 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
First-Name,Last-Name,Dogs,Cats,Birds,Fish | ||
Dan,McAllister,2,0,, | ||
Lucy,Laweless,,5,0, | ||
,,,,, | ||
Miles,O'Brian,0,0,0,21 | ||
Nancy,Homes,2,0,1, | ||
Hernán,Curaçon,3,0,0, | ||
,,,,, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
first,last,date,price | ||
Ben,Miller,10/30/1998,$44.50 | ||
Tom,Turner,2/1/2011,€15 | ||
Ken,Smith,01/09/2013,$0.11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'spec_helper' | ||
|
||
fixture_path = 'spec/fixtures' | ||
|
||
describe 'be_able_to' do | ||
it 'loads_file_with_dashes_in_header_fields as strings' do | ||
options = {:strings_as_keys => true} | ||
data = SmarterCSV.process("#{fixture_path}/with_dashes.csv", options) | ||
data.flatten.size.should == 5 | ||
data[0]['first_name'].should eq 'Dan' | ||
data[0]['last_name'].should eq 'McAllister' | ||
end | ||
|
||
it 'loads_file_with_dashes_in_header_fields as symbols' do | ||
options = {:strings_as_keys => false} | ||
data = SmarterCSV.process("#{fixture_path}/with_dashes.csv", options) | ||
data.flatten.size.should == 5 | ||
data[0][:first_name].should eq 'Dan' | ||
data[0][:last_name].should eq 'McAllister' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require 'spec_helper' | ||
|
||
fixture_path = 'spec/fixtures' | ||
|
||
require 'date' | ||
class DateConverter | ||
def self.convert(value) | ||
Date.strptime( value, '%m/%d/%Y') | ||
end | ||
end | ||
|
||
class CurrencyConverter | ||
def self.convert(value) | ||
value.sub(/[$€]/,'').to_f # would be nice to add a computed column :currency => '€' | ||
end | ||
end | ||
|
||
describe 'be_able_to' do | ||
it 'convert date values into Date instances' do | ||
options = {:value_converters => {:date => DateConverter}} | ||
data = SmarterCSV.process("#{fixture_path}/with_dates.csv", options) | ||
data.flatten.size.should == 3 | ||
data[0][:date].class.should eq Date | ||
data[0][:date].to_s.should eq "1998-10-30" | ||
data[1][:date].to_s.should eq "2011-02-01" | ||
data[2][:date].to_s.should eq "2013-01-09" | ||
end | ||
|
||
it 'converts dollar prices into float values' do | ||
options = {:value_converters => {:price => CurrencyConverter}} | ||
data = SmarterCSV.process("#{fixture_path}/money.csv", options) | ||
data.flatten.size.should == 2 | ||
data[0][:price].class.should eq Float | ||
data[0][:price].should eq 9.99 | ||
data[1][:price].should eq 14.99 | ||
end | ||
|
||
it 'convert can use multiple value converters' do | ||
options = {:value_converters => {:date => DateConverter, :price => CurrencyConverter}} | ||
data = SmarterCSV.process("#{fixture_path}/with_dates.csv", options) | ||
data.flatten.size.should == 3 | ||
data[0][:date].class.should eq Date | ||
data[0][:date].to_s.should eq "1998-10-30" | ||
data[1][:date].to_s.should eq "2011-02-01" | ||
data[2][:date].to_s.should eq "2013-01-09" | ||
|
||
data[0][:price].class.should eq Float | ||
data[0][:price].should eq 44.50 | ||
data[1][:price].should eq 15.0 | ||
data[2][:price].should eq 0.11 | ||
end | ||
end |