Sha256: cd753e9fa20d3cad598e69c09d0189b39bfabbf993207ed67af0f1e5f707fce9

Contents?: true

Size: 683 Bytes

Versions: 13

Compression:

Stored size: 683 Bytes

Contents

# apply regex transformation to a field

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

13 entries across 13 versions & 1 rubygems

Version Path
masticate-0.6.2 lib/masticate/gsubber.rb
masticate-0.6.1 lib/masticate/gsubber.rb
masticate-0.6.0 lib/masticate/gsubber.rb
masticate-0.5.1 lib/masticate/gsubber.rb
masticate-0.5.0 lib/masticate/gsubber.rb
masticate-0.4.2 lib/masticate/gsubber.rb
masticate-0.4.1 lib/masticate/gsubber.rb
masticate-0.4.0 lib/masticate/gsubber.rb
masticate-0.3.2 lib/masticate/gsubber.rb
masticate-0.3.1 lib/masticate/gsubber.rb
masticate-0.3 lib/masticate/gsubber.rb
masticate-0.2.3 lib/masticate/gsubber.rb
masticate-0.2.2 lib/masticate/gsubber.rb