Class: Masticate::Gsubber

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

Overview

apply regex transformation 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
# File 'lib/masticate/gsubber.rb', line 4

def configure(opts)
  standard_options(opts)

  @field = opts[:field] or raise "missing field to gsub"
  @from = Regexp.new(opts[:from]) or raise "Invalid regex '#{opts[:from]}' for conversion"
  @to = opts[:to] or raise "missing 'to' string for gsub"
end

#crunch(row) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/masticate/gsubber.rb', line 21

def crunch(row)
  if !@headers
    set_headers(row)
  elsif row
    row[@index] = row[@index].gsub(@from, @to)
  end
  row
end

#gsub(opts) ⇒ Object



17
18
19
# File 'lib/masticate/gsubber.rb', line 17

def gsub(opts)
  execute(opts)
end

#set_headers(row) ⇒ Object



12
13
14
15
# File 'lib/masticate/gsubber.rb', line 12

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