Class: Masticate::Transform

Inherits:
Base
  • Object
show all
Defined in:
lib/masticate/transform.rb

Overview

apply transformation rules to a field

Instance Attribute Summary

Attributes inherited from Base

#csv_options, #filename, #input, #input_count, #output, #output_count

Instance Method Summary collapse

Methods inherited from Base

#emit, #execute, #get, #initialize, #standard_options, #with_input

Constructor Details

This class inherits a constructor from Masticate::Base

Instance Method Details

#configure(opts) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/masticate/transform.rb', line 4

def configure(opts)
  standard_options(opts)

  @field = opts[:field] or raise "missing field to transform"
  @rule = opts[:rule] or raise "missing transformation rule"
  unless ['upcase', 'downcase'].include?(@rule)
    raise "invalid transformation rule <#{@rule}>: supported rules are downcase, upcase"
  end
end

#crunch(row) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/masticate/transform.rb', line 23

def crunch(row)
  if !@headers
    set_headers(row)
  elsif row
    row[@index] = case @rule
    when 'downcase'
      row[@index].downcase
    when 'upcase'
      row[@index].upcase
    else
      raise "no code for rule #{@rule}"
    end
  end

  row
end

#set_headers(row) ⇒ Object



14
15
16
17
# File 'lib/masticate/transform.rb', line 14

def set_headers(row)
  @headers = row
  @index = @headers.index(@field) or raise "Unable to find column '#{@field}' in headers"
end

#transform(opts) ⇒ Object



19
20
21
# File 'lib/masticate/transform.rb', line 19

def transform(opts)
  execute(opts)
end