updating to couchrest 1.1.2 and as_couch_json method

This commit is contained in:
Sam Lown 2011-07-19 21:28:44 +02:00
parent 9d724aee47
commit 3258ac22e9
4 changed files with 22 additions and 6 deletions

View file

@ -23,7 +23,7 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"] s.require_paths = ["lib"]
s.add_dependency(%q<couchrest>, "1.1.1") s.add_dependency(%q<couchrest>, "~> 1.1.2")
s.add_dependency(%q<mime-types>, "~> 1.15") s.add_dependency(%q<mime-types>, "~> 1.15")
s.add_dependency(%q<activemodel>, "~> 3.0") s.add_dependency(%q<activemodel>, "~> 3.0")
s.add_dependency(%q<tzinfo>, "~> 0.3.22") s.add_dependency(%q<tzinfo>, "~> 0.3.22")

View file

@ -1,5 +1,11 @@
# CouchRest Model Change History # CouchRest Model Change History
## 1.1.2 - 2011-07-XX
* Minor fixes
* Upgrade to couchrest 1.1.2
* Override as_couch_json to ensure nil values not stored
## 1.1.1 - 2011-07-04 ## 1.1.1 - 2011-07-04
* Minor fix * Minor fix

View file

@ -12,8 +12,10 @@ module CouchRest
raise "You can only mixin Properties in a class responding to [] and []=, if you tried to mixin CastedModel, make sure your class inherits from Hash or responds to the proper methods" unless (method_defined?(:[]) && method_defined?(:[]=)) raise "You can only mixin Properties in a class responding to [] and []=, if you tried to mixin CastedModel, make sure your class inherits from Hash or responds to the proper methods" unless (method_defined?(:[]) && method_defined?(:[]=))
end end
def as_json(options = nil) # Provide an attribute hash ready to be sent to CouchDB but with
Hash[self].reject{|k,v| v.nil?}.as_json(options) # all the nil attributes removed.
def as_couch_json
super.delete_if{|k,v| v.nil?}
end end
# Returns the Class properties with their values # Returns the Class properties with their values

View file

@ -62,15 +62,23 @@ describe CouchRest::Model::Property do
@card.updated_at.should_not be_nil @card.updated_at.should_not be_nil
end end
describe "#as_json" do describe "#as_couch_json" do
it "should provide a simple hash from model" do it "should provide a simple hash from model" do
@card.as_json.class.should eql(Hash) @card.as_couch_json.class.should eql(Hash)
end end
it "should remove properties from Hash if value is nil" do it "should remove properties from Hash if value is nil" do
@card.last_name = nil @card.last_name = nil
@card.as_json.keys.include?('last_name').should be_false @card.as_couch_json.keys.include?('last_name').should be_false
end
end
describe "#as_json" do
it "should provide a simple hash from model" do
@card.as_json.class.should eql(Hash)
end end
it "should pass options to Active Support's as_json" do it "should pass options to Active Support's as_json" do