Class: Embulk::Filter::RubyProc
- Inherits:
-
FilterPlugin
- Object
- FilterPlugin
- Embulk::Filter::RubyProc
- Defined in:
- lib/embulk/filter/ruby_proc.rb
Defined Under Namespace
Classes: Evaluator
Class Method Summary collapse
- .procs ⇒ Object
- .row_procs ⇒ Object
- .transaction(config, in_schema) {|task, out_columns| ... } ⇒ Object
Instance Method Summary collapse
Class Method Details
.procs ⇒ Object
75 76 77 |
# File 'lib/embulk/filter/ruby_proc.rb', line 75 def self.procs @procs end |
.row_procs ⇒ Object
79 80 81 |
# File 'lib/embulk/filter/ruby_proc.rb', line 79 def self.row_procs @row_procs end |
.transaction(config, in_schema) {|task, out_columns| ... } ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/embulk/filter/ruby_proc.rb', line 31 def self.transaction(config, in_schema, &control) task = { "columns" => config.param("columns", :array, default: []), "rows" => config.param("rows", :array, default: []), "requires" => config.param("requires", :array, default: []), "variables" => config.param("variables", :hash, default: {}), } out_columns = in_schema.map do |col| target = task["columns"].find { |filter_col| filter_col["name"] == col.name } if target type = target["type"] ? target["type"].to_sym : col.type Embulk::Column.new(index: col.index, name: col.name, type: type || col.type, format: target["format"] || col.format) else col end end task["requires"].each do |lib| require lib end evaluator_binding = Evaluator.new(task["variables"]).get_binding # In order to avoid multithread probrem, initialize procs here @procs = Hash[task["columns"].map {|col| if col["proc"] [col["name"], eval(col["proc"], evaluator_binding)] else [col["name"], eval(File.read(col["proc_file"]), evaluator_binding, File.(col["proc_file"]))] end }] @row_procs = task["rows"].map {|rowdef| if rowdef["proc"] eval(rowdef["proc"], evaluator_binding) else eval(File.read(rowdef["proc_file"]), evaluator_binding, File.(rowdef["proc_file"])) end }.compact raise "Need columns or rows parameter" if @row_procs.empty? && @procs.empty? yield(task, out_columns) end |
Instance Method Details
#add(page) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/embulk/filter/ruby_proc.rb', line 92 def add(page) page.each do |record| if row_procs.empty? record_hashes = [hashrize(record)] else record_hashes = row_procs.each_with_object([]) do |pr, arr| result = pr.call(hashrize(record)) case result when Array result.each do |r| arr << r end when Hash arr << result else raise "row proc return value must be a Array or Hash" end end end record_hashes.each do |record_hash| procs.each do |col, pr| next unless record_hash.has_key?(col) next if record_hash[col].nil? && skip_nils[col] if pr.arity == 1 record_hash[col] = pr.call(record_hash[col]) else record_hash[col] = pr.call(record_hash[col], record_hash) end end page_builder.add(record_hash.values) end end end |
#close ⇒ Object
89 90 |
# File 'lib/embulk/filter/ruby_proc.rb', line 89 def close end |
#finish ⇒ Object
128 129 130 |
# File 'lib/embulk/filter/ruby_proc.rb', line 128 def finish page_builder.finish end |
#init ⇒ Object
83 84 85 86 87 |
# File 'lib/embulk/filter/ruby_proc.rb', line 83 def init @skip_nils = Hash[task["columns"].map {|col| [col["name"], col["skip_nil"].nil? ? true : !!col["skip_nil"]] }] end |