Class: Masticate::Plucker

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

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



5
6
7
8
9
10
11
12
# File 'lib/masticate/plucker.rb', line 5

def configure(opts)
  standard_options(opts)

  @fields = opts[:fields] or raise "missing fields to pluck"
  if @fields.is_a?(String)
    @fields = @fields.split(/,\s*/)
  end
end

#crunch(row) ⇒ Object



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

def crunch(row)
  if !@headers
    @headers = row
    @indexes = @fields.map do |f|
      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}'"
      end
    end
    @indexes.map {|i| row[i]}
  elsif row
    # output is just the selected columns
    @indexes.map {|i| row[i]}
  end
end

#pluck(opts) ⇒ Object



14
15
16
# File 'lib/masticate/plucker.rb', line 14

def pluck(opts)
  execute(opts)
end