Created
September 4, 2013 12:59
-
-
Save leemour/6436596 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class BatchItemProcessor | |
attr_accessor :processed_items | |
def initialize | |
@processed_items = [] | |
end | |
def process_items(list) | |
list = list.select(&@condition) if @condition | |
items = parse list | |
items -= @processed_items | |
@processed_items += items | |
items.map { |item| yield item } unless items.empty? | |
end | |
def parse(items) | |
items.map do |item| | |
if item.is_a? Hash | |
result = @id ? item[@id] : item[@id].object_id | |
else | |
result = item | |
end | |
end.compact | |
end | |
def identify(id) | |
@id = id | |
end | |
def should_process(&block) | |
@condition = block | |
end | |
def reset | |
@processed_items = [] | |
@id = nil | |
@condition = nil | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment