factor out couchrest::document
This commit is contained in:
parent
a6f285adfa
commit
0ce716518f
|
@ -28,6 +28,7 @@ require 'couchrest/monkeypatches'
|
||||||
module CouchRest
|
module CouchRest
|
||||||
autoload :Server, 'couchrest/core/server'
|
autoload :Server, 'couchrest/core/server'
|
||||||
autoload :Database, 'couchrest/core/database'
|
autoload :Database, 'couchrest/core/database'
|
||||||
|
autoload :Document, 'couchrest/core/document'
|
||||||
autoload :Model, 'couchrest/core/model'
|
autoload :Model, 'couchrest/core/model'
|
||||||
autoload :Pager, 'couchrest/helper/pager'
|
autoload :Pager, 'couchrest/helper/pager'
|
||||||
autoload :FileManager, 'couchrest/helper/file_manager'
|
autoload :FileManager, 'couchrest/helper/file_manager'
|
||||||
|
|
25
lib/couchrest/core/document.rb
Normal file
25
lib/couchrest/core/document.rb
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
module CouchRest
|
||||||
|
class Response < Hash
|
||||||
|
def initialize keys = {}
|
||||||
|
keys.each do |k,v|
|
||||||
|
self[k.to_s] = v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
def []= key, value
|
||||||
|
super(key.to_s, value)
|
||||||
|
end
|
||||||
|
def [] key
|
||||||
|
super(key.to_s)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Document < Response
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
class Design < Document
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
end
|
|
@ -68,15 +68,12 @@ module CouchRest
|
||||||
#
|
#
|
||||||
# Article.by_tags :key => "ruby", :reduce => true
|
# Article.by_tags :key => "ruby", :reduce => true
|
||||||
#
|
#
|
||||||
class Model < Hash
|
class Model < Document
|
||||||
|
|
||||||
# instantiates the hash by converting all the keys to strings.
|
# instantiates the hash by converting all the keys to strings.
|
||||||
def initialize keys = {}
|
def initialize keys = {}
|
||||||
super()
|
super(keys)
|
||||||
apply_defaults
|
apply_defaults
|
||||||
keys.each do |k,v|
|
|
||||||
self[k.to_s] = v
|
|
||||||
end
|
|
||||||
cast_keys
|
cast_keys
|
||||||
unless self['_id'] && self['_rev']
|
unless self['_id'] && self['_rev']
|
||||||
self['couchrest-type'] = self.class.to_s
|
self['couchrest-type'] = self.class.to_s
|
||||||
|
@ -314,9 +311,8 @@ module CouchRest
|
||||||
# returns stored defaults if the there is a view named this in the design doc
|
# returns stored defaults if the there is a view named this in the design doc
|
||||||
def has_view?(view)
|
def has_view?(view)
|
||||||
view = view.to_s
|
view = view.to_s
|
||||||
if generated_design_doc['views'][view]
|
generated_design_doc['views'][view] &&
|
||||||
generated_design_doc['views'][view]["couchrest-defaults"]
|
generated_design_doc['views'][view]["couchrest-defaults"]
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Fetch the generated design doc. Could raise an error if the generated views have not been queried yet.
|
# Fetch the generated design doc. Could raise an error if the generated views have not been queried yet.
|
||||||
|
@ -495,7 +491,7 @@ module CouchRest
|
||||||
def apply_defaults
|
def apply_defaults
|
||||||
if self.class.default
|
if self.class.default
|
||||||
self.class.default.each do |k,v|
|
self.class.default.each do |k,v|
|
||||||
self[k.to_s] = v
|
self[k] = v
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
29
spec/couchrest/core/document_spec.rb
Normal file
29
spec/couchrest/core/document_spec.rb
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
require File.dirname(__FILE__) + '/../../spec_helper'
|
||||||
|
|
||||||
|
describe CouchRest::Document, "[]=" do
|
||||||
|
before(:each) do
|
||||||
|
@doc = CouchRest::Document.new
|
||||||
|
end
|
||||||
|
it "should work" do
|
||||||
|
@doc["enamel"].should == nil
|
||||||
|
@doc["enamel"] = "Strong"
|
||||||
|
@doc["enamel"].should == "Strong"
|
||||||
|
end
|
||||||
|
it "[]= should convert to string" do
|
||||||
|
@doc["enamel"].should == nil
|
||||||
|
@doc[:enamel] = "Strong"
|
||||||
|
@doc["enamel"].should == "Strong"
|
||||||
|
end
|
||||||
|
it "should read as a string" do
|
||||||
|
@doc[:enamel] = "Strong"
|
||||||
|
@doc[:enamel].should == "Strong"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe CouchRest::Document, "new" do
|
||||||
|
it "should create itself from a Hash" do
|
||||||
|
@doc = CouchRest::Document.new("key" => [1,2,3], :more => "values")
|
||||||
|
@doc["key"].should == [1,2,3]
|
||||||
|
@doc["more"].should == "values"
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue