Class: Ethel::Operations::Cast

Inherits:
Ethel::Operation show all
Defined in:
lib/ethel/operations/cast.rb

Instance Method Summary collapse

Methods inherited from Ethel::Operation

#before_transform

Constructor Details

#initialize(field, new_type) ⇒ Cast

Returns a new instance of Cast.



4
5
6
7
8
9
10
11
# File 'lib/ethel/operations/cast.rb', line 4

def initialize(field, new_type)
  super
  @original_field = field
  @field_name = field.name
  @new_type = new_type
  @new_field = Field.new(@field_name, :type => @new_type)
  add_child_operation(AddField.new(@new_field))
end

Instance Method Details

#transform(row) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ethel/operations/cast.rb', line 13

def transform(row)
  row = super(row)

  row[@field_name] =
    case @new_type
    when :integer
      row[@field_name].to_i
    when :string
      row[@field_name].to_s
    end

  row
end