From 1ef800afe41505fc80256f1a0f9deae54b385e99 Mon Sep 17 00:00:00 2001 From: Yurii Rashkovskii Date: Sun, 4 May 2008 11:01:30 +0300 Subject: [PATCH] Now Document.new and #update_slots are able to accept 'method-based' 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. --- lib/strokedb/document.rb | 4 ++-- spec/lib/strokedb/document/document_spec.rb | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/strokedb/document.rb b/lib/strokedb/document.rb index bbb5c359..06d5cfd0 100644 --- a/lib/strokedb/document.rb +++ b/lib/strokedb/document.rb @@ -432,7 +432,7 @@ def save!(perform_validation = true) # Updates slots with a specified hash 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 @@ -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 diff --git a/spec/lib/strokedb/document/document_spec.rb b/spec/lib/strokedb/document/document_spec.rb index 374a5fbc..69b91b27 100644 --- a/spec/lib/strokedb/document/document_spec.rb +++ b/spec/lib/strokedb/document/document_spec.rb @@ -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 @@ -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