Skip to content

Instantly share code, notes, and snippets.

@kuon
Created January 30, 2012 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuon/1704214 to your computer and use it in GitHub Desktop.
Save kuon/1704214 to your computer and use it in GitHub Desktop.
enumerator = self.dynamo_db_table.items.query(options)
# this enumerator returns some proxy objects and can be used like so
enumerator.each do |item|
# do something with item, convert it into real item
data = item.attributes.to_h
obj = self.new(:shard => table)
obj.send(:hydrate, id, data)
# Now do something with obj
obj
end
# I'd like to wrap enumerator so I can do
enumerator.each do |obj|
# no conversion needed, the above code has already been called
end
# This can be achieved with the following code
result = []
enumerator.each do |item|
# do something with item, convert it into real item
data = item.attributes.to_h
obj = self.new(:shard => table)
obj.send(:hydrate, id, data)
# Now do something with obj
result << obj
end
return result
# But this code is not lazy, I don't want to populate an array, I want
# to wrap the unerlying enumerator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment