Created
March 19, 2020 15:46
-
-
Save ttilberg/fd29a91676aa126dd5f558df5a9667aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'kiba' | |
require 'awesome_print' | |
class ExampleSource | |
def each | |
(1..10).each do |i| | |
yield ({ | |
index: i | |
}) | |
end | |
end | |
end | |
class BufferedPrinter | |
def initialize(buffer: 3) | |
@buffer = buffer | |
@rows = [] | |
end | |
def write(row) | |
@rows << row | |
flush if @rows.size >= @buffer | |
end | |
def close | |
flush | |
end | |
def flush | |
@rows.each {|r| ap r} | |
@rows = [] | |
end | |
end | |
class ArrayExploder | |
def process(row) | |
if row.is_a? Array | |
row.each{|r| yield r} | |
return nil | |
end | |
row | |
end | |
end | |
job = Kiba.parse do | |
source ExampleSource | |
transform do |row| | |
[1, 2, 3, 4, 5].map do |i| | |
row.merge(sub_index: i) | |
end | |
end | |
transform ArrayExploder | |
transform do |row| | |
%w(guy buddy pal).map do |guy| | |
row.merge(guy: guy) | |
end | |
end | |
transform ArrayExploder | |
destination BufferedPrinter | |
end | |
Kiba.run job |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment