Skip to content

Instantly share code, notes, and snippets.

@TSFoster
Created October 7, 2014 14:58
Show Gist options
  • Save TSFoster/9e035b8e2c704f5ff066 to your computer and use it in GitHub Desktop.
Save TSFoster/9e035b8e2c704f5ff066 to your computer and use it in GitHub Desktop.
class BatchItemProcessor
attr_accessor :processed_items
def initialize
reset
end
def process_items items
items.each do |item|
[email protected](item)
unless @processed_items.include? identifier
@processed_items.push identifier
yield item if @processor.call(item)
end
end
end
def reset
@processed_items = []
@identifier = proc{|x| x}
@processor = proc{|x| true}
end
def identify identifier
@identifier = proc{|x| x.is_a?(Hash) ? x[identifier] : x.send(identifier)}
end
def should_process &block
@processor = block
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment