class Embulk::Filter::Script

Public Class Methods

new(task, in_schema, out_schema, page_builder) click to toggle source
Calls superclass method
# File lib/embulk/filter/script.rb, line 28
def initialize(task, in_schema, out_schema, page_builder)
  super
  load_script_file(task['path'])
end
out_schema(drop_columns, in_schema) click to toggle source
# File lib/embulk/filter/script.rb, line 16
def self.out_schema(drop_columns, in_schema)
  idx = 0
  schema = []
  in_schema.each do |sch|
    unless drop_columns.find {|n| n == sch.name}
      schema << Column.new(idx, sch.name, sch.type, sch.format)
      idx += 1
    end
  end
  schema
end
transaction(config, in_schema) { |task, out_schema| ... } click to toggle source
# File lib/embulk/filter/script.rb, line 7
def self.transaction(config, in_schema, &control)
  task = {
    'path'  => config.param('path', :string),
    'drop_columns' => config.param('drop_columns', :array, default: [])
  }
  out_schema = out_schema(task['drop_columns'], in_schema)
  yield(task, out_schema)
end

Public Instance Methods

add(page) click to toggle source
# File lib/embulk/filter/script.rb, line 33
def add(page)
  task
  page.each do |record|
    result = {}
    filter(hash_record(record)).each do |key, value|
      unless task['drop_columns'].find {|n| n == key }
        result[key] = value
      end
    end
    @page_builder.add(result.values)
  end
end
finish() click to toggle source
# File lib/embulk/filter/script.rb, line 46
def finish
  @page_builder.finish
end

Private Instance Methods

hash_record(record) click to toggle source
# File lib/embulk/filter/script.rb, line 57
def hash_record(record)
  Hash[in_schema.names.zip(record)]
end
load_script_file(path) click to toggle source
# File lib/embulk/filter/script.rb, line 52
def load_script_file(path)
  raise ConfigError, "Ruby script file does not exist: #{path}" unless File.exist?(path)
  eval "self.instance_eval do;" + IO.read(path) + ";end"
end