Sha256: 47030ce1ec5b2e85cab60d51bc1a81b6b5f53f38825f2411db93524f511ef2f8

Contents?: true

Size: 1.02 KB

Versions: 5

Compression:

Stored size: 1.02 KB

Contents

# exclude rows based on field = value

class Masticate::Include < Masticate::Base
  def configure(opts)
    standard_options(opts)

    @field = opts[:field] or raise "missing field to include"
    @value = opts[:value] or raise "missing value to include"

    # row-loading automatically strips leading & trailing whitespace and converts blanks to nils,
    # so when looking for blanks need to compare to nil instead of ''
    @value = nil if @value.empty?
  end

  def exclude(opts)
    execute(opts)
  end

  def crunch(row)
    if !@headers
      @headers = row
      f = @field
      @index =
        case f
        when Fixnum, /^\d+$/
          f = f.to_i
          if f > row.count
            raise "Cannot pluck column #{f}, there are only #{row.count} fields"
          else
            f-1
          end
        else
          row.index(f) or raise "Unable to find column '#{f}' in headers"
        end
      row
    elsif row
      if row[@index] == @value
        # include only these matches
        row
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
masticate-0.6.2 lib/masticate/include.rb
masticate-0.6.1 lib/masticate/include.rb
masticate-0.6.0 lib/masticate/include.rb
masticate-0.5.1 lib/masticate/include.rb
masticate-0.5.0 lib/masticate/include.rb