Sha256: 6494451370c7a8946cde762b759dd1733cf149b264efa0a051d8ad8eff66a439

Contents?: true

Size: 1.64 KB

Versions: 26

Compression:

Stored size: 1.64 KB

Contents

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

Version data entries

26 entries across 26 versions & 2 rubygems

Version Path
smarter_csv-1.6.1 spec/smarter_csv/value_converters_spec.rb
smarter_csv-1.6.0 spec/smarter_csv/value_converters_spec.rb
smarter_csv-1.5.2 spec/smarter_csv/value_converters_spec.rb
smarter_csv-1.5.1 spec/smarter_csv/value_converters_spec.rb
smarter_csv-1.5.0 spec/smarter_csv/value_converters_spec.rb
smarter_csv-1.4.2 spec/smarter_csv/value_converters_spec.rb
smarter_csv-1.4.0 spec/smarter_csv/value_converters_spec.rb
smarter_csv-1.3.0 spec/smarter_csv/value_converters_spec.rb
smarter_csv-1.2.8 spec/smarter_csv/value_converters_spec.rb
smarter_csv-1.2.7 spec/smarter_csv/value_converters_spec.rb
pokedex-terminal-0.2.8 vendor/bundle/ruby/2.7.0/gems/smarter_csv-1.2.6/spec/smarter_csv/value_converters_spec.rb
pokedex-terminal-0.2.7 vendor/bundle/ruby/2.7.0/gems/smarter_csv-1.2.6/spec/smarter_csv/value_converters_spec.rb
pokedex-terminal-0.2.6 vendor/bundle/ruby/2.7.0/gems/smarter_csv-1.2.6/spec/smarter_csv/value_converters_spec.rb
pokedex-terminal-0.2.5 vendor/bundle/ruby/2.7.0/gems/smarter_csv-1.2.6/spec/smarter_csv/value_converters_spec.rb
pokedex-terminal-0.2.4 vendor/bundle/ruby/2.7.0/gems/smarter_csv-1.2.6/spec/smarter_csv/value_converters_spec.rb
smarter_csv-1.2.6 spec/smarter_csv/value_converters_spec.rb
smarter_csv-1.2.5 spec/smarter_csv/value_converters_spec.rb
smarter_csv-1.2.4 spec/smarter_csv/value_converters_spec.rb
smarter_csv-1.2.3 spec/smarter_csv/value_converters_spec.rb
smarter_csv-1.2.0 spec/smarter_csv/value_converters_spec.rb