Skip to content

Commit

Permalink
Add a null process to Preprocessor
Browse files Browse the repository at this point in the history
Fixes Andrew's 'refactor'.
  • Loading branch information
fkotsian committed May 21, 2015
1 parent 03ab4c6 commit e4292ad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
12 changes: 8 additions & 4 deletions lib/bookbinder/preprocessing/preprocessor.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
module Bookbinder
module Preprocessing
class Preprocessor
def initialize(*processes, default: nil)
class NullProcess
def preprocess(*)
end
end

def initialize(*processes)
@processes = processes
@default = default
end

def preprocess(objs, *args, &block)
objs.group_by { |obj|
processes.detect ->{ default } { |process| process.applicable_to?(obj) }
processes.detect ->{ NullProcess.new } { |process| process.applicable_to?(obj) }
}.each do |process, objs|
process.preprocess(objs, *args)
end
end

private

attr_reader :processes, :default
attr_reader :processes
end
end
end
17 changes: 7 additions & 10 deletions spec/lib/bookbinder/preprocessing/preprocessor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,23 @@ module Preprocessing
preprocessor.preprocess(objs, 'extra', 'args')
end

it "applies the default process to objects that aren't applicable to any process" do
it "does nothing to objects that aren't applicable to any process" do
objs = [
Object.new,
Object.new,
Object.new,
Object.new,
]

process_1 = double('default process')
process_2 = double('some other process')
process_1 = double('some other process')
process_2 = double('second process')

preprocessor = Preprocessor.new(process_2, default: process_1)

block = ->(*){}
preprocessor = Preprocessor.new(process_1, process_2)

allow(process_1).to receive(:applicable_to?) { false }
allow(process_2).to receive(:applicable_to?) { |obj| objs[2..3].include?(obj) }
allow(process_1).to receive(:applicable_to?) { |obj| objs[2..3].include?(obj) }
allow(process_2).to receive(:applicable_to?) { false }

expect(process_1).to receive(:preprocess).with(objs[0..1], 'extra', 'args')
expect(process_2).to receive(:preprocess).with(objs[2..3], 'extra', 'args')
expect(process_1).to receive(:preprocess).with(objs[2..3], 'extra', 'args')

preprocessor.preprocess(objs, 'extra', 'args')
end
Expand Down

0 comments on commit e4292ad

Please sign in to comment.