Created
January 30, 2012 12:44
-
-
Save kuon/1704214 to your computer and use it in GitHub Desktop.
This file contains 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
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