andrewtimberlake-couch_potato 0.2.8.3 → 0.2.8.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/core_ext/time.rb +1 -1
- data/lib/couch_potato/database.rb +17 -16
- data/spec/unit/database_spec.rb +13 -1
- metadata +1 -1
data/lib/core_ext/time.rb
CHANGED
@@ -1,24 +1,24 @@
|
|
1
1
|
module CouchPotato
|
2
2
|
class Database
|
3
|
-
|
3
|
+
|
4
4
|
class ValidationsFailedError < ::StandardError; end
|
5
|
-
|
5
|
+
|
6
6
|
def initialize(couchrest_database)
|
7
7
|
@database = couchrest_database
|
8
8
|
begin
|
9
|
-
couchrest_database.info
|
9
|
+
couchrest_database.info
|
10
10
|
rescue RestClient::ResourceNotFound
|
11
11
|
raise "Database '#{couchrest_database.name}' does not exist."
|
12
12
|
end
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
def view(spec)
|
16
16
|
results = CouchPotato::View::ViewQuery.new(database,
|
17
17
|
spec.design_document, spec.view_name, spec.map_function,
|
18
18
|
spec.reduce_function).query_view!(spec.view_parameters)
|
19
19
|
spec.process_results results
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
def save_document(document)
|
23
23
|
return true unless document.dirty?
|
24
24
|
if document.new?
|
@@ -28,12 +28,12 @@ module CouchPotato
|
|
28
28
|
end
|
29
29
|
end
|
30
30
|
alias_method :save, :save_document
|
31
|
-
|
31
|
+
|
32
32
|
def save_document!(document)
|
33
33
|
save_document(document) || raise(ValidationsFailedError.new(document.errors.full_messages))
|
34
34
|
end
|
35
35
|
alias_method :save!, :save_document!
|
36
|
-
|
36
|
+
|
37
37
|
def destroy_document(document)
|
38
38
|
document.run_callbacks :before_destroy
|
39
39
|
document._deleted = true
|
@@ -43,12 +43,13 @@ module CouchPotato
|
|
43
43
|
document._rev = nil
|
44
44
|
end
|
45
45
|
alias_method :destroy, :destroy_document
|
46
|
-
|
46
|
+
|
47
47
|
def load_document(id)
|
48
48
|
raise "Can't load a document without an id (got nil)" if id.nil?
|
49
49
|
begin
|
50
50
|
json = database.get(id)
|
51
|
-
|
51
|
+
klass = json['ruby_class'].split('::').inject(Class){|scope, const| scope.const_get(const)}
|
52
|
+
instance = klass.json_create json
|
52
53
|
instance.database = self
|
53
54
|
instance
|
54
55
|
rescue(RestClient::ResourceNotFound)
|
@@ -56,11 +57,11 @@ module CouchPotato
|
|
56
57
|
end
|
57
58
|
end
|
58
59
|
alias_method :load, :load_document
|
59
|
-
|
60
|
+
|
60
61
|
def inspect
|
61
62
|
"#<CouchPotato::Database>"
|
62
63
|
end
|
63
|
-
|
64
|
+
|
64
65
|
private
|
65
66
|
|
66
67
|
def clean_hash(hash)
|
@@ -68,7 +69,7 @@ module CouchPotato
|
|
68
69
|
hash.delete k unless v
|
69
70
|
end
|
70
71
|
end
|
71
|
-
|
72
|
+
|
72
73
|
def create_document(document)
|
73
74
|
document.database = self
|
74
75
|
document.run_callbacks :before_validation_on_save
|
@@ -83,7 +84,7 @@ module CouchPotato
|
|
83
84
|
document.run_callbacks :after_create
|
84
85
|
true
|
85
86
|
end
|
86
|
-
|
87
|
+
|
87
88
|
def update_document(document)
|
88
89
|
document.run_callbacks :before_validation_on_save
|
89
90
|
document.run_callbacks :before_validation_on_update
|
@@ -96,10 +97,10 @@ module CouchPotato
|
|
96
97
|
document.run_callbacks :after_update
|
97
98
|
true
|
98
99
|
end
|
99
|
-
|
100
|
+
|
100
101
|
def database
|
101
102
|
@database
|
102
103
|
end
|
103
|
-
|
104
|
+
|
104
105
|
end
|
105
|
-
end
|
106
|
+
end
|
data/spec/unit/database_spec.rb
CHANGED
@@ -3,6 +3,13 @@ require File.dirname(__FILE__) + '/../spec_helper'
|
|
3
3
|
class DbTestUser
|
4
4
|
end
|
5
5
|
|
6
|
+
# namespaced model
|
7
|
+
module Parent
|
8
|
+
class Child
|
9
|
+
include CouchPotato::Persistence
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
6
13
|
describe CouchPotato::Database, 'new' do
|
7
14
|
it "should raise an exception if the database doesn't exist" do
|
8
15
|
lambda {
|
@@ -18,7 +25,7 @@ describe CouchPotato::Database, 'load' do
|
|
18
25
|
db.load nil
|
19
26
|
}.should raise_error("Can't load a document without an id (got nil)")
|
20
27
|
end
|
21
|
-
|
28
|
+
|
22
29
|
it "should set itself on the model" do
|
23
30
|
user = mock 'user'
|
24
31
|
DbTestUser.stub!(:new).and_return(user)
|
@@ -26,6 +33,11 @@ describe CouchPotato::Database, 'load' do
|
|
26
33
|
user.should_receive(:database=).with(db)
|
27
34
|
db.load '1'
|
28
35
|
end
|
36
|
+
|
37
|
+
it "should load namespaced models" do
|
38
|
+
db = CouchPotato::Database.new(stub('couchrest db', :info => nil, :get => {'ruby_class' => 'Parent::Child'}))
|
39
|
+
db.load('1').class.should == Parent::Child
|
40
|
+
end
|
29
41
|
end
|
30
42
|
|
31
43
|
describe CouchPotato::Database, 'save_document' do
|