Added attributes= to casted model and extended doc
This commit is contained in:
parent
e48a6c8866
commit
704d0a09bd
|
@ -25,5 +25,17 @@ module CouchRest
|
||||||
def [] key
|
def [] key
|
||||||
super(key.to_s)
|
super(key.to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Sets the attributes from a hash
|
||||||
|
def update_attributes_without_saving(hash)
|
||||||
|
hash.each do |k, v|
|
||||||
|
raise NoMethodError, "#{k}= method not available, use property :#{k}" unless self.respond_to?("#{k}=")
|
||||||
|
end
|
||||||
|
hash.each do |k, v|
|
||||||
|
self.send("#{k}=",v)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
alias :attributes= :update_attributes_without_saving
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -113,6 +113,7 @@ module CouchRest
|
||||||
self.send("#{k}=",v)
|
self.send("#{k}=",v)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
alias :attributes= :update_attributes_without_saving
|
||||||
|
|
||||||
# Takes a hash as argument, and applies the values by using writer methods
|
# Takes a hash as argument, and applies the values by using writer methods
|
||||||
# for each key. Raises a NoMethodError if the corresponding methods are
|
# for each key. Raises a NoMethodError if the corresponding methods are
|
||||||
|
|
|
@ -4,6 +4,7 @@ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
|
||||||
require File.join(FIXTURE_PATH, 'more', 'card')
|
require File.join(FIXTURE_PATH, 'more', 'card')
|
||||||
require File.join(FIXTURE_PATH, 'more', 'cat')
|
require File.join(FIXTURE_PATH, 'more', 'cat')
|
||||||
require File.join(FIXTURE_PATH, 'more', 'person')
|
require File.join(FIXTURE_PATH, 'more', 'person')
|
||||||
|
require File.join(FIXTURE_PATH, 'more', 'question')
|
||||||
|
|
||||||
|
|
||||||
class WithCastedModelMixin < Hash
|
class WithCastedModelMixin < Hash
|
||||||
|
@ -106,7 +107,40 @@ describe CouchRest::CastedModel do
|
||||||
@obj.keywords.should be_an_instance_of(Array)
|
@obj.keywords.should be_an_instance_of(Array)
|
||||||
@obj.keywords.first.should == 'couch'
|
@obj.keywords.first.should == 'couch'
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "update attributes without saving" do
|
||||||
|
before(:each) do
|
||||||
|
@question = Question.new(:q => "What is your quest?", :a => "To seek the Holy Grail")
|
||||||
|
end
|
||||||
|
it "should work for attribute= methods" do
|
||||||
|
@question.q.should == "What is your quest?"
|
||||||
|
@question['a'].should == "To seek the Holy Grail"
|
||||||
|
@question.update_attributes_without_saving(:q => "What is your favorite color?", 'a' => "Blue")
|
||||||
|
@question['q'].should == "What is your favorite color?"
|
||||||
|
@question.a.should == "Blue"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should also work for attributes= alias" do
|
||||||
|
@question.respond_to?(:attributes=).should be_true
|
||||||
|
@question.attributes = {:q => "What is your favorite color?", 'a' => "Blue"}
|
||||||
|
@question['q'].should == "What is your favorite color?"
|
||||||
|
@question.a.should == "Blue"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should flip out if an attribute= method is missing" do
|
||||||
|
lambda {
|
||||||
|
@q.update_attributes_without_saving('foo' => "something", :a => "No green")
|
||||||
|
}.should raise_error(NoMethodError)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not change any attributes if there is an error" do
|
||||||
|
lambda {
|
||||||
|
@q.update_attributes_without_saving('foo' => "something", :a => "No green")
|
||||||
|
}.should raise_error(NoMethodError)
|
||||||
|
@question.q.should == "What is your quest?"
|
||||||
|
@question.a.should == "To seek the Holy Grail"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "saved document with casted models" do
|
describe "saved document with casted models" do
|
||||||
|
|
|
@ -109,6 +109,12 @@ describe "ExtendedDocument" do
|
||||||
@art['title'].should == "super danger"
|
@art['title'].should == "super danger"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should also work using attributes= alias" do
|
||||||
|
@art.respond_to?(:attributes=).should be_true
|
||||||
|
@art.attributes = {'date' => Time.now, :title => "something else"}
|
||||||
|
@art['title'].should == "something else"
|
||||||
|
end
|
||||||
|
|
||||||
it "should flip out if an attribute= method is missing" do
|
it "should flip out if an attribute= method is missing" do
|
||||||
lambda {
|
lambda {
|
||||||
@art.update_attributes_without_saving('slug' => "new-slug", :title => "super danger")
|
@art.update_attributes_without_saving('slug' => "new-slug", :title => "super danger")
|
||||||
|
|
Loading…
Reference in a new issue