acfs 0.42.0 → 0.43.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +8 -9
- data/acfs.gemspec +3 -3
- data/lib/acfs.rb +0 -2
- data/lib/acfs/adapter/base.rb +0 -2
- data/lib/acfs/adapter/typhoeus.rb +6 -9
- data/lib/acfs/collection.rb +3 -3
- data/lib/acfs/collections/paginatable.rb +16 -16
- data/lib/acfs/configuration.rb +3 -5
- data/lib/acfs/errors.rb +8 -7
- data/lib/acfs/global.rb +1 -1
- data/lib/acfs/location.rb +11 -11
- data/lib/acfs/middleware/base.rb +1 -2
- data/lib/acfs/middleware/json.rb +0 -1
- data/lib/acfs/middleware/logger.rb +0 -2
- data/lib/acfs/middleware/print.rb +0 -2
- data/lib/acfs/middleware/serializer.rb +3 -6
- data/lib/acfs/operation.rb +3 -4
- data/lib/acfs/request.rb +2 -3
- data/lib/acfs/request/callbacks.rb +2 -3
- data/lib/acfs/resource.rb +34 -5
- data/lib/acfs/{model → resource}/attributes.rb +70 -46
- data/lib/acfs/{model → resource}/attributes/base.rb +10 -6
- data/lib/acfs/resource/attributes/boolean.rb +33 -0
- data/lib/acfs/resource/attributes/date_time.rb +32 -0
- data/lib/acfs/{model → resource}/attributes/dict.rb +1 -3
- data/lib/acfs/{model → resource}/attributes/float.rb +3 -6
- data/lib/acfs/{model → resource}/attributes/integer.rb +3 -6
- data/lib/acfs/{model → resource}/attributes/list.rb +2 -5
- data/lib/acfs/{model → resource}/attributes/string.rb +4 -6
- data/lib/acfs/{model → resource}/attributes/uuid.rb +18 -8
- data/lib/acfs/resource/dirty.rb +47 -0
- data/lib/acfs/{model → resource}/initialization.rb +8 -10
- data/lib/acfs/{model → resource}/loadable.rb +3 -4
- data/lib/acfs/{model → resource}/locatable.rb +22 -23
- data/lib/acfs/{model → resource}/operational.rb +2 -3
- data/lib/acfs/resource/persistence.rb +257 -0
- data/lib/acfs/{model → resource}/query_methods.rb +81 -66
- data/lib/acfs/{model → resource}/service.rb +10 -9
- data/lib/acfs/resource/validation.rb +28 -0
- data/lib/acfs/response.rb +1 -2
- data/lib/acfs/response/formats.rb +1 -2
- data/lib/acfs/response/status.rb +3 -5
- data/lib/acfs/runner.rb +4 -5
- data/lib/acfs/service.rb +4 -6
- data/lib/acfs/service/middleware.rb +1 -3
- data/lib/acfs/singleton_resource.rb +11 -24
- data/lib/acfs/stub.rb +30 -22
- data/lib/acfs/util.rb +1 -1
- data/lib/acfs/version.rb +4 -2
- data/spec/acfs/adapter/typhoeus_spec.rb +4 -4
- data/spec/acfs/collection_spec.rb +33 -33
- data/spec/acfs/configuration_spec.rb +0 -1
- data/spec/acfs/global_spec.rb +3 -3
- data/spec/acfs/middleware/json_spec.rb +2 -2
- data/spec/acfs/middleware/msgpack_spec.rb +4 -4
- data/spec/acfs/request/callbacks_spec.rb +8 -8
- data/spec/acfs/request_spec.rb +5 -5
- data/spec/acfs/{model → resource}/attributes/boolean_spec.rb +2 -2
- data/spec/acfs/{model → resource}/attributes/date_time_spec.rb +7 -7
- data/spec/acfs/{model → resource}/attributes/dict_spec.rb +6 -6
- data/spec/acfs/{model → resource}/attributes/float_spec.rb +3 -3
- data/spec/acfs/{model → resource}/attributes/list_spec.rb +5 -5
- data/spec/acfs/{model → resource}/attributes/uuid_spec.rb +6 -6
- data/spec/acfs/{model → resource}/attributes_spec.rb +31 -17
- data/spec/acfs/{model → resource}/dirty_spec.rb +7 -5
- data/spec/acfs/{model → resource}/initialization_spec.rb +7 -7
- data/spec/acfs/{model → resource}/loadable_spec.rb +4 -3
- data/spec/acfs/{model → resource}/locatable_spec.rb +24 -14
- data/spec/acfs/{model → resource}/persistance_spec.rb +34 -34
- data/spec/acfs/{model → resource}/query_methods_spec.rb +171 -130
- data/spec/acfs/{model → resource}/validation_spec.rb +5 -6
- data/spec/acfs/response/formats_spec.rb +1 -1
- data/spec/acfs/response/status_spec.rb +1 -1
- data/spec/acfs/runner_spec.rb +2 -3
- data/spec/acfs/service/middleware_spec.rb +1 -1
- data/spec/acfs/service_spec.rb +3 -5
- data/spec/acfs/singleton_resource_spec.rb +3 -3
- data/spec/acfs/stub_spec.rb +52 -24
- data/spec/acfs_spec.rb +22 -19
- data/spec/spec_helper.rb +1 -1
- data/spec/support/hash.rb +9 -0
- data/spec/support/service.rb +4 -7
- data/spec/support/shared/find_callbacks.rb +7 -7
- metadata +52 -52
- data/lib/acfs/model.rb +0 -43
- data/lib/acfs/model/attributes/boolean.rb +0 -38
- data/lib/acfs/model/attributes/date_time.rb +0 -30
- data/lib/acfs/model/dirty.rb +0 -49
- data/lib/acfs/model/persistence.rb +0 -243
- data/lib/acfs/model/relations.rb +0 -10
- data/lib/acfs/model/validation.rb +0 -30
@@ -2,23 +2,23 @@ shared_examples 'a query method with multi-callback support' do
|
|
2
2
|
let(:cb) { Proc.new }
|
3
3
|
|
4
4
|
it 'should invoke callback' do
|
5
|
-
expect
|
5
|
+
expect do |cb|
|
6
6
|
action.call cb
|
7
7
|
Acfs.run
|
8
|
-
|
8
|
+
end.to yield_with_args
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'should invoke multiple callbacks' do
|
12
|
-
expect
|
12
|
+
expect do |cb|
|
13
13
|
object = action.call cb
|
14
14
|
Acfs.add_callback object, &cb
|
15
15
|
Acfs.run
|
16
|
-
|
16
|
+
end.to yield_control.exactly(2).times
|
17
17
|
end
|
18
18
|
|
19
19
|
describe 'callback' do
|
20
20
|
it 'should be invoked with resource' do
|
21
|
-
proc =
|
21
|
+
proc = proc {}
|
22
22
|
expect(proc).to receive(:call) do |res|
|
23
23
|
expect(res).to equal @object
|
24
24
|
expect(res).to be_loaded
|
@@ -29,8 +29,8 @@ shared_examples 'a query method with multi-callback support' do
|
|
29
29
|
end
|
30
30
|
|
31
31
|
it 'should invoke multiple callback with loaded resource' do
|
32
|
-
proc1 =
|
33
|
-
proc2 =
|
32
|
+
proc1 = proc {}
|
33
|
+
proc2 = proc {}
|
34
34
|
expect(proc1).to receive(:call) do |user|
|
35
35
|
expect(user).to equal @object
|
36
36
|
expect(user).to be_loaded
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acfs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.43.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan Graichen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-03-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -134,31 +134,29 @@ files:
|
|
134
134
|
- lib/acfs/middleware/msgpack.rb
|
135
135
|
- lib/acfs/middleware/print.rb
|
136
136
|
- lib/acfs/middleware/serializer.rb
|
137
|
-
- lib/acfs/model.rb
|
138
|
-
- lib/acfs/model/attributes.rb
|
139
|
-
- lib/acfs/model/attributes/base.rb
|
140
|
-
- lib/acfs/model/attributes/boolean.rb
|
141
|
-
- lib/acfs/model/attributes/date_time.rb
|
142
|
-
- lib/acfs/model/attributes/dict.rb
|
143
|
-
- lib/acfs/model/attributes/float.rb
|
144
|
-
- lib/acfs/model/attributes/integer.rb
|
145
|
-
- lib/acfs/model/attributes/list.rb
|
146
|
-
- lib/acfs/model/attributes/string.rb
|
147
|
-
- lib/acfs/model/attributes/uuid.rb
|
148
|
-
- lib/acfs/model/dirty.rb
|
149
|
-
- lib/acfs/model/initialization.rb
|
150
|
-
- lib/acfs/model/loadable.rb
|
151
|
-
- lib/acfs/model/locatable.rb
|
152
|
-
- lib/acfs/model/operational.rb
|
153
|
-
- lib/acfs/model/persistence.rb
|
154
|
-
- lib/acfs/model/query_methods.rb
|
155
|
-
- lib/acfs/model/relations.rb
|
156
|
-
- lib/acfs/model/service.rb
|
157
|
-
- lib/acfs/model/validation.rb
|
158
137
|
- lib/acfs/operation.rb
|
159
138
|
- lib/acfs/request.rb
|
160
139
|
- lib/acfs/request/callbacks.rb
|
161
140
|
- lib/acfs/resource.rb
|
141
|
+
- lib/acfs/resource/attributes.rb
|
142
|
+
- lib/acfs/resource/attributes/base.rb
|
143
|
+
- lib/acfs/resource/attributes/boolean.rb
|
144
|
+
- lib/acfs/resource/attributes/date_time.rb
|
145
|
+
- lib/acfs/resource/attributes/dict.rb
|
146
|
+
- lib/acfs/resource/attributes/float.rb
|
147
|
+
- lib/acfs/resource/attributes/integer.rb
|
148
|
+
- lib/acfs/resource/attributes/list.rb
|
149
|
+
- lib/acfs/resource/attributes/string.rb
|
150
|
+
- lib/acfs/resource/attributes/uuid.rb
|
151
|
+
- lib/acfs/resource/dirty.rb
|
152
|
+
- lib/acfs/resource/initialization.rb
|
153
|
+
- lib/acfs/resource/loadable.rb
|
154
|
+
- lib/acfs/resource/locatable.rb
|
155
|
+
- lib/acfs/resource/operational.rb
|
156
|
+
- lib/acfs/resource/persistence.rb
|
157
|
+
- lib/acfs/resource/query_methods.rb
|
158
|
+
- lib/acfs/resource/service.rb
|
159
|
+
- lib/acfs/resource/validation.rb
|
162
160
|
- lib/acfs/response.rb
|
163
161
|
- lib/acfs/response/formats.rb
|
164
162
|
- lib/acfs/response/status.rb
|
@@ -177,23 +175,23 @@ files:
|
|
177
175
|
- spec/acfs/global_spec.rb
|
178
176
|
- spec/acfs/middleware/json_spec.rb
|
179
177
|
- spec/acfs/middleware/msgpack_spec.rb
|
180
|
-
- spec/acfs/model/attributes/boolean_spec.rb
|
181
|
-
- spec/acfs/model/attributes/date_time_spec.rb
|
182
|
-
- spec/acfs/model/attributes/dict_spec.rb
|
183
|
-
- spec/acfs/model/attributes/float_spec.rb
|
184
|
-
- spec/acfs/model/attributes/list_spec.rb
|
185
|
-
- spec/acfs/model/attributes/uuid_spec.rb
|
186
|
-
- spec/acfs/model/attributes_spec.rb
|
187
|
-
- spec/acfs/model/dirty_spec.rb
|
188
|
-
- spec/acfs/model/initialization_spec.rb
|
189
|
-
- spec/acfs/model/loadable_spec.rb
|
190
|
-
- spec/acfs/model/locatable_spec.rb
|
191
|
-
- spec/acfs/model/persistance_spec.rb
|
192
|
-
- spec/acfs/model/query_methods_spec.rb
|
193
|
-
- spec/acfs/model/validation_spec.rb
|
194
178
|
- spec/acfs/operation_spec.rb
|
195
179
|
- spec/acfs/request/callbacks_spec.rb
|
196
180
|
- spec/acfs/request_spec.rb
|
181
|
+
- spec/acfs/resource/attributes/boolean_spec.rb
|
182
|
+
- spec/acfs/resource/attributes/date_time_spec.rb
|
183
|
+
- spec/acfs/resource/attributes/dict_spec.rb
|
184
|
+
- spec/acfs/resource/attributes/float_spec.rb
|
185
|
+
- spec/acfs/resource/attributes/list_spec.rb
|
186
|
+
- spec/acfs/resource/attributes/uuid_spec.rb
|
187
|
+
- spec/acfs/resource/attributes_spec.rb
|
188
|
+
- spec/acfs/resource/dirty_spec.rb
|
189
|
+
- spec/acfs/resource/initialization_spec.rb
|
190
|
+
- spec/acfs/resource/loadable_spec.rb
|
191
|
+
- spec/acfs/resource/locatable_spec.rb
|
192
|
+
- spec/acfs/resource/persistance_spec.rb
|
193
|
+
- spec/acfs/resource/query_methods_spec.rb
|
194
|
+
- spec/acfs/resource/validation_spec.rb
|
197
195
|
- spec/acfs/response/formats_spec.rb
|
198
196
|
- spec/acfs/response/status_spec.rb
|
199
197
|
- spec/acfs/runner_spec.rb
|
@@ -204,6 +202,7 @@ files:
|
|
204
202
|
- spec/acfs_spec.rb
|
205
203
|
- spec/fixtures/config.yml
|
206
204
|
- spec/spec_helper.rb
|
205
|
+
- spec/support/hash.rb
|
207
206
|
- spec/support/response.rb
|
208
207
|
- spec/support/service.rb
|
209
208
|
- spec/support/shared/find_callbacks.rb
|
@@ -227,7 +226,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
227
226
|
version: '0'
|
228
227
|
requirements: []
|
229
228
|
rubyforge_project:
|
230
|
-
rubygems_version: 2.
|
229
|
+
rubygems_version: 2.4.6
|
231
230
|
signing_key:
|
232
231
|
specification_version: 4
|
233
232
|
summary: An abstract API base client for service oriented application.
|
@@ -238,23 +237,23 @@ test_files:
|
|
238
237
|
- spec/acfs/global_spec.rb
|
239
238
|
- spec/acfs/middleware/json_spec.rb
|
240
239
|
- spec/acfs/middleware/msgpack_spec.rb
|
241
|
-
- spec/acfs/model/attributes/boolean_spec.rb
|
242
|
-
- spec/acfs/model/attributes/date_time_spec.rb
|
243
|
-
- spec/acfs/model/attributes/dict_spec.rb
|
244
|
-
- spec/acfs/model/attributes/float_spec.rb
|
245
|
-
- spec/acfs/model/attributes/list_spec.rb
|
246
|
-
- spec/acfs/model/attributes/uuid_spec.rb
|
247
|
-
- spec/acfs/model/attributes_spec.rb
|
248
|
-
- spec/acfs/model/dirty_spec.rb
|
249
|
-
- spec/acfs/model/initialization_spec.rb
|
250
|
-
- spec/acfs/model/loadable_spec.rb
|
251
|
-
- spec/acfs/model/locatable_spec.rb
|
252
|
-
- spec/acfs/model/persistance_spec.rb
|
253
|
-
- spec/acfs/model/query_methods_spec.rb
|
254
|
-
- spec/acfs/model/validation_spec.rb
|
255
240
|
- spec/acfs/operation_spec.rb
|
256
241
|
- spec/acfs/request/callbacks_spec.rb
|
257
242
|
- spec/acfs/request_spec.rb
|
243
|
+
- spec/acfs/resource/attributes/boolean_spec.rb
|
244
|
+
- spec/acfs/resource/attributes/date_time_spec.rb
|
245
|
+
- spec/acfs/resource/attributes/dict_spec.rb
|
246
|
+
- spec/acfs/resource/attributes/float_spec.rb
|
247
|
+
- spec/acfs/resource/attributes/list_spec.rb
|
248
|
+
- spec/acfs/resource/attributes/uuid_spec.rb
|
249
|
+
- spec/acfs/resource/attributes_spec.rb
|
250
|
+
- spec/acfs/resource/dirty_spec.rb
|
251
|
+
- spec/acfs/resource/initialization_spec.rb
|
252
|
+
- spec/acfs/resource/loadable_spec.rb
|
253
|
+
- spec/acfs/resource/locatable_spec.rb
|
254
|
+
- spec/acfs/resource/persistance_spec.rb
|
255
|
+
- spec/acfs/resource/query_methods_spec.rb
|
256
|
+
- spec/acfs/resource/validation_spec.rb
|
258
257
|
- spec/acfs/response/formats_spec.rb
|
259
258
|
- spec/acfs/response/status_spec.rb
|
260
259
|
- spec/acfs/runner_spec.rb
|
@@ -265,6 +264,7 @@ test_files:
|
|
265
264
|
- spec/acfs_spec.rb
|
266
265
|
- spec/fixtures/config.yml
|
267
266
|
- spec/spec_helper.rb
|
267
|
+
- spec/support/hash.rb
|
268
268
|
- spec/support/response.rb
|
269
269
|
- spec/support/service.rb
|
270
270
|
- spec/support/shared/find_callbacks.rb
|
data/lib/acfs/model.rb
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
require 'active_model'
|
2
|
-
|
3
|
-
# @api public
|
4
|
-
#
|
5
|
-
module Acfs::Model
|
6
|
-
require 'acfs/model/initialization'
|
7
|
-
require 'acfs/model/attributes'
|
8
|
-
require 'acfs/model/dirty'
|
9
|
-
require 'acfs/model/loadable'
|
10
|
-
require 'acfs/model/locatable'
|
11
|
-
require 'acfs/model/operational'
|
12
|
-
require 'acfs/model/persistence'
|
13
|
-
require 'acfs/model/query_methods'
|
14
|
-
require 'acfs/model/relations'
|
15
|
-
require 'acfs/model/service'
|
16
|
-
require 'acfs/model/validation'
|
17
|
-
|
18
|
-
extend ActiveSupport::Concern
|
19
|
-
|
20
|
-
included do
|
21
|
-
if ActiveModel::VERSION::MAJOR >= 4
|
22
|
-
include ActiveModel::Model
|
23
|
-
else
|
24
|
-
extend ActiveModel::Naming
|
25
|
-
extend ActiveModel::Translation
|
26
|
-
include ActiveModel::Conversion
|
27
|
-
include ActiveModel::Validations
|
28
|
-
end
|
29
|
-
|
30
|
-
include Initialization
|
31
|
-
|
32
|
-
include Attributes
|
33
|
-
include Loadable
|
34
|
-
include Persistence
|
35
|
-
include Locatable
|
36
|
-
include Operational
|
37
|
-
include QueryMethods
|
38
|
-
include Relations
|
39
|
-
include Service
|
40
|
-
include Dirty
|
41
|
-
include Validation
|
42
|
-
end
|
43
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
module Acfs::Model
|
2
|
-
module Attributes
|
3
|
-
|
4
|
-
# @api public
|
5
|
-
#
|
6
|
-
# Boolean attribute type. Use it in your model as an attribute type:
|
7
|
-
#
|
8
|
-
# @example
|
9
|
-
# class User
|
10
|
-
# include Acfs::Model
|
11
|
-
# attribute :name, :boolean
|
12
|
-
# end
|
13
|
-
#
|
14
|
-
# Given objects will be converted to string. The following strings
|
15
|
-
# are considered true, everything else false:
|
16
|
-
#
|
17
|
-
# true, on, yes
|
18
|
-
#
|
19
|
-
class Boolean < Base
|
20
|
-
|
21
|
-
TRUE_VALUES = %w(true on yes 1)
|
22
|
-
|
23
|
-
# @api public
|
24
|
-
#
|
25
|
-
# Cast given object to boolean.
|
26
|
-
#
|
27
|
-
# @param [Object] obj Object to cast.
|
28
|
-
# @return [TrueClass, FalseClass] Casted boolean.
|
29
|
-
#
|
30
|
-
def cast_type(obj)
|
31
|
-
return true if obj.is_a? TrueClass
|
32
|
-
return false if obj.is_a? FalseClass
|
33
|
-
|
34
|
-
TRUE_VALUES.include? obj.to_s
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
module Acfs::Model::Attributes
|
2
|
-
|
3
|
-
# @api public
|
4
|
-
#
|
5
|
-
# DateTime attribute type. Use it in your model as an attribute type:
|
6
|
-
#
|
7
|
-
# @example
|
8
|
-
# class User
|
9
|
-
# include Acfs::Model
|
10
|
-
# attribute :name, :date_time
|
11
|
-
# end
|
12
|
-
#
|
13
|
-
class DateTime < Base
|
14
|
-
|
15
|
-
# @api public
|
16
|
-
#
|
17
|
-
# Cast given object to DateTime.
|
18
|
-
# Expect
|
19
|
-
#
|
20
|
-
# @param [Object] obj Object to cast.
|
21
|
-
# @return [DateTime] Casted object as DateTime.
|
22
|
-
#
|
23
|
-
def cast_type(obj)
|
24
|
-
return nil if nil_allowed? and obj.blank?
|
25
|
-
return obj if obj.is_a? ::DateTime
|
26
|
-
return ::DateTime.iso8601(obj.iso8601) if obj.is_a? Time or obj.is_a? Date
|
27
|
-
return ::DateTime.iso8601(obj)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
data/lib/acfs/model/dirty.rb
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
module Acfs
|
2
|
-
module Model
|
3
|
-
|
4
|
-
# Thin wrapper around ActiveModel::Dirty
|
5
|
-
#
|
6
|
-
module Dirty
|
7
|
-
extend ActiveSupport::Concern
|
8
|
-
include ActiveModel::Dirty
|
9
|
-
|
10
|
-
# @api private
|
11
|
-
#
|
12
|
-
# Resets all changes. Does not touch previous changes.
|
13
|
-
#
|
14
|
-
def reset_changes
|
15
|
-
changed_attributes.clear
|
16
|
-
end
|
17
|
-
|
18
|
-
# @api private
|
19
|
-
#
|
20
|
-
# Save current changes as previous changes and reset
|
21
|
-
# current one.
|
22
|
-
#
|
23
|
-
def swap_changes
|
24
|
-
@previously_changed = changes
|
25
|
-
reset_changes
|
26
|
-
end
|
27
|
-
|
28
|
-
# @api private
|
29
|
-
#
|
30
|
-
def save!(*)
|
31
|
-
super.tap { |_| swap_changes }
|
32
|
-
end
|
33
|
-
|
34
|
-
# @api private
|
35
|
-
#
|
36
|
-
def loaded!
|
37
|
-
reset_changes
|
38
|
-
super
|
39
|
-
end
|
40
|
-
|
41
|
-
# @api private
|
42
|
-
#
|
43
|
-
def write_raw_attribute(name, value, opts = {}) # :nodoc:
|
44
|
-
attribute_will_change! name if opts[:change].nil? or opts[:change]
|
45
|
-
super
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
@@ -1,243 +0,0 @@
|
|
1
|
-
module Acfs
|
2
|
-
module Model
|
3
|
-
|
4
|
-
# Allow to track the persistence state of a model.
|
5
|
-
#
|
6
|
-
module Persistence
|
7
|
-
extend ActiveSupport::Concern
|
8
|
-
|
9
|
-
# @api public
|
10
|
-
#
|
11
|
-
# Check if the model is persisted. A model is persisted if
|
12
|
-
# it is saved after being created
|
13
|
-
#
|
14
|
-
# @example Newly created resource:
|
15
|
-
# user = User.new name: "John"
|
16
|
-
# user.persisted? # => false
|
17
|
-
# user.save
|
18
|
-
# user.persisted? # => true
|
19
|
-
#
|
20
|
-
# @example Modified resource:
|
21
|
-
# user2 = User.find 5
|
22
|
-
# user2.persisted? # => true
|
23
|
-
# user2.name = 'Amy'
|
24
|
-
# user2.persisted? # => true
|
25
|
-
# user2.save
|
26
|
-
# user2.persisted? # => true
|
27
|
-
#
|
28
|
-
# @return [Boolean] True if resource has been saved
|
29
|
-
#
|
30
|
-
def persisted?
|
31
|
-
!new?
|
32
|
-
end
|
33
|
-
|
34
|
-
# @api public
|
35
|
-
#
|
36
|
-
# Return true if model is a new record and was not saved yet.
|
37
|
-
#
|
38
|
-
# @return [Boolean] True if resource is newly created, false otherwise.
|
39
|
-
#
|
40
|
-
def new?
|
41
|
-
!loaded?
|
42
|
-
end
|
43
|
-
alias :new_record? :new?
|
44
|
-
|
45
|
-
# @api public
|
46
|
-
#
|
47
|
-
# Saves the resource.
|
48
|
-
#
|
49
|
-
# It will PUT to the service to update the resource or send
|
50
|
-
# a POST to create a new one if the resource is new.
|
51
|
-
#
|
52
|
-
# Saving a resource is a synchronous operation.
|
53
|
-
#
|
54
|
-
# @return [Boolean] True if save operation was successful, false otherwise.
|
55
|
-
# @see #save! See #save! for available options.
|
56
|
-
#
|
57
|
-
def save(*args)
|
58
|
-
save! *args
|
59
|
-
true
|
60
|
-
rescue Acfs::Error
|
61
|
-
false
|
62
|
-
end
|
63
|
-
|
64
|
-
# @api public
|
65
|
-
#
|
66
|
-
# Saves the resource. Raises an error if something happens.
|
67
|
-
#
|
68
|
-
# Saving a resource is a synchronous operation.
|
69
|
-
#
|
70
|
-
# @param [Hash] opts Hash with additional options.
|
71
|
-
# @option opts [Hash] :data Data to send to remote service. Default will be resource attributes.
|
72
|
-
#
|
73
|
-
# @raise [Acfs::InvalidResource]
|
74
|
-
# If remote services respond with 422 response. Will fill errors with data from response
|
75
|
-
# @raise [Acfs::ErroneousResponse]
|
76
|
-
# If remote service respond with not successful response.
|
77
|
-
#
|
78
|
-
# @see #save
|
79
|
-
#
|
80
|
-
def save!(opts = {})
|
81
|
-
opts[:data] = attributes unless opts[:data]
|
82
|
-
|
83
|
-
operation (new? ? :create : :update), opts do |data|
|
84
|
-
update_with data
|
85
|
-
end
|
86
|
-
rescue InvalidResource => err
|
87
|
-
self.remote_errors = err.errors
|
88
|
-
raise err
|
89
|
-
end
|
90
|
-
|
91
|
-
# @api public
|
92
|
-
#
|
93
|
-
# Update attributes with given data and save resource.
|
94
|
-
#
|
95
|
-
# Saving a resource is a synchronous operation.
|
96
|
-
#
|
97
|
-
# @param [Hash] attrs Hash with attributes to write.
|
98
|
-
# @param [Hash] opts Options passed to `save`.
|
99
|
-
#
|
100
|
-
# @return [Boolean] True if save operation was successful, false otherwise.
|
101
|
-
#
|
102
|
-
# @see #save
|
103
|
-
# @see #attributes=
|
104
|
-
# @see #update_attributes!
|
105
|
-
#
|
106
|
-
def update_attributes(attrs, opts = {})
|
107
|
-
check_loaded! opts
|
108
|
-
|
109
|
-
self.attributes = attrs
|
110
|
-
save opts
|
111
|
-
end
|
112
|
-
|
113
|
-
# @api public
|
114
|
-
#
|
115
|
-
# Update attributes with given data and save resource.
|
116
|
-
#
|
117
|
-
# Saving a resource is a synchronous operation.
|
118
|
-
#
|
119
|
-
# @param [Hash] attrs Hash with attributes to write.
|
120
|
-
# @param [Hash] opts Options passed to `save!`.
|
121
|
-
#
|
122
|
-
# @raise [Acfs::InvalidResource]
|
123
|
-
# If remote services respond with 422 response. Will fill errors with data from response
|
124
|
-
# @raise [Acfs::ErroneousResponse]
|
125
|
-
# If remote service respond with not successful response.
|
126
|
-
#
|
127
|
-
# @see #save!
|
128
|
-
# @see #attributes=
|
129
|
-
# @see #update_attributes
|
130
|
-
#
|
131
|
-
def update_attributes!(attrs, opts = {})
|
132
|
-
check_loaded! opts
|
133
|
-
|
134
|
-
self.attributes = attrs
|
135
|
-
save! opts
|
136
|
-
end
|
137
|
-
|
138
|
-
# @api public
|
139
|
-
#
|
140
|
-
# Destroy resource by sending a DELETE request.
|
141
|
-
#
|
142
|
-
# Deleting a resource is a synchronous operation.
|
143
|
-
#
|
144
|
-
# @return [Boolean]
|
145
|
-
# @see #delete!
|
146
|
-
#
|
147
|
-
def delete(opts = {})
|
148
|
-
delete! opts
|
149
|
-
true
|
150
|
-
rescue Acfs::Error
|
151
|
-
false
|
152
|
-
end
|
153
|
-
|
154
|
-
# @api public
|
155
|
-
#
|
156
|
-
# Destroy resource by sending a DELETE request.
|
157
|
-
# Will raise an error in case something goes wrong.
|
158
|
-
#
|
159
|
-
# Deleting a resource is a synchronous operation.
|
160
|
-
|
161
|
-
# @raise [Acfs::ErroneousResponse]
|
162
|
-
# If remote service respond with not successful response.
|
163
|
-
# @return [undefined]
|
164
|
-
# @see #delete
|
165
|
-
#
|
166
|
-
def delete!(opts = {})
|
167
|
-
opts[:params] ||= {}
|
168
|
-
opts[:params] = attributes_for_url(:delete).merge opts[:params]
|
169
|
-
|
170
|
-
operation :delete, opts do |data|
|
171
|
-
update_with data
|
172
|
-
freeze
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
private
|
177
|
-
def attributes_for_url(action)
|
178
|
-
arguments_for_url = self.class.location(action: action).arguments
|
179
|
-
attributes.slice(*arguments_for_url)
|
180
|
-
end
|
181
|
-
|
182
|
-
module ClassMethods
|
183
|
-
|
184
|
-
# @api public
|
185
|
-
#
|
186
|
-
# Create a new resource sending given data. If resource cannot be
|
187
|
-
# created an error will be thrown.
|
188
|
-
#
|
189
|
-
# Saving a resource is a synchronous operation.
|
190
|
-
#
|
191
|
-
# @param [Hash{Symbol, String => Object}] data Data to send in create request.
|
192
|
-
# @return [self] Newly resource object.
|
193
|
-
#
|
194
|
-
# @raise [Acfs::InvalidResource]
|
195
|
-
# If remote services respond with 422 response. Will fill errors with data from response
|
196
|
-
# @raise [Acfs::ErroneousResponse]
|
197
|
-
# If remote service respond with not successful response.
|
198
|
-
#
|
199
|
-
# @see Acfs::Model::Persistence#save! Available options. `:data` will be overridden with provided data hash.
|
200
|
-
# @see #create
|
201
|
-
#
|
202
|
-
def create!(data, opts = {})
|
203
|
-
new(data).tap do |model|
|
204
|
-
model.save!
|
205
|
-
end
|
206
|
-
end
|
207
|
-
|
208
|
-
# @api public
|
209
|
-
#
|
210
|
-
# Create a new resource sending given data. If resource cannot be
|
211
|
-
# create model will be returned and error hash contains response
|
212
|
-
# errors if available.
|
213
|
-
#
|
214
|
-
# Saving a resource is a synchronous operation.
|
215
|
-
#
|
216
|
-
# @param [Hash{Symbol, String => Object}] data Data to send in create request.
|
217
|
-
# @return [self] Newly resource object.
|
218
|
-
#
|
219
|
-
# @raise [Acfs::ErroneousResponse]
|
220
|
-
# If remote service respond with not successful response.
|
221
|
-
#
|
222
|
-
# @see Acfs::Model::Persistence#save! Available options. `:data` will be overridden with provided data hash.
|
223
|
-
# @see #create!
|
224
|
-
#
|
225
|
-
def create(data, opts = {})
|
226
|
-
model = new data
|
227
|
-
model.save
|
228
|
-
model
|
229
|
-
end
|
230
|
-
end
|
231
|
-
|
232
|
-
private
|
233
|
-
def update_with(data)
|
234
|
-
self.attributes = data
|
235
|
-
loaded!
|
236
|
-
end
|
237
|
-
|
238
|
-
def check_loaded!(opts = {})
|
239
|
-
raise ResourceNotLoaded.new resource: self unless loaded? or opts[:force]
|
240
|
-
end
|
241
|
-
end
|
242
|
-
end
|
243
|
-
end
|