Skip to content

Commit

Permalink
Simplify context and runner code
Browse files Browse the repository at this point in the history
Hat-tip to @ollie for pushing for this (see #3).
  • Loading branch information
thbar committed Jun 21, 2015
1 parent 938e816 commit a483f3a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
2 changes: 2 additions & 0 deletions Changes.md
@@ -1,6 +1,8 @@
Unreleased
----------

- Internal refactoring of processing engine (should not affect regular use)

0.6.0
-----

Expand Down
10 changes: 3 additions & 7 deletions lib/kiba/context.rb
Expand Up @@ -6,27 +6,23 @@ def initialize(control)
end

def pre_process(&block)
@control.pre_processes << block
@control.pre_processes << { block: block }
end

def source(klass, *initialization_params)
@control.sources << { klass: klass, args: initialization_params }
end

def transform(klass = nil, *initialization_params, &block)
if klass
@control.transforms << { klass: klass, args: initialization_params }
else
@control.transforms << block
end
@control.transforms << { klass: klass, args: initialization_params, block: block }
end

def destination(klass, *initialization_params)
@control.destinations << { klass: klass, args: initialization_params }
end

def post_process(&block)
@control.post_processes << block
@control.post_processes << { block: block }
end
end
end
25 changes: 13 additions & 12 deletions lib/kiba/runner.rb
@@ -1,5 +1,10 @@
module Kiba
module Runner
# allow to handle a block form just like a regular transform
class AliasingProc < Proc
alias_method :process, :call
end

def run(control)
# instantiate early so that error are raised before any processing occurs
pre_processes = to_instances(control.pre_processes, true, false)
Expand All @@ -18,13 +23,7 @@ def process_rows(sources, transforms, destinations)
sources.each do |source|
source.each do |row|
transforms.each do |transform|
# TODO: avoid the case completely by e.g. subclassing Proc
# and aliasing `process` to `call`. Benchmark needed first though.
if transform.is_a?(Proc)
row = transform.call(row)
else
row = transform.process(row)
end
row = transform.process(row)
break unless row
end
next unless row
Expand All @@ -38,13 +37,15 @@ def process_rows(sources, transforms, destinations)
# not using keyword args because JRuby defaults to 1.9 syntax currently
def to_instances(definitions, allow_block = false, allow_class = true)
definitions.map do |d|
case d
when Proc
fail 'Block form is not allowed here' unless allow_block
d
else
if d[:klass]
fail 'Class form is not allowed here' unless allow_class
d[:klass].new(*d[:args])
elsif d[:block]
fail 'Block form is not allowed here' unless allow_block
AliasingProc.new(&d[:block])
else
# TODO: support block passing to a class form definition?
fail "Class and block form cannot be used together at the moment"
end
end
end
Expand Down
1 change: 1 addition & 0 deletions test/test_integration.rb
Expand Up @@ -3,6 +3,7 @@
require_relative 'support/test_csv_source'
require_relative 'support/test_csv_destination'
require_relative 'support/test_rename_field_transform'
require_relative 'support/test_enumerable_source'

# End-to-end tests go here
class TestIntegration < Kiba::Test
Expand Down
6 changes: 3 additions & 3 deletions test/test_parser.rb
Expand Up @@ -20,7 +20,7 @@ def test_block_transform_definition
transform { |row| row }
end

assert_instance_of Proc, control.transforms[0]
assert_instance_of Proc, control.transforms[0][:block]
end

def test_class_transform_definition
Expand All @@ -46,15 +46,15 @@ def test_block_post_process_definition
post_process {}
end

assert_instance_of Proc, control.post_processes[0]
assert_instance_of Proc, control.post_processes[0][:block]
end

def test_block_pre_process_definition
control = Kiba.parse do
pre_process {}
end

assert_instance_of Proc, control.pre_processes[0]
assert_instance_of Proc, control.pre_processes[0][:block]
end

def test_source_as_string_parsing
Expand Down
10 changes: 5 additions & 5 deletions test/test_runner.rb
Expand Up @@ -22,22 +22,22 @@ class TestRunner < Kiba::Test

def test_block_transform_processing
# is there a better way to assert a block was called in minitest?
control.transforms << lambda { |r| @called = true; r }
control.transforms << { block: lambda { |r| @called = true; r } }
Kiba.run(control)
assert_equal true, @called
end

def test_dismissed_row_not_passed_to_next_transform
control.transforms << lambda { |_| nil }
control.transforms << lambda { |_| @called = true; nil }
control.transforms << { block: lambda { |_| nil } }
control.transforms << { block: lambda { |_| @called = true; nil } }
Kiba.run(control)
assert_nil @called
end

def test_post_process_runs_once
assert_equal 2, rows.size
@called = 0
control.post_processes << lambda { @called += 1 }
control.post_processes << { block: lambda { @called += 1 } }
Kiba.run(control)
assert_equal 1, @called
end
Expand All @@ -52,7 +52,7 @@ def test_post_process_not_called_after_row_failure
def test_pre_process_runs_once
assert_equal 2, rows.size
@called = 0
control.pre_processes << lambda { @called += 1 }
control.pre_processes << { block: lambda { @called += 1 } }
Kiba.run(control)
assert_equal 1, @called
end
Expand Down

0 comments on commit a483f3a

Please sign in to comment.