Skip to content

Commit

Permalink
Initial has_many filtering implementation [#36 state:resolved]
Browse files Browse the repository at this point in the history
  • Loading branch information
yrashk committed Apr 30, 2008
1 parent 6c34648 commit 4f028f9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
17 changes: 10 additions & 7 deletions lib/strokedb/document/associations.rb
Expand Up @@ -8,8 +8,13 @@ def view.map(uuid, doc)
through = self[:through]
expected_meta = self[:expected_meta]
expected_nsurl = self[:expected_nsurl]
conditions = self[:conditions]

if doc.meta.name == expected_meta && doc.meta.nsurl == expected_nsurl
if reference_slotname_value = doc[reference_slotname]
if (reference_slotname_value = doc[reference_slotname]) &&
(conditions.nil? ||
(conditions &&
(conditions.keys.select {|k| doc[k] == conditions[k]}.size == conditions.size)))
begin
through.each {|t| doc = doc.send(t) }
rescue SlotNotFoundError
Expand Down Expand Up @@ -38,9 +43,6 @@ def create!(slots={})
new(slots).save!
end

def find(query={})
association_owner._has_many_association(association_slotname,query)
end
def <<(doc)
doc.update_slots! association_reference_slotname => association_owner
self
Expand All @@ -67,6 +69,7 @@ def has_many(slotname, opts={}, &block)
meta = (through.shift || slotname).to_s.singularize.camelize
nsurl = opts['nsurl'] || (name.modulize.empty? ? Module.nsurl : meta.modulize.constantize.nsurl)
extend_with = opts['extend'] || block
conditions = opts['conditions']

@meta_initialization_procs << Proc.new do
case extend_with
Expand All @@ -93,11 +96,11 @@ def has_many(slotname, opts={}, &block)
# end

view = View.define!("#{name.modulize.empty? ? Module.nsurl : name.modulize.constantize.nsurl}##{name.demodulize.tableize.singularize}_has_many_#{slotname}",
{ :reference_slotname => reference_slotname, :through => through, :expected_meta => meta, :expected_nsurl => nsurl, :extend_with => extend_with }, &AssociationViewImplementation)
{ :reference_slotname => reference_slotname, :through => through, :expected_meta => meta, :expected_nsurl => nsurl, :extend_with => extend_with, :conditions => conditions }, &AssociationViewImplementation)

@args.last.reverse_merge!({"has_many_#{slotname}" => view})
define_method(slotname) do
_has_many_association(slotname,{})
_has_many_association(slotname)
end

end
Expand All @@ -107,7 +110,7 @@ def has_many(slotname, opts={}, &block)
private

def initialize_associations
define_method(:_has_many_association) do |slotname, additional_query|
define_method(:_has_many_association) do |slotname|
slot_has_many = meta["has_many_#{slotname}"]
result = LazyArray.new.load_with do |lazy_array|
slot_has_many.find(:key => self)
Expand Down
6 changes: 2 additions & 4 deletions spec/lib/strokedb/document/associations_spec.rb
Expand Up @@ -117,7 +117,7 @@
playlist = Playlist.create!
rock_song = Song.create!(:playlist => playlist, :genre => 'Rock')
pop_song = Song.create!(:playlist => playlist, :genre => 'Pop')
pending("filtering is not ready") do
pending("result filtering is not ready") do
playlist.songs.find(:genre => 'Rock').should == [rock_song]
playlist.songs.find(:genre => 'Pop').should == [pop_song]
end
Expand Down Expand Up @@ -200,9 +200,7 @@
playlist = Playlist.create!
rock_song = Song.create!(:playlist => playlist, :genre => 'Rock')
pop_song = Song.create!(:playlist => playlist, :genre => 'Pop')
pending("filtering is not done yet") do
playlist.rock_songs.should == [rock_song]
end
playlist.rock_songs.should == [rock_song]
end

end
Expand Down

0 comments on commit 4f028f9

Please sign in to comment.