Sha256: 8366802c79c84863816734a76a4f3f8528a3204b40264dffd68a193f5e6f3956

Contents?: true

Size: 694 Bytes

Versions: 1

Compression:

Stored size: 694 Bytes

Contents

# extract subset of columns from CSV
require "csv"

class Masticate::Gsubber < Masticate::Base
  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

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

  def gsub(opts)
    execute(opts)
  end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
masticate-0.2.1 lib/masticate/gsubber.rb