From 78534f8ec921e8eef853f6e53377a1863ce550ca Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Sat, 22 Nov 2008 14:15:07 -0800 Subject: [PATCH] I seem to have all the CR::Model specs passing --- lib/couchrest/core/design.rb | 30 ++++------ lib/couchrest/core/model.rb | 91 ++++++++++++++++++++-------- spec/couchrest/core/design_spec.rb | 3 +- spec/couchrest/core/model_spec.rb | 96 +++++++++++++++--------------- 4 files changed, 127 insertions(+), 93 deletions(-) diff --git a/lib/couchrest/core/design.rb b/lib/couchrest/core/design.rb index e284a6c..f9b8e22 100644 --- a/lib/couchrest/core/design.rb +++ b/lib/couchrest/core/design.rb @@ -17,31 +17,23 @@ module CouchRest else doc_keys = keys.collect{|k|"doc['#{k}']"} # this is where :require => 'doc.x == true' would show up key_emit = doc_keys.length == 1 ? "#{doc_keys.first}" : "[#{doc_keys.join(', ')}]" - guards = doc_keys + guards = opts.delete(:guards) || [] + guards.concat doc_keys map_function = <<-JAVASCRIPT - function(doc) { - if (#{guards.join(' && ')}) { - emit(#{key_emit}, null); - } - } - JAVASCRIPT +function(doc) { + if (#{guards.join(' && ')}) { + emit(#{key_emit}, null); + } +} +JAVASCRIPT self['views'][method_name] = { 'map' => map_function } - self['views'][method_name]['couchrest-defaults'] = opts - method_name end + self['views'][method_name]['couchrest-defaults'] = opts unless opts.empty? + method_name end - - # def method_missing m, *args - # if opts = has_view?(m) - # query = args.shift || {} - # view(m, opts.merge(query), *args) - # else - # super - # end - # end - + # Dispatches to any named view. def view view_name, query={}, &block view_name = view_name.to_s diff --git a/lib/couchrest/core/model.rb b/lib/couchrest/core/model.rb index 3d79d2c..4ac2da3 100644 --- a/lib/couchrest/core/model.rb +++ b/lib/couchrest/core/model.rb @@ -1,7 +1,7 @@ require 'rubygems' require 'extlib' require 'digest/md5' - +require File.dirname(__FILE__) + '/document' # = CouchRest::Model - ORM, the CouchDB way module CouchRest # = CouchRest::Model - ORM, the CouchDB way @@ -111,15 +111,16 @@ module CouchRest # Load all documents that have the "couchrest-type" field equal to the # name of the current class. Take thes the standard set of # CouchRest::Database#view options. - # def all opts = {} - # self.generated_design_doc ||= default_design_doc - # unless design_doc_fresh - # refresh_design_doc - # end - # view_name = "#{design_doc_slug}/all" - # raw = opts.delete(:raw) - # fetch_view_with_docs(view_name, opts, raw) - # end + def all opts = {}, &block + self.design_doc ||= Design.new(default_design_doc) + unless design_doc_fresh + refresh_design_doc + end + # view_name = "#{design_doc_slug}/all" + # raw = opts.delete(:raw) + # fetch_view_with_docs(view_name, opts, raw) + view :all, opts, &block + end # Cast a field as another class. The class must be happy to have the # field's primitive type as the argument to it's constucture. Classes @@ -266,7 +267,14 @@ module CouchRest def view_by *keys self.design_doc ||= Design.new(default_design_doc) + opts = keys.pop if keys.last.is_a?(Hash) + opts ||= {} + ducktype = opts.delete(:ducktype) + # if ducktype + # end + keys.push opts self.design_doc.view_by(*keys) + self.design_doc_fresh = false end # def view_by *keys @@ -319,7 +327,7 @@ 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 - design_doc['views'][view] + design_doc && design_doc['views'] && design_doc['views'][view] end # # Fetch the generated design doc. Could raise an error if the generated @@ -397,21 +405,22 @@ module CouchRest } end - # def refresh_design_doc - # did = "_design/#{design_doc_slug}" - # saved = database.get(did) rescue nil - # if saved - # design_doc['views'].each do |name, view| - # saved['views'][name] = view - # end - # database.save(saved) - # else - # design_doc['_id'] = did - # design_doc.database = database - # design_doc.save - # end - # self.design_doc_fresh = true - # end + def refresh_design_doc + did = "_design/#{design_doc_slug}" + saved = database.get(did) rescue nil + if saved + design_doc['views'].each do |name, view| + saved['views'][name] = view + end + database.save(saved) + else + design_doc['_id'] = did + design_doc.delete('_rev') + design_doc.database = database + design_doc.save + end + self.design_doc_fresh = true + end end # class << self @@ -433,6 +442,34 @@ module CouchRest save end + # for compatibility with old-school frameworks + alias :new_record? :new_document? + + # We override this to create the create and update callback opportunities. + # I think we should drop those and just have save. If you care, in your callback, + # check new_document? + def save actually=false + if actually + super() + else + if new_document? + create + else + update + end + end + end + + def update + save :actually + end + + def create + # can we use the callbacks for this? + set_unique_id if self.respond_to?(:set_unique_id) + save :actually + end + private def apply_defaults @@ -461,6 +498,8 @@ module CouchRest end include ::Extlib::Hook + # todo: drop create and update hooks... + # (use new_record? in callbacks if you care) register_instance_hooks :save, :create, :update, :destroy end # class Model diff --git a/spec/couchrest/core/design_spec.rb b/spec/couchrest/core/design_spec.rb index bbe2f2e..c408383 100644 --- a/spec/couchrest/core/design_spec.rb +++ b/spec/couchrest/core/design_spec.rb @@ -119,7 +119,8 @@ describe CouchRest::Design do method = @des.view_by :name, :age @des.database = @db @des.save - @db.bulk_save([{"name" => "a", "age" => 2},{"name" => "a", "age" => 4},{"name" => "z", "age" => 9}]) + @db.bulk_save([{"name" => "a", "age" => 2}, + {"name" => "a", "age" => 4},{"name" => "z", "age" => 9}]) end it "should work" do res = @des.view :by_name_and_age diff --git a/spec/couchrest/core/model_spec.rb b/spec/couchrest/core/model_spec.rb index 8b5c1e0..20ddbbf 100644 --- a/spec/couchrest/core/model_spec.rb +++ b/spec/couchrest/core/model_spec.rb @@ -1,5 +1,5 @@ require File.dirname(__FILE__) + '/../../spec_helper' -__END__ + class Basic < CouchRest::Model end @@ -34,8 +34,6 @@ class Course < CouchRest::Model view_by :dept, :ducktype => true end - - class Article < CouchRest::Model use_database CouchRest.database!('http://localhost:5984/couchrest-model-test') unique_id :slug @@ -216,24 +214,24 @@ describe CouchRest::Model do @course["questions"][0].a[0].should == "beast" end end - # - # describe "finding all instances of a model" do - # before(:all) do - # WithTemplate.new('important-field' => '1').save - # WithTemplate.new('important-field' => '2').save - # WithTemplate.new('important-field' => '3').save - # WithTemplate.new('important-field' => '4').save - # end - # it "should make the design doc" do - # WithTemplate.all - # d = WithTemplate.design_doc - # d['views']['all']['map'].should include('WithTemplate') - # end - # it "should find all" do - # rs = WithTemplate.all - # rs.length.should == 4 - # end - # end + + describe "finding all instances of a model" do + before(:all) do + WithTemplate.new('important-field' => '1').save + WithTemplate.new('important-field' => '2').save + WithTemplate.new('important-field' => '3').save + WithTemplate.new('important-field' => '4').save + end + it "should make the design doc" do + WithTemplate.all + d = WithTemplate.design_doc + d['views']['all']['map'].should include('WithTemplate') + end + it "should find all" do + rs = WithTemplate.all + rs.length.should == 4 + end + end describe "getting a model with a subobject field" do before(:all) do @@ -288,6 +286,11 @@ describe CouchRest::Model do Article.database.delete(@old) if @old end + it "should be a new document" do + @art.should be_a_new_document + @art.title.should be_nil + end + it "should require the title" do lambda{@art.save}.should raise_error @art.title = 'This is the title' @@ -409,10 +412,10 @@ describe CouchRest::Model do view['rows'].length.should == 4 end - # it "should return the matching objects (with default argument :descending => true)" do - # articles = Article.by_date - # articles.collect{|a|a.title}.should == @titles.reverse - # end + it "should return the matching objects (with default argument :descending => true)" do + articles = Article.by_date + articles.collect{|a|a.title}.should == @titles.reverse + end it "should allow you to override default args" do articles = Article.by_date :descending => false @@ -433,25 +436,25 @@ describe CouchRest::Model do doc = Course.design_doc doc['views']['all']['map'].should include('Course') end - # it "should can query via view" do - # # register methods with method-missing, for local dispatch. method - # # missing lookup table, no heuristics. - # view = Course.view :by_title - # designed = Course.by_title - # view.should == designed - # end - # it "should get them" do - # rs = Course.by_title - # rs.length.should == 4 - # end - # it "should yield" do - # courses = [] - # rs = Course.by_title # remove me - # Course.view(:by_title) do |course| - # courses << course - # end - # courses[0]["doc"]["title"].should =='aaa' - # end + it "should can query via view" do + # register methods with method-missing, for local dispatch. method + # missing lookup table, no heuristics. + view = Course.view :by_title + designed = Course.by_title + view.should == designed + end + it "should get them" do + rs = Course.by_title + rs.length.should == 4 + end + it "should yield" do + courses = [] + rs = Course.by_title # remove me + Course.view(:by_title) do |course| + courses << course + end + courses[0]["doc"]["title"].should =='aaa' + end end @@ -473,9 +476,6 @@ describe CouchRest::Model do @as[0]['_id'].should == @id end end - -end -__END__ describe "a model with a compound key view" do before(:all) do @@ -553,6 +553,8 @@ __END__ Article.by_updated_at newdocs = Article.database.documents :startkey => "_design/", :endkey => "_design/\u9999" + # puts @design_docs.inspect + # puts newdocs.inspect newdocs["rows"].length.should == @design_docs["rows"].length + 1 end end