Skip to content

Commit

Permalink
Now Document.new and #update_slots are able to accept 'method-based' …
Browse files Browse the repository at this point in the history
…slots, like:

User = Meta.new do

   def email=(v)
     # ...
   end

end

User.new(:email => "yrashk@idbns.com") or user.update_slots(:email => "yrashk@idbns.com") will pass control to User#email= instead of saving it directly
to the slot.
  • Loading branch information
Yurii Rashkovskii committed May 4, 2008
1 parent 18b624b commit feb7619
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/strokedb/document.rb
Expand Up @@ -432,7 +432,7 @@ def save!(perform_validation = true)
# Updates slots with a specified <tt>hash</tt> and returns itself.
def update_slots(hash)
hash.each do |k, v|
self[k] = v unless self[k] == v
send("#{k}=", v) unless self[k] == v
end
self
end
Expand Down Expand Up @@ -651,7 +651,7 @@ def initialize_slots(slots) #:nodoc:
meta.each {|m| metas.add_meta(m) }
end

slots.except('meta').each {|name,value| self[name] = value }
slots.except('meta').each {|name,value| send("#{name}=", value) }

# now, when we have all slots initialized, we can run initialization callbacks
execute_callbacks :on_initialization
Expand Down
19 changes: 19 additions & 0 deletions spec/lib/strokedb/document/document_spec.rb
Expand Up @@ -90,6 +90,12 @@
@document.bbb.should == true
end

it "should pass batch update slots to matching slot= methods if any" do
@document.should_receive(:aaa=).with("aaa").once
@document.should_receive(:bbb=).with(true).once
@document.update_slots(:aaa => "aaa", :bbb => true)
end

it "should batch update slots but should not touch version/previous_version if update haven't changed document" do
@document = @document.update_slots!(:aaa => "aaa", :bbb => true).reload
lambda do
Expand Down Expand Up @@ -332,6 +338,19 @@
end

it_should_behave_like "Document"

describe "with #slot= method(s)" do
it "should pass matching slots to methods" do
Kernel.should_receive(:called_slot1=).with("val1")
my_document_class = Class.new(Document) do
def slot1=(v)
Kernel.send(:called_slot1=,v)
end
end
@document = my_document_class.new(:slot1 => "val1", :slot2 => "val2")

end
end

end

Expand Down

0 comments on commit feb7619

Please sign in to comment.