langalex-couch_potato 0.2.7 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +5 -3
- data/VERSION.yml +2 -2
- data/lib/core_ext/string.rb +8 -4
- data/lib/core_ext/symbol.rb +15 -0
- data/lib/couch_potato/persistence/callbacks.rb +3 -2
- data/lib/couch_potato/persistence/json.rb +1 -0
- data/lib/couch_potato/persistence/properties.rb +1 -0
- data/lib/couch_potato/persistence/validation.rb +18 -0
- data/lib/couch_potato/persistence.rb +3 -1
- data/lib/couch_potato.rb +0 -2
- data/spec/callbacks_spec.rb +19 -1
- data/spec/fixtures/address.rb +9 -0
- data/spec/fixtures/person.rb +6 -0
- data/spec/property_spec.rb +19 -0
- data/spec/unit/callbacks_spec.rb +33 -0
- metadata +10 -2
data/README.md
CHANGED
@@ -228,12 +228,14 @@ Couch Potato supports the usual lifecycle callbacks known from ActiveRecord:
|
|
228
228
|
include CouchPotato::Persistence
|
229
229
|
|
230
230
|
before_create :do_something_before_create
|
231
|
-
before_update {|user
|
231
|
+
before_update {|user| user.do_something_on_update}
|
232
232
|
end
|
233
233
|
|
234
|
-
This will call the method do_something_before_create before creating an object and run the given lambda before updating one. Lambda callbacks get passed the model as their first argument.
|
234
|
+
This will call the method do_something_before_create before creating an object and run the given lambda before updating one. Lambda callbacks get passed the model as their first argument. Method callbacks don't receive any arguments.
|
235
235
|
|
236
|
-
Supported callbacks are: :before_validation_on_create, :before_validation_on_update, :before_validation_on_save, :before_create, :after_create, :before_update, :after_update, :before_save, :after_save, :before_destroy, :after_destroy.
|
236
|
+
Supported callbacks are: :before_validation, :before_validation_on_create, :before_validation_on_update, :before_validation_on_save, :before_create, :after_create, :before_update, :after_update, :before_save, :after_save, :before_destroy, :after_destroy.
|
237
|
+
|
238
|
+
If you need access to the database in a callback: Couch Potato automatically assigns a database instance to the model before saving and when loading. It is available as _database_ accessor from within your model instance.
|
237
239
|
|
238
240
|
#### Testing
|
239
241
|
|
data/VERSION.yml
CHANGED
data/lib/core_ext/string.rb
CHANGED
@@ -5,11 +5,15 @@ module ActiveSupportMethods
|
|
5
5
|
end
|
6
6
|
end
|
7
7
|
|
8
|
+
# Source
|
9
|
+
# http://github.com/rails/rails/blob/b600bf2cd728c90d50cc34456c944b2dfefe8c8d/activesupport/lib/active_support/inflector.rb
|
8
10
|
def underscore
|
9
|
-
gsub(/
|
10
|
-
'
|
11
|
-
|
11
|
+
gsub(/::/, '/').
|
12
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
13
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
14
|
+
tr("-", "_").
|
15
|
+
downcase
|
12
16
|
end
|
13
17
|
end
|
14
18
|
|
15
|
-
String.send :include, ActiveSupportMethods
|
19
|
+
String.send :include, ActiveSupportMethods unless String.new.respond_to?(:underscore)
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# taken from ActiveSupport 2.3.2
|
2
|
+
unless :to_proc.respond_to?(:to_proc)
|
3
|
+
class Symbol
|
4
|
+
# Turns the symbol into a simple proc, which is especially useful for enumerations. Examples:
|
5
|
+
#
|
6
|
+
# # The same as people.collect { |p| p.name }
|
7
|
+
# people.collect(&:name)
|
8
|
+
#
|
9
|
+
# # The same as people.select { |p| p.manager? }.collect { |p| p.salary }
|
10
|
+
# people.select(&:manager?).collect(&:salary)
|
11
|
+
def to_proc
|
12
|
+
Proc.new { |*args| args.shift.__send__(self, *args) }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -8,8 +8,8 @@ module CouchPotato
|
|
8
8
|
attr_accessor :skip_callbacks
|
9
9
|
def self.callbacks
|
10
10
|
@callbacks ||= {}
|
11
|
-
@callbacks[self.name] ||= {:before_validation_on_create => [],
|
12
|
-
:before_validation_on_update => [], :before_validation_on_save => [], :before_create => [],
|
11
|
+
@callbacks[self.name] ||= {:before_validation => [], :before_validation_on_create => [],
|
12
|
+
:before_validation_on_update => [], :before_validation_on_save => [], :before_create => [],
|
13
13
|
:after_create => [], :before_update => [], :after_update => [],
|
14
14
|
:before_save => [], :after_save => [],
|
15
15
|
:before_destroy => [], :after_destroy => []}
|
@@ -35,6 +35,7 @@ module CouchPotato
|
|
35
35
|
|
36
36
|
module ClassMethods
|
37
37
|
[
|
38
|
+
:before_validation,
|
38
39
|
:before_validation_on_create,
|
39
40
|
:before_validation_on_update,
|
40
41
|
:before_validation_on_save,
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'validatable'
|
2
|
+
|
3
|
+
module CouchPotato
|
4
|
+
module Persistence
|
5
|
+
module Validation
|
6
|
+
def self.included(base)
|
7
|
+
base.send :include, Validatable
|
8
|
+
base.class_eval do
|
9
|
+
# Override the validate method to first run before_validation callback
|
10
|
+
def valid?
|
11
|
+
self.run_callbacks :before_validation
|
12
|
+
super
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -5,6 +5,7 @@ require File.dirname(__FILE__) + '/persistence/magic_timestamps'
|
|
5
5
|
require File.dirname(__FILE__) + '/persistence/callbacks'
|
6
6
|
require File.dirname(__FILE__) + '/persistence/json'
|
7
7
|
require File.dirname(__FILE__) + '/persistence/dirty_attributes'
|
8
|
+
require File.dirname(__FILE__) + '/persistence/validation'
|
8
9
|
require File.dirname(__FILE__) + '/view/custom_views'
|
9
10
|
require File.dirname(__FILE__) + '/view/view_query'
|
10
11
|
|
@@ -13,7 +14,7 @@ module CouchPotato
|
|
13
14
|
module Persistence
|
14
15
|
|
15
16
|
def self.included(base)
|
16
|
-
base.send :include, Properties, Callbacks,
|
17
|
+
base.send :include, Properties, Callbacks, Validation, Json, CouchPotato::View::CustomViews
|
17
18
|
base.send :include, DirtyAttributes
|
18
19
|
base.send :include, MagicTimestamps
|
19
20
|
base.class_eval do
|
@@ -78,6 +79,7 @@ module CouchPotato
|
|
78
79
|
def new?
|
79
80
|
_rev.nil?
|
80
81
|
end
|
82
|
+
alias_method :new_record?, :new?
|
81
83
|
|
82
84
|
# returns the document id
|
83
85
|
# this is used by rails to construct URLs
|
data/lib/couch_potato.rb
CHANGED
data/spec/callbacks_spec.rb
CHANGED
@@ -7,7 +7,7 @@ class CallbackRecorder
|
|
7
7
|
|
8
8
|
validates_presence_of :required_property
|
9
9
|
|
10
|
-
[:before_validation_on_create,
|
10
|
+
[:before_validation, :before_validation_on_create,
|
11
11
|
:before_validation_on_save, :before_validation_on_update,
|
12
12
|
:before_save, :before_create, :before_create,
|
13
13
|
:after_save, :after_create, :after_create,
|
@@ -76,6 +76,11 @@ describe 'create callbacks' do
|
|
76
76
|
@recorder.required_property = 1
|
77
77
|
end
|
78
78
|
|
79
|
+
it "should call before_validation" do
|
80
|
+
@recorder.valid?
|
81
|
+
@recorder.callbacks.should include(:before_validation)
|
82
|
+
end
|
83
|
+
|
79
84
|
it "should call before_validation_on_create" do
|
80
85
|
@db.save_document! @recorder
|
81
86
|
@recorder.callbacks.should include(:before_validation_on_create)
|
@@ -110,6 +115,11 @@ describe 'create callbacks' do
|
|
110
115
|
|
111
116
|
describe "failed create" do
|
112
117
|
|
118
|
+
it "should call before_validation" do
|
119
|
+
@recorder.valid?
|
120
|
+
@recorder.callbacks.should include(:before_validation)
|
121
|
+
end
|
122
|
+
|
113
123
|
it "should call before_validation_on_create" do
|
114
124
|
@db.save_document @recorder
|
115
125
|
@recorder.callbacks.should include(:before_validation_on_create)
|
@@ -161,6 +171,10 @@ describe "update callbacks" do
|
|
161
171
|
@db.save_document! @recorder
|
162
172
|
end
|
163
173
|
|
174
|
+
it "should call before_validation" do
|
175
|
+
@recorder.callbacks.should include(:before_validation)
|
176
|
+
end
|
177
|
+
|
164
178
|
it "should call before_validation_on_update" do
|
165
179
|
@recorder.callbacks.should include(:before_validation_on_update)
|
166
180
|
end
|
@@ -194,6 +208,10 @@ describe "update callbacks" do
|
|
194
208
|
@db.save_document @recorder
|
195
209
|
end
|
196
210
|
|
211
|
+
it "should call before_validation" do
|
212
|
+
@recorder.callbacks.should include(:before_validation)
|
213
|
+
end
|
214
|
+
|
197
215
|
it "should call before_validation_on_update" do
|
198
216
|
@recorder.callbacks.should include(:before_validation_on_update)
|
199
217
|
end
|
data/spec/property_spec.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
require File.join(File.dirname(__FILE__), 'fixtures', 'address')
|
3
|
+
require File.join(File.dirname(__FILE__), 'fixtures', 'person')
|
2
4
|
|
3
5
|
class Watch
|
4
6
|
include CouchPotato::Persistence
|
@@ -44,6 +46,23 @@ describe 'properties' do
|
|
44
46
|
w.time.year.should == Time.now.year
|
45
47
|
end
|
46
48
|
|
49
|
+
it "should persist an object" do
|
50
|
+
p = Person.new :name => 'Bob'
|
51
|
+
a = Address.new :city => 'Denver'
|
52
|
+
p.ship_address = a
|
53
|
+
CouchPotato.database.save_document! p
|
54
|
+
p = CouchPotato.database.load_document p.id
|
55
|
+
p.ship_address.should === a
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should persist null for a null " do
|
59
|
+
p = Person.new :name => 'Bob'
|
60
|
+
p.ship_address = nil
|
61
|
+
CouchPotato.database.save_document! p
|
62
|
+
p = CouchPotato.database.load_document p.id
|
63
|
+
p.ship_address.should be_nil
|
64
|
+
end
|
65
|
+
|
47
66
|
describe "predicate" do
|
48
67
|
it "should return true if property set" do
|
49
68
|
Comment.new(:title => 'title').title?.should be_true
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
class Tree
|
4
|
+
include CouchPotato::Persistence
|
5
|
+
before_validation :water!
|
6
|
+
before_validation lambda {|tree| tree.root_count += 1 }
|
7
|
+
|
8
|
+
property :leaf_count
|
9
|
+
property :root_count
|
10
|
+
|
11
|
+
def water!
|
12
|
+
self.leaf_count += 1
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
describe 'before_validation callback' do
|
18
|
+
before :each do
|
19
|
+
@tree = Tree.new(:leaf_count => 1, :root_count => 1)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should call water! when validated" do
|
23
|
+
@tree.leaf_count.should == 1
|
24
|
+
@tree.should be_valid
|
25
|
+
@tree.leaf_count.should == 2
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should call lambda when validated" do
|
29
|
+
@tree.root_count.should == 1
|
30
|
+
@tree.should be_valid
|
31
|
+
@tree.root_count.should == 2
|
32
|
+
end
|
33
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: langalex-couch_potato
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Lang
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-07-01 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -58,6 +58,7 @@ files:
|
|
58
58
|
- lib/core_ext/date.rb
|
59
59
|
- lib/core_ext/object.rb
|
60
60
|
- lib/core_ext/string.rb
|
61
|
+
- lib/core_ext/symbol.rb
|
61
62
|
- lib/core_ext/time.rb
|
62
63
|
- lib/couch_potato.rb
|
63
64
|
- lib/couch_potato/database.rb
|
@@ -69,6 +70,7 @@ files:
|
|
69
70
|
- lib/couch_potato/persistence/magic_timestamps.rb
|
70
71
|
- lib/couch_potato/persistence/properties.rb
|
71
72
|
- lib/couch_potato/persistence/simple_property.rb
|
73
|
+
- lib/couch_potato/persistence/validation.rb
|
72
74
|
- lib/couch_potato/view/base_view_spec.rb
|
73
75
|
- lib/couch_potato/view/custom_view_spec.rb
|
74
76
|
- lib/couch_potato/view/custom_views.rb
|
@@ -81,10 +83,13 @@ files:
|
|
81
83
|
- spec/create_spec.rb
|
82
84
|
- spec/custom_view_spec.rb
|
83
85
|
- spec/destroy_spec.rb
|
86
|
+
- spec/fixtures/address.rb
|
87
|
+
- spec/fixtures/person.rb
|
84
88
|
- spec/property_spec.rb
|
85
89
|
- spec/spec.opts
|
86
90
|
- spec/spec_helper.rb
|
87
91
|
- spec/unit/attributes_spec.rb
|
92
|
+
- spec/unit/callbacks_spec.rb
|
88
93
|
- spec/unit/create_spec.rb
|
89
94
|
- spec/unit/customs_views_spec.rb
|
90
95
|
- spec/unit/database_spec.rb
|
@@ -123,9 +128,12 @@ test_files:
|
|
123
128
|
- spec/create_spec.rb
|
124
129
|
- spec/custom_view_spec.rb
|
125
130
|
- spec/destroy_spec.rb
|
131
|
+
- spec/fixtures/address.rb
|
132
|
+
- spec/fixtures/person.rb
|
126
133
|
- spec/property_spec.rb
|
127
134
|
- spec/spec_helper.rb
|
128
135
|
- spec/unit/attributes_spec.rb
|
136
|
+
- spec/unit/callbacks_spec.rb
|
129
137
|
- spec/unit/create_spec.rb
|
130
138
|
- spec/unit/customs_views_spec.rb
|
131
139
|
- spec/unit/database_spec.rb
|