diff --git a/lib/couchrest.rb b/lib/couchrest.rb index 5c05b02..bec5ad9 100644 --- a/lib/couchrest.rb +++ b/lib/couchrest.rb @@ -28,6 +28,7 @@ require 'couchrest/monkeypatches' module CouchRest autoload :Server, 'couchrest/core/server' autoload :Database, 'couchrest/core/database' + autoload :Document, 'couchrest/core/document' autoload :Model, 'couchrest/core/model' autoload :Pager, 'couchrest/helper/pager' autoload :FileManager, 'couchrest/helper/file_manager' diff --git a/lib/couchrest/core/document.rb b/lib/couchrest/core/document.rb new file mode 100644 index 0000000..b076e97 --- /dev/null +++ b/lib/couchrest/core/document.rb @@ -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 \ No newline at end of file diff --git a/lib/couchrest/core/model.rb b/lib/couchrest/core/model.rb index 463eed5..e41a3d3 100644 --- a/lib/couchrest/core/model.rb +++ b/lib/couchrest/core/model.rb @@ -68,15 +68,12 @@ module CouchRest # # Article.by_tags :key => "ruby", :reduce => true # - class Model < Hash + class Model < Document # instantiates the hash by converting all the keys to strings. def initialize keys = {} - super() + super(keys) apply_defaults - keys.each do |k,v| - self[k.to_s] = v - end cast_keys unless self['_id'] && self['_rev'] 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 def has_view?(view) view = view.to_s - if generated_design_doc['views'][view] + generated_design_doc['views'][view] && generated_design_doc['views'][view]["couchrest-defaults"] - end end # 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 if self.class.default self.class.default.each do |k,v| - self[k.to_s] = v + self[k] = v end end end diff --git a/spec/couchrest/core/document_spec.rb b/spec/couchrest/core/document_spec.rb new file mode 100644 index 0000000..a1e7bda --- /dev/null +++ b/spec/couchrest/core/document_spec.rb @@ -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 \ No newline at end of file