Class: CSVConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_converter.rb

Instance Method Summary collapse

Constructor Details

#initialize(convert) ⇒ CSVConverter

Returns a new instance of CSVConverter.



2
3
4
5
# File 'lib/csv_converter.rb', line 2

def initialize(convert)
  @key = convert.keys.first
  @conversion = convert[@key]
end

Instance Method Details

#convert(hash) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/csv_converter.rb', line 7

def convert(hash)
  case @conversion
  when :integer
    hash[@key] = hash[@key].to_i
  when :float
    hash[@key] = hash[@key].to_f
  when :string
    # nop, default
  else
    raise ArgumentError.new("Unknown converter: `#{@conversion}`")
  end

  hash
end