couchrest-type
This commit is contained in:
parent
8cfed5af4f
commit
03f4169350
|
@ -23,7 +23,7 @@ module CouchRest
|
|||
# view_by :tags,
|
||||
# :map =>
|
||||
# "function(doc) {
|
||||
# if (doc.type == 'Article' && doc.tags) {
|
||||
# if (doc['couchrest-type'] == 'Article' && doc.tags) {
|
||||
# doc.tags.forEach(function(tag){
|
||||
# emit(tag, 1);
|
||||
# });
|
||||
|
@ -50,16 +50,12 @@ module CouchRest
|
|||
# instantiates the hash by converting all the keys to strings.
|
||||
def initialize keys = {}
|
||||
super()
|
||||
if self.class.default
|
||||
self.class.default.each do |k,v|
|
||||
self[k.to_s] = v
|
||||
end
|
||||
end
|
||||
apply_defaults
|
||||
keys.each do |k,v|
|
||||
self[k.to_s] = v
|
||||
end
|
||||
unless self['_id'] && self['_rev']
|
||||
init_doc
|
||||
self['couchrest-type'] = self.class.to_s
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -86,7 +82,8 @@ module CouchRest
|
|||
end
|
||||
|
||||
def cast field, opts = {}
|
||||
|
||||
@casts ||= {}
|
||||
@casts[field.to_s] = opts
|
||||
end
|
||||
|
||||
# Defines methods for reading and writing from fields in the document.
|
||||
|
@ -178,7 +175,7 @@ module CouchRest
|
|||
# view_by :tags,
|
||||
# :map =>
|
||||
# "function(doc) {
|
||||
# if (doc.type == 'Post' && doc.tags) {
|
||||
# if (doc['couchrest-type'] == 'Post' && doc.tags) {
|
||||
# doc.tags.forEach(function(tag){
|
||||
# emit(doc.tag, 1);
|
||||
# });
|
||||
|
@ -194,7 +191,7 @@ module CouchRest
|
|||
# function:
|
||||
#
|
||||
# function(doc) {
|
||||
# if (doc.type == 'Post' && doc.date) {
|
||||
# if (doc['couchrest-type'] == 'Post' && doc.date) {
|
||||
# emit(doc.date, null);
|
||||
# }
|
||||
# }
|
||||
|
@ -243,7 +240,7 @@ module CouchRest
|
|||
key_emit = doc_keys.length == 1 ? "#{doc_keys.first}" : "[#{doc_keys.join(', ')}]"
|
||||
map_function = <<-JAVASCRIPT
|
||||
function(doc) {
|
||||
if (doc.type == '#{type}' && #{key_protection}) {
|
||||
if (doc['couchrest-type'] == '#{type}' && #{key_protection}) {
|
||||
emit(#{key_emit}, null);
|
||||
}
|
||||
}
|
||||
|
@ -391,8 +388,12 @@ module CouchRest
|
|||
result['ok']
|
||||
end
|
||||
|
||||
def init_doc
|
||||
self['type'] = self.class.to_s
|
||||
def apply_defaults
|
||||
if self.class.default
|
||||
self.class.default.each do |k,v|
|
||||
self[k.to_s] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
include ::Extlib::Hook
|
||||
|
|
|
@ -14,6 +14,15 @@ class WithTemplate < CouchRest::Model
|
|||
key_accessor :preset
|
||||
end
|
||||
|
||||
class Question < CouchRest::Model
|
||||
key_accessor :q, :a
|
||||
end
|
||||
|
||||
class Course < CouchRest::Model
|
||||
key_accessor :title
|
||||
cast :questions, :as => [Question]
|
||||
end
|
||||
|
||||
class Article < CouchRest::Model
|
||||
use_database CouchRest.database!('http://localhost:5984/couchrest-model-test')
|
||||
unique_id :slug
|
||||
|
@ -24,7 +33,7 @@ class Article < CouchRest::Model
|
|||
view_by :tags,
|
||||
:map =>
|
||||
"function(doc) {
|
||||
if (doc.type == 'Article' && doc.tags) {
|
||||
if (doc['couchrest-type'] == 'Article' && doc.tags) {
|
||||
doc.tags.forEach(function(tag){
|
||||
emit(tag, 1);
|
||||
});
|
||||
|
@ -135,6 +144,37 @@ describe CouchRest::Model do
|
|||
end
|
||||
end
|
||||
|
||||
describe "getting a model with a subobjects array" do
|
||||
before(:all) do
|
||||
course_doc = {
|
||||
"title" => "Metaphysics 200",
|
||||
"questions" => [
|
||||
{
|
||||
"q" => "Carve the ___ of reality at the ___.",
|
||||
"a" => ["beast","joints"]
|
||||
},{
|
||||
"q" => "Who layed the smack down on Leibniz's Law?",
|
||||
"a" => "Willard Van Orman Quine"
|
||||
}
|
||||
]
|
||||
}
|
||||
r = Course.database.save course_doc
|
||||
@course = Course.get r['id']
|
||||
end
|
||||
it "should load the course" do
|
||||
@course.title.should == "Metaphysics 200"
|
||||
end
|
||||
it "should instantiate them as such" do
|
||||
@course["questions"][0].a[0].should == "beast"
|
||||
end
|
||||
end
|
||||
|
||||
describe "getting a model with a subobject field" do
|
||||
it "should instantiate it as such" do
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
describe "saving a model" do
|
||||
before(:all) do
|
||||
@obj = Basic.new
|
||||
|
@ -158,7 +198,7 @@ describe CouchRest::Model do
|
|||
end
|
||||
|
||||
it "should set the type" do
|
||||
@obj['type'].should == 'Basic'
|
||||
@obj['couchrest-type'].should == 'Basic'
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue