Sha256: ca8195f7c700fc403f83de8d7867dbdef074d4990372dc9740a80d9170a47334

Contents?: true

Size: 805 Bytes

Versions: 12

Compression:

Stored size: 805 Bytes

Contents

# extract subset of columns from CSV
require "csv"

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

    @fields = opts[:fields] or raise "missing fields to pluck"
  end

  def pluck(opts)
    execute(opts)
  end

  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
end

Version data entries

12 entries across 12 versions & 1 rubygems

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