Sha256: 58d32b92cb8a37cb4adf225907407f9938cddf820ffbfded0b1bc148869a1f9a

Contents?: true

Size: 691 Bytes

Versions: 6

Compression:

Stored size: 691 Bytes

Contents

# convert date columns to numerics
require "csv"

class Masticate::Datify < Masticate::Base
  def configure(opts)
    standard_options(opts)
    @field = opts[:field] or raise "missing field to datify"
    @format = opts[:format] or raise "strptime format required for parsing timestamps"
  end

  def datify(opts)
    execute(opts)
  end

  def crunch(row)
    if !@index
      if @field.is_a?(Fixnum) || @field =~ /^\d+/
        @index = @field.to_i
      else
        @index = row.index(@field) or raise "Unable to find column '#{@field}'"
      end
    elsif row
      ts = DateTime.strptime(row[@index], @format).to_time
      row[@index] = ts.to_i rescue nil
    end
    row
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
masticate-0.4.0 lib/masticate/datify.rb
masticate-0.3.2 lib/masticate/datify.rb
masticate-0.3.1 lib/masticate/datify.rb
masticate-0.3 lib/masticate/datify.rb
masticate-0.2.3 lib/masticate/datify.rb
masticate-0.2.2 lib/masticate/datify.rb