Class: Masticate::Include

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

Overview

exclude rows based on field = value

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
13
# File 'lib/masticate/include.rb', line 4

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

#crunch(row) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/masticate/include.rb', line 19

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

#exclude(opts) ⇒ Object



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

def exclude(opts)
  execute(opts)
end