logical_model 0.2.11 → 0.2.12

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.11
1
+ 0.2.12
@@ -48,8 +48,13 @@ class LogicalModel
48
48
  include ActiveModel::Validations
49
49
  include ActiveModel::MassAssignmentSecurity
50
50
 
51
+ extend ActiveModel::Callbacks
52
+ define_model_callbacks :create, :save, :update, :destroy
53
+
51
54
  self.include_root_in_json = false
52
55
 
56
+ # TODO clean this file refactoring into modules
57
+
53
58
  def self.attribute_keys=(keys)
54
59
  @attribute_keys = keys
55
60
  attr_accessor *keys
@@ -230,6 +235,37 @@ class LogicalModel
230
235
  end
231
236
  end
232
237
 
238
+ def create(params={})
239
+ run_callbacks :save do
240
+ run_callbacks :create do
241
+ _create(params)
242
+ end
243
+ end
244
+ end
245
+
246
+ def update(attribute_params)
247
+ run_callbacks :save do
248
+ run_callbacks :update do
249
+ _update(attribute_params)
250
+ end
251
+ end
252
+
253
+ end
254
+
255
+ def save
256
+ run_callbacks :save do
257
+ run_callbacks new_record?? :create : :update do
258
+ _save
259
+ end
260
+ end
261
+ end
262
+
263
+ def delete(id, params={})
264
+ run_callbacks :destroy do
265
+ _delete(id, params={})
266
+ end
267
+ end
268
+
233
269
  # ============================================================================================================
234
270
  # Following methods are API specific.
235
271
  # They assume we are using a RESTfull API.
@@ -391,7 +427,7 @@ class LogicalModel
391
427
  # @example Usage:
392
428
  # @person = Person.new(params[:person])
393
429
  # @person.create( non_attribute_param: "value" )
394
- def create(params = {})
430
+ def _create(params = {})
395
431
  return false unless valid?
396
432
 
397
433
  params = { self.json_root => self.attributes }.merge(params)
@@ -429,7 +465,7 @@ class LogicalModel
429
465
  #
430
466
  # Usage:
431
467
  # @person.update(params[:person])
432
- def update(attribute_params)
468
+ def _update(attribute_params)
433
469
 
434
470
  self.attributes = attribute_params[self.json_root]
435
471
 
@@ -474,7 +510,7 @@ class LogicalModel
474
510
  #
475
511
  # Usage:
476
512
  # @person.save
477
- def save
513
+ def _save
478
514
  self.attributes = attributes
479
515
 
480
516
  return false unless valid?
@@ -509,7 +545,7 @@ class LogicalModel
509
545
  #
510
546
  # Usage:
511
547
  # Person.delete(params[:id])
512
- def self.delete(id, params={})
548
+ def self._delete(id, params={})
513
549
 
514
550
  params = self.merge_key(params)
515
551
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "logical_model"
8
- s.version = "0.2.11"
8
+ s.version = "0.2.12"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Dwayne Macgowan"]
12
- s.date = "2012-01-17"
12
+ s.date = "2012-01-25"
13
13
  s.description = "LogicalModel allows to use a resource as a model. It is based on web presentation http://www.slideshare.net/ihower/serviceoriented-design-and-implement-with-rails3"
14
14
  s.email = "dwaynemac@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -154,4 +154,31 @@ describe "LogicalModel User client" do
154
154
  end
155
155
  end
156
156
  end
157
+
158
+ describe "callbacks" do
159
+ describe "before_save" do
160
+ before do
161
+ class User < LogicalModel
162
+ before_save :raise_message
163
+
164
+ def raise_message
165
+ raise 'before_save_called'
166
+ end
167
+ end
168
+ end
169
+ it "should be called on #save" do
170
+ u = User.new
171
+ expect{u.save}.to raise_error('before_save_called')
172
+ end
173
+ it "should be called on #create" do
174
+ u = User.new
175
+ expect{u.create}.to raise_error('before_save_called')
176
+ end
177
+ it "should be called on #update" do
178
+ u = User.new
179
+ expect{u.update(id: 1)}.to raise_error('before_save_called')
180
+ end
181
+ end
182
+ end
183
+
157
184
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logical_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.11
4
+ version: 0.2.12
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-17 00:00:00.000000000Z
12
+ date: 2012-01-25 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
16
- requirement: &79777970 !ruby/object:Gem::Requirement
16
+ requirement: &85369270 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *79777970
24
+ version_requirements: *85369270
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &79777350 !ruby/object:Gem::Requirement
27
+ requirement: &85368680 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *79777350
35
+ version_requirements: *85368680
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: typhoeus
38
- requirement: &79776880 !ruby/object:Gem::Requirement
38
+ requirement: &85368130 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 0.2.4
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *79776880
46
+ version_requirements: *85368130
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: kaminari
49
- requirement: &79776350 !ruby/object:Gem::Requirement
49
+ requirement: &85367470 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 0.13.0
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *79776350
57
+ version_requirements: *85367470
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: activerecord
60
- requirement: &79775710 !ruby/object:Gem::Requirement
60
+ requirement: &85367000 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *79775710
68
+ version_requirements: *85367000
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: shoulda
71
- requirement: &79774980 !ruby/object:Gem::Requirement
71
+ requirement: &85366330 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *79774980
79
+ version_requirements: *85366330
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: bundler
82
- requirement: &79774320 !ruby/object:Gem::Requirement
82
+ requirement: &85365440 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: 1.0.10
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *79774320
90
+ version_requirements: *85365440
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: jeweler
93
- requirement: &79773570 !ruby/object:Gem::Requirement
93
+ requirement: &85364770 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ~>
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: 1.6.4
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *79773570
101
+ version_requirements: *85364770
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: rcov
104
- requirement: &79772870 !ruby/object:Gem::Requirement
104
+ requirement: &85364210 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *79772870
112
+ version_requirements: *85364210
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: sqlite3-ruby
115
- requirement: &79743610 !ruby/object:Gem::Requirement
115
+ requirement: &85333950 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,10 +120,10 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *79743610
123
+ version_requirements: *85333950
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: sinatra
126
- requirement: &79742900 !ruby/object:Gem::Requirement
126
+ requirement: &85332410 !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
129
129
  - - ~>
@@ -131,10 +131,10 @@ dependencies:
131
131
  version: 1.2.6
132
132
  type: :development
133
133
  prerelease: false
134
- version_requirements: *79742900
134
+ version_requirements: *85332410
135
135
  - !ruby/object:Gem::Dependency
136
136
  name: json
137
- requirement: &79741620 !ruby/object:Gem::Requirement
137
+ requirement: &85331880 !ruby/object:Gem::Requirement
138
138
  none: false
139
139
  requirements:
140
140
  - - ! '>='
@@ -142,10 +142,10 @@ dependencies:
142
142
  version: '0'
143
143
  type: :development
144
144
  prerelease: false
145
- version_requirements: *79741620
145
+ version_requirements: *85331880
146
146
  - !ruby/object:Gem::Dependency
147
147
  name: rspec-rails
148
- requirement: &79740370 !ruby/object:Gem::Requirement
148
+ requirement: &85331510 !ruby/object:Gem::Requirement
149
149
  none: false
150
150
  requirements:
151
151
  - - ! '>='
@@ -153,10 +153,10 @@ dependencies:
153
153
  version: '0'
154
154
  type: :development
155
155
  prerelease: false
156
- version_requirements: *79740370
156
+ version_requirements: *85331510
157
157
  - !ruby/object:Gem::Dependency
158
158
  name: guard-rspec
159
- requirement: &79739390 !ruby/object:Gem::Requirement
159
+ requirement: &85330640 !ruby/object:Gem::Requirement
160
160
  none: false
161
161
  requirements:
162
162
  - - ! '>='
@@ -164,10 +164,10 @@ dependencies:
164
164
  version: '0'
165
165
  type: :development
166
166
  prerelease: false
167
- version_requirements: *79739390
167
+ version_requirements: *85330640
168
168
  - !ruby/object:Gem::Dependency
169
169
  name: rb-inotify
170
- requirement: &79738470 !ruby/object:Gem::Requirement
170
+ requirement: &85329900 !ruby/object:Gem::Requirement
171
171
  none: false
172
172
  requirements:
173
173
  - - ! '>='
@@ -175,10 +175,10 @@ dependencies:
175
175
  version: '0'
176
176
  type: :development
177
177
  prerelease: false
178
- version_requirements: *79738470
178
+ version_requirements: *85329900
179
179
  - !ruby/object:Gem::Dependency
180
180
  name: libnotify
181
- requirement: &79737780 !ruby/object:Gem::Requirement
181
+ requirement: &85329310 !ruby/object:Gem::Requirement
182
182
  none: false
183
183
  requirements:
184
184
  - - ! '>='
@@ -186,7 +186,7 @@ dependencies:
186
186
  version: '0'
187
187
  type: :development
188
188
  prerelease: false
189
- version_requirements: *79737780
189
+ version_requirements: *85329310
190
190
  description: LogicalModel allows to use a resource as a model. It is based on web
191
191
  presentation http://www.slideshare.net/ihower/serviceoriented-design-and-implement-with-rails3
192
192
  email: dwaynemac@gmail.com
@@ -234,7 +234,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
234
234
  version: '0'
235
235
  segments:
236
236
  - 0
237
- hash: 788754387
237
+ hash: 550676079
238
238
  required_rubygems_version: !ruby/object:Gem::Requirement
239
239
  none: false
240
240
  requirements: