Skip to content

Instantly share code, notes, and snippets.

@mbj
Created November 27, 2012 14:44
Show Gist options
  • Save mbj/4154585 to your computer and use it in GitHub Desktop.
Save mbj/4154585 to your computer and use it in GitHub Desktop.
Poor mans RA with dm-1
class Foo
property :id, Seriaal
property :name, STring
has n, :bar
# Enumerate bar with name
#
# @return [self]
# if block given
#
# @return [Enumerator<Array>]
# otherwise
#
# @api private
#
def bar_with_name(query={})
return to_enum(__method__) unless block_given?
bar.all(query).each do |bar|
yield [bar, name]
end
self
end
end
class Bar
belongs_to :foo
property :something
end
Foo.first.bar_with_name(:your => :restriction).each do |bar, name|
# do your stuff
end
@dkubb
Copy link

dkubb commented Nov 27, 2012

@mbj you probably want to use each there not map since you don't want to return the block results.

@mbj
Copy link
Author

mbj commented Nov 27, 2012

@dkubb, yeah this is a refactoring leftover, I started with map and than realized the each version with its lazyness is best... but did not changed the map. Thx for spotting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment