Skip to content

Instantly share code, notes, and snippets.

@leemour
Created September 4, 2013 12:59
Show Gist options
  • Save leemour/6436596 to your computer and use it in GitHub Desktop.
Save leemour/6436596 to your computer and use it in GitHub Desktop.
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