Class: Embulk::Filter::ScriptRuby

Inherits:
FilterPlugin
  • Object
show all
Defined in:
lib/embulk/filter/script_ruby.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.transaction(config, in_schema) {|task, out_columns| ... } ⇒ Object

Yields:

  • (task, out_columns)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/embulk/filter/script_ruby.rb', line 7

def self.transaction(config, in_schema, &control)
  # configuration code:
  task = {
      'script'  => config.param('script', :string),
      'class'  => config.param('class', :string),
      'columns' => config.param('columns', :array, default: [])
  }
  
  c = 0
  out_columns = task['columns'].map do | e | 
    col = Column.new(c, e['name'], e['type'].to_sym)
    c+=1
 col
  end
  
  yield(task, out_columns)
end

Instance Method Details

#add(page) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/embulk/filter/script_ruby.rb', line 41

def add(page)
  # filtering code:
  page.each do |record|
    begin
      h = Hash[in_schema.names.zip(record)]
      result = @filter_class.filter(h)
      out_record = []
      out_schema.sort_by{|e| e['index']}.each do | e |
        out_record << (result.has_key?(e['name']) ? result[e['name']] : nil)
      end
      page_builder.add(out_record) if out_record.size > 0
    rescue => e
      raise e.to_s + " backtrace: " + e.backtrace.to_s 
    end
  end
end

#closeObject



38
39
# File 'lib/embulk/filter/script_ruby.rb', line 38

def close
end

#finishObject



58
59
60
# File 'lib/embulk/filter/script_ruby.rb', line 58

def finish
  page_builder.finish
end

#initObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/embulk/filter/script_ruby.rb', line 25

def init
  # initialization code:
  @script = task['script']
 
  @out_map = {}
  out_schema.each do | e |
    @out_map[e['name']] = true
  end
  
  require @script
  @filter_class = Object.const_get(task['class']).new()
end