I seem to have all the CR::Model specs passing
This commit is contained in:
parent
19a70ffd7d
commit
78534f8ec9
|
@ -17,7 +17,8 @@ module CouchRest
|
||||||
else
|
else
|
||||||
doc_keys = keys.collect{|k|"doc['#{k}']"} # this is where :require => 'doc.x == true' would show up
|
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(', ')}]"
|
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
|
map_function = <<-JAVASCRIPT
|
||||||
function(doc) {
|
function(doc) {
|
||||||
if (#{guards.join(' && ')}) {
|
if (#{guards.join(' && ')}) {
|
||||||
|
@ -28,19 +29,10 @@ module CouchRest
|
||||||
self['views'][method_name] = {
|
self['views'][method_name] = {
|
||||||
'map' => map_function
|
'map' => map_function
|
||||||
}
|
}
|
||||||
self['views'][method_name]['couchrest-defaults'] = opts
|
end
|
||||||
|
self['views'][method_name]['couchrest-defaults'] = opts unless opts.empty?
|
||||||
method_name
|
method_name
|
||||||
end
|
end
|
||||||
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.
|
# Dispatches to any named view.
|
||||||
def view view_name, query={}, &block
|
def view view_name, query={}, &block
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
require 'extlib'
|
require 'extlib'
|
||||||
require 'digest/md5'
|
require 'digest/md5'
|
||||||
|
require File.dirname(__FILE__) + '/document'
|
||||||
# = CouchRest::Model - ORM, the CouchDB way
|
# = CouchRest::Model - ORM, the CouchDB way
|
||||||
module CouchRest
|
module CouchRest
|
||||||
# = CouchRest::Model - ORM, the CouchDB way
|
# = CouchRest::Model - ORM, the CouchDB way
|
||||||
|
@ -111,15 +111,16 @@ module CouchRest
|
||||||
# Load all documents that have the "couchrest-type" field equal to the
|
# Load all documents that have the "couchrest-type" field equal to the
|
||||||
# name of the current class. Take thes the standard set of
|
# name of the current class. Take thes the standard set of
|
||||||
# CouchRest::Database#view options.
|
# CouchRest::Database#view options.
|
||||||
# def all opts = {}
|
def all opts = {}, &block
|
||||||
# self.generated_design_doc ||= default_design_doc
|
self.design_doc ||= Design.new(default_design_doc)
|
||||||
# unless design_doc_fresh
|
unless design_doc_fresh
|
||||||
# refresh_design_doc
|
refresh_design_doc
|
||||||
# end
|
end
|
||||||
# view_name = "#{design_doc_slug}/all"
|
# view_name = "#{design_doc_slug}/all"
|
||||||
# raw = opts.delete(:raw)
|
# raw = opts.delete(:raw)
|
||||||
# fetch_view_with_docs(view_name, opts, raw)
|
# fetch_view_with_docs(view_name, opts, raw)
|
||||||
# end
|
view :all, opts, &block
|
||||||
|
end
|
||||||
|
|
||||||
# Cast a field as another class. The class must be happy to have the
|
# 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
|
# field's primitive type as the argument to it's constucture. Classes
|
||||||
|
@ -266,7 +267,14 @@ module CouchRest
|
||||||
|
|
||||||
def view_by *keys
|
def view_by *keys
|
||||||
self.design_doc ||= Design.new(default_design_doc)
|
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.view_by(*keys)
|
||||||
|
self.design_doc_fresh = false
|
||||||
end
|
end
|
||||||
|
|
||||||
# def view_by *keys
|
# 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
|
# 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
|
||||||
design_doc['views'][view]
|
design_doc && design_doc['views'] && design_doc['views'][view]
|
||||||
end
|
end
|
||||||
|
|
||||||
# # Fetch the generated design doc. Could raise an error if the generated
|
# # Fetch the generated design doc. Could raise an error if the generated
|
||||||
|
@ -397,21 +405,22 @@ module CouchRest
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
# def refresh_design_doc
|
def refresh_design_doc
|
||||||
# did = "_design/#{design_doc_slug}"
|
did = "_design/#{design_doc_slug}"
|
||||||
# saved = database.get(did) rescue nil
|
saved = database.get(did) rescue nil
|
||||||
# if saved
|
if saved
|
||||||
# design_doc['views'].each do |name, view|
|
design_doc['views'].each do |name, view|
|
||||||
# saved['views'][name] = view
|
saved['views'][name] = view
|
||||||
# end
|
end
|
||||||
# database.save(saved)
|
database.save(saved)
|
||||||
# else
|
else
|
||||||
# design_doc['_id'] = did
|
design_doc['_id'] = did
|
||||||
# design_doc.database = database
|
design_doc.delete('_rev')
|
||||||
# design_doc.save
|
design_doc.database = database
|
||||||
# end
|
design_doc.save
|
||||||
# self.design_doc_fresh = true
|
end
|
||||||
# end
|
self.design_doc_fresh = true
|
||||||
|
end
|
||||||
|
|
||||||
end # class << self
|
end # class << self
|
||||||
|
|
||||||
|
@ -433,6 +442,34 @@ module CouchRest
|
||||||
save
|
save
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def apply_defaults
|
def apply_defaults
|
||||||
|
@ -461,6 +498,8 @@ module CouchRest
|
||||||
end
|
end
|
||||||
|
|
||||||
include ::Extlib::Hook
|
include ::Extlib::Hook
|
||||||
|
# todo: drop create and update hooks...
|
||||||
|
# (use new_record? in callbacks if you care)
|
||||||
register_instance_hooks :save, :create, :update, :destroy
|
register_instance_hooks :save, :create, :update, :destroy
|
||||||
|
|
||||||
end # class Model
|
end # class Model
|
||||||
|
|
|
@ -119,7 +119,8 @@ describe CouchRest::Design do
|
||||||
method = @des.view_by :name, :age
|
method = @des.view_by :name, :age
|
||||||
@des.database = @db
|
@des.database = @db
|
||||||
@des.save
|
@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
|
end
|
||||||
it "should work" do
|
it "should work" do
|
||||||
res = @des.view :by_name_and_age
|
res = @des.view :by_name_and_age
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
require File.dirname(__FILE__) + '/../../spec_helper'
|
require File.dirname(__FILE__) + '/../../spec_helper'
|
||||||
__END__
|
|
||||||
class Basic < CouchRest::Model
|
class Basic < CouchRest::Model
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -34,8 +34,6 @@ class Course < CouchRest::Model
|
||||||
view_by :dept, :ducktype => true
|
view_by :dept, :ducktype => true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Article < CouchRest::Model
|
class Article < CouchRest::Model
|
||||||
use_database CouchRest.database!('http://localhost:5984/couchrest-model-test')
|
use_database CouchRest.database!('http://localhost:5984/couchrest-model-test')
|
||||||
unique_id :slug
|
unique_id :slug
|
||||||
|
@ -216,24 +214,24 @@ describe CouchRest::Model do
|
||||||
@course["questions"][0].a[0].should == "beast"
|
@course["questions"][0].a[0].should == "beast"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
#
|
|
||||||
# describe "finding all instances of a model" do
|
describe "finding all instances of a model" do
|
||||||
# before(:all) do
|
before(:all) do
|
||||||
# WithTemplate.new('important-field' => '1').save
|
WithTemplate.new('important-field' => '1').save
|
||||||
# WithTemplate.new('important-field' => '2').save
|
WithTemplate.new('important-field' => '2').save
|
||||||
# WithTemplate.new('important-field' => '3').save
|
WithTemplate.new('important-field' => '3').save
|
||||||
# WithTemplate.new('important-field' => '4').save
|
WithTemplate.new('important-field' => '4').save
|
||||||
# end
|
end
|
||||||
# it "should make the design doc" do
|
it "should make the design doc" do
|
||||||
# WithTemplate.all
|
WithTemplate.all
|
||||||
# d = WithTemplate.design_doc
|
d = WithTemplate.design_doc
|
||||||
# d['views']['all']['map'].should include('WithTemplate')
|
d['views']['all']['map'].should include('WithTemplate')
|
||||||
# end
|
end
|
||||||
# it "should find all" do
|
it "should find all" do
|
||||||
# rs = WithTemplate.all
|
rs = WithTemplate.all
|
||||||
# rs.length.should == 4
|
rs.length.should == 4
|
||||||
# end
|
end
|
||||||
# end
|
end
|
||||||
|
|
||||||
describe "getting a model with a subobject field" do
|
describe "getting a model with a subobject field" do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
|
@ -288,6 +286,11 @@ describe CouchRest::Model do
|
||||||
Article.database.delete(@old) if @old
|
Article.database.delete(@old) if @old
|
||||||
end
|
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
|
it "should require the title" do
|
||||||
lambda{@art.save}.should raise_error
|
lambda{@art.save}.should raise_error
|
||||||
@art.title = 'This is the title'
|
@art.title = 'This is the title'
|
||||||
|
@ -409,10 +412,10 @@ describe CouchRest::Model do
|
||||||
view['rows'].length.should == 4
|
view['rows'].length.should == 4
|
||||||
end
|
end
|
||||||
|
|
||||||
# it "should return the matching objects (with default argument :descending => true)" do
|
it "should return the matching objects (with default argument :descending => true)" do
|
||||||
# articles = Article.by_date
|
articles = Article.by_date
|
||||||
# articles.collect{|a|a.title}.should == @titles.reverse
|
articles.collect{|a|a.title}.should == @titles.reverse
|
||||||
# end
|
end
|
||||||
|
|
||||||
it "should allow you to override default args" do
|
it "should allow you to override default args" do
|
||||||
articles = Article.by_date :descending => false
|
articles = Article.by_date :descending => false
|
||||||
|
@ -433,25 +436,25 @@ describe CouchRest::Model do
|
||||||
doc = Course.design_doc
|
doc = Course.design_doc
|
||||||
doc['views']['all']['map'].should include('Course')
|
doc['views']['all']['map'].should include('Course')
|
||||||
end
|
end
|
||||||
# it "should can query via view" do
|
it "should can query via view" do
|
||||||
# # register methods with method-missing, for local dispatch. method
|
# register methods with method-missing, for local dispatch. method
|
||||||
# # missing lookup table, no heuristics.
|
# missing lookup table, no heuristics.
|
||||||
# view = Course.view :by_title
|
view = Course.view :by_title
|
||||||
# designed = Course.by_title
|
designed = Course.by_title
|
||||||
# view.should == designed
|
view.should == designed
|
||||||
# end
|
end
|
||||||
# it "should get them" do
|
it "should get them" do
|
||||||
# rs = Course.by_title
|
rs = Course.by_title
|
||||||
# rs.length.should == 4
|
rs.length.should == 4
|
||||||
# end
|
end
|
||||||
# it "should yield" do
|
it "should yield" do
|
||||||
# courses = []
|
courses = []
|
||||||
# rs = Course.by_title # remove me
|
rs = Course.by_title # remove me
|
||||||
# Course.view(:by_title) do |course|
|
Course.view(:by_title) do |course|
|
||||||
# courses << course
|
courses << course
|
||||||
# end
|
end
|
||||||
# courses[0]["doc"]["title"].should =='aaa'
|
courses[0]["doc"]["title"].should =='aaa'
|
||||||
# end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -474,9 +477,6 @@ describe CouchRest::Model do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
|
||||||
__END__
|
|
||||||
|
|
||||||
describe "a model with a compound key view" do
|
describe "a model with a compound key view" do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
written_at = Time.now - 24 * 3600 * 7
|
written_at = Time.now - 24 * 3600 * 7
|
||||||
|
@ -553,6 +553,8 @@ __END__
|
||||||
Article.by_updated_at
|
Article.by_updated_at
|
||||||
newdocs = Article.database.documents :startkey => "_design/",
|
newdocs = Article.database.documents :startkey => "_design/",
|
||||||
:endkey => "_design/\u9999"
|
:endkey => "_design/\u9999"
|
||||||
|
# puts @design_docs.inspect
|
||||||
|
# puts newdocs.inspect
|
||||||
newdocs["rows"].length.should == @design_docs["rows"].length + 1
|
newdocs["rows"].length.should == @design_docs["rows"].length + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue