acfs 1.3.2 → 1.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- metadata +8 -141
- data/CHANGELOG.md +0 -325
- data/LICENSE +0 -22
- data/README.md +0 -334
- data/acfs.gemspec +0 -37
- data/lib/acfs.rb +0 -49
- data/lib/acfs/adapter/base.rb +0 -26
- data/lib/acfs/adapter/typhoeus.rb +0 -60
- data/lib/acfs/collection.rb +0 -27
- data/lib/acfs/collections/paginatable.rb +0 -75
- data/lib/acfs/configuration.rb +0 -113
- data/lib/acfs/errors.rb +0 -106
- data/lib/acfs/global.rb +0 -99
- data/lib/acfs/location.rb +0 -78
- data/lib/acfs/middleware/base.rb +0 -20
- data/lib/acfs/middleware/json.rb +0 -27
- data/lib/acfs/middleware/logger.rb +0 -23
- data/lib/acfs/middleware/msgpack.rb +0 -30
- data/lib/acfs/middleware/print.rb +0 -21
- data/lib/acfs/middleware/serializer.rb +0 -39
- data/lib/acfs/operation.rb +0 -81
- data/lib/acfs/request.rb +0 -36
- data/lib/acfs/request/callbacks.rb +0 -50
- data/lib/acfs/resource.rb +0 -37
- data/lib/acfs/resource/attributes.rb +0 -268
- data/lib/acfs/resource/attributes/base.rb +0 -28
- data/lib/acfs/resource/attributes/boolean.rb +0 -37
- data/lib/acfs/resource/attributes/date_time.rb +0 -31
- data/lib/acfs/resource/attributes/dict.rb +0 -37
- data/lib/acfs/resource/attributes/float.rb +0 -31
- data/lib/acfs/resource/attributes/integer.rb +0 -27
- data/lib/acfs/resource/attributes/list.rb +0 -34
- data/lib/acfs/resource/attributes/string.rb +0 -24
- data/lib/acfs/resource/attributes/uuid.rb +0 -47
- data/lib/acfs/resource/dirty.rb +0 -35
- data/lib/acfs/resource/initialization.rb +0 -29
- data/lib/acfs/resource/loadable.rb +0 -33
- data/lib/acfs/resource/locatable.rb +0 -128
- data/lib/acfs/resource/operational.rb +0 -22
- data/lib/acfs/resource/persistence.rb +0 -257
- data/lib/acfs/resource/query_methods.rb +0 -264
- data/lib/acfs/resource/service.rb +0 -42
- data/lib/acfs/resource/validation.rb +0 -37
- data/lib/acfs/response.rb +0 -28
- data/lib/acfs/response/formats.rb +0 -25
- data/lib/acfs/response/status.rb +0 -31
- data/lib/acfs/rspec.rb +0 -11
- data/lib/acfs/runner.rb +0 -97
- data/lib/acfs/service.rb +0 -91
- data/lib/acfs/service/middleware.rb +0 -56
- data/lib/acfs/service/middleware/stack.rb +0 -63
- data/lib/acfs/singleton_resource.rb +0 -83
- data/lib/acfs/stub.rb +0 -172
- data/lib/acfs/util.rb +0 -20
- data/lib/acfs/version.rb +0 -14
- data/lib/acfs/yard.rb +0 -5
- data/spec/acfs/adapter/typhoeus_spec.rb +0 -28
- data/spec/acfs/collection_spec.rb +0 -155
- data/spec/acfs/configuration_spec.rb +0 -51
- data/spec/acfs/global_spec.rb +0 -137
- data/spec/acfs/location_spec.rb +0 -23
- data/spec/acfs/middleware/json_spec.rb +0 -63
- data/spec/acfs/middleware/msgpack_spec.rb +0 -60
- data/spec/acfs/operation_spec.rb +0 -10
- data/spec/acfs/request/callbacks_spec.rb +0 -46
- data/spec/acfs/request_spec.rb +0 -77
- data/spec/acfs/resource/attributes/boolean_spec.rb +0 -56
- data/spec/acfs/resource/attributes/date_time_spec.rb +0 -49
- data/spec/acfs/resource/attributes/dict_spec.rb +0 -75
- data/spec/acfs/resource/attributes/float_spec.rb +0 -59
- data/spec/acfs/resource/attributes/integer_spec.rb +0 -34
- data/spec/acfs/resource/attributes/list_spec.rb +0 -58
- data/spec/acfs/resource/attributes/uuid_spec.rb +0 -40
- data/spec/acfs/resource/attributes_spec.rb +0 -179
- data/spec/acfs/resource/dirty_spec.rb +0 -47
- data/spec/acfs/resource/initialization_spec.rb +0 -30
- data/spec/acfs/resource/loadable_spec.rb +0 -20
- data/spec/acfs/resource/locatable_spec.rb +0 -116
- data/spec/acfs/resource/persistance_spec.rb +0 -316
- data/spec/acfs/resource/query_methods_spec.rb +0 -541
- data/spec/acfs/resource/validation_spec.rb +0 -127
- data/spec/acfs/response/formats_spec.rb +0 -50
- data/spec/acfs/response/status_spec.rb +0 -69
- data/spec/acfs/runner_spec.rb +0 -97
- data/spec/acfs/service/middleware_spec.rb +0 -33
- data/spec/acfs/service_spec.rb +0 -46
- data/spec/acfs/singleton_resource_spec.rb +0 -15
- data/spec/acfs/stub_spec.rb +0 -343
- data/spec/acfs_spec.rb +0 -203
- data/spec/fixtures/config.yml +0 -14
- data/spec/spec_helper.rb +0 -41
- data/spec/support/hash.rb +0 -9
- data/spec/support/response.rb +0 -10
- data/spec/support/service.rb +0 -91
- data/spec/support/shared/find_callbacks.rb +0 -48
@@ -1,34 +0,0 @@
|
|
1
|
-
module Acfs::Resource::Attributes
|
2
|
-
# @api public
|
3
|
-
#
|
4
|
-
# List attribute type. Use it in your model as an attribute type:
|
5
|
-
#
|
6
|
-
# @example
|
7
|
-
# class User < Acfs::Resource
|
8
|
-
# attribute :name, :list
|
9
|
-
# end
|
10
|
-
#
|
11
|
-
class List < Base
|
12
|
-
# @api public
|
13
|
-
#
|
14
|
-
# Cast given object to a list.
|
15
|
-
#
|
16
|
-
# @param [Object] value Object to cast.
|
17
|
-
# @return [Fixnum] Casted object as list.
|
18
|
-
# @raise [TypeError] If object cannot be casted to a list.
|
19
|
-
#
|
20
|
-
def cast_value(value)
|
21
|
-
return [] if value.blank?
|
22
|
-
|
23
|
-
if value.is_a?(::Array)
|
24
|
-
value
|
25
|
-
elsif value.respond_to?(:to_ary)
|
26
|
-
value.to_ary
|
27
|
-
elsif value.respond_to?(:to_a)
|
28
|
-
value.to_a
|
29
|
-
else
|
30
|
-
Array(value)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
module Acfs::Resource::Attributes
|
2
|
-
# @api public
|
3
|
-
#
|
4
|
-
# String attribute type. Use it in your model as
|
5
|
-
# an attribute type:
|
6
|
-
#
|
7
|
-
# @example
|
8
|
-
# class User < Acfs::Resource
|
9
|
-
# attribute :name, :string
|
10
|
-
# end
|
11
|
-
#
|
12
|
-
class String < Base
|
13
|
-
# @api public
|
14
|
-
#
|
15
|
-
# Cast given object to string.
|
16
|
-
#
|
17
|
-
# @param [Object] value Object to cast.
|
18
|
-
# @return [String] Casted string.
|
19
|
-
#
|
20
|
-
def cast_value(value)
|
21
|
-
value.to_s
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
module Acfs::Resource::Attributes
|
2
|
-
# @api public
|
3
|
-
#
|
4
|
-
# UUID attribute type. Use it in your model as an attribute type:
|
5
|
-
#
|
6
|
-
# @example
|
7
|
-
# class User < Acfs::Resource
|
8
|
-
# attribute :id, :uuid
|
9
|
-
# end
|
10
|
-
#
|
11
|
-
class UUID < Base
|
12
|
-
#
|
13
|
-
UUID_REGEXP = /[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}/i
|
14
|
-
|
15
|
-
# @api public
|
16
|
-
#
|
17
|
-
# Check if given object looks like a UUID, eg:
|
18
|
-
# `450b7a40-94ad-11e3-baa8-0800200c9a66`
|
19
|
-
# Valid UUIDs are 16 byte numbers represented as
|
20
|
-
# a hexadecimal string in five sub-groups seperated
|
21
|
-
# by a dash. Each group has to consist of a fixed
|
22
|
-
# number of hexadecimal digits:
|
23
|
-
# | Group | Digits |
|
24
|
-
# | -----:|:------ |
|
25
|
-
# | 1 | 8 |
|
26
|
-
# | 2 | 4 |
|
27
|
-
# | 3 | 4 |
|
28
|
-
# | 4 | 4 |
|
29
|
-
# | 5 | 12 |
|
30
|
-
#
|
31
|
-
# @param [Object] value Object to cast.
|
32
|
-
# @return [String] Casted object as UUID.
|
33
|
-
#
|
34
|
-
def cast_value(value)
|
35
|
-
if value.blank?
|
36
|
-
nil
|
37
|
-
elsif value.to_s =~ UUID_REGEXP
|
38
|
-
value
|
39
|
-
else
|
40
|
-
raise TypeError.new "Invalid UUID: `#{value.to_s}'"
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
# Lower-case alias for automatic type lookup
|
46
|
-
Uuid = UUID
|
47
|
-
end
|
data/lib/acfs/resource/dirty.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
class Acfs::Resource
|
2
|
-
#
|
3
|
-
# Thin wrapper around ActiveModel::Dirty
|
4
|
-
#
|
5
|
-
module Dirty
|
6
|
-
extend ActiveSupport::Concern
|
7
|
-
include ActiveModel::Dirty
|
8
|
-
|
9
|
-
# @api private
|
10
|
-
#
|
11
|
-
def reset_changes
|
12
|
-
clear_changes_information
|
13
|
-
end
|
14
|
-
|
15
|
-
# @api private
|
16
|
-
#
|
17
|
-
def save!(*)
|
18
|
-
super.tap {|_| changes_applied }
|
19
|
-
end
|
20
|
-
|
21
|
-
# @api private
|
22
|
-
#
|
23
|
-
def loaded!
|
24
|
-
reset_changes
|
25
|
-
super
|
26
|
-
end
|
27
|
-
|
28
|
-
# @api private
|
29
|
-
#
|
30
|
-
def write_raw_attribute(name, value, opts = {})
|
31
|
-
attribute_will_change!(name) if opts[:change].nil? || opts[:change]
|
32
|
-
super
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
class Acfs::Resource
|
2
|
-
#
|
3
|
-
# Initialization drop-in for pre-4.0 ActiveModel.
|
4
|
-
#
|
5
|
-
module Initialization
|
6
|
-
#
|
7
|
-
# @api public
|
8
|
-
#
|
9
|
-
# Initializes a new model with the given `params`. fff
|
10
|
-
#
|
11
|
-
# @example
|
12
|
-
# class User < Acfs::Resource
|
13
|
-
# attribute :name
|
14
|
-
# attribute :email, default: ->{ "#{name}@dom.tld" }
|
15
|
-
# attribute :age, :integer, default: 18
|
16
|
-
# end
|
17
|
-
#
|
18
|
-
# user = User.new(name: 'bob')
|
19
|
-
# user.name # => "bob"
|
20
|
-
# user.email # => "bob@dom.tld"
|
21
|
-
# user.age # => 18
|
22
|
-
#
|
23
|
-
# @param params [Hash{Symbol => Object}] Attributes to set on resource.
|
24
|
-
#
|
25
|
-
def initialize(params = {})
|
26
|
-
write_attributes params if params
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
class Acfs::Resource
|
2
|
-
# Provides method to check for loading state of resources.
|
3
|
-
# A resource that is created but not yet fetched will be loaded
|
4
|
-
# after running {Acfs::Global#run Acfs.run}.
|
5
|
-
#
|
6
|
-
# @example
|
7
|
-
# user = User.find 5
|
8
|
-
# user.loaded? # => false
|
9
|
-
# Acfs.run
|
10
|
-
# user.loaded? # => true
|
11
|
-
#
|
12
|
-
module Loadable
|
13
|
-
extend ActiveSupport::Concern
|
14
|
-
|
15
|
-
# @api public
|
16
|
-
#
|
17
|
-
# Check if model is loaded or if request is still queued.
|
18
|
-
#
|
19
|
-
# @return [Boolean] True if resource is loaded, false otherwise.
|
20
|
-
#
|
21
|
-
def loaded?
|
22
|
-
@loaded.nil? ? false : @loaded
|
23
|
-
end
|
24
|
-
|
25
|
-
# @api private
|
26
|
-
#
|
27
|
-
# Mark model as loaded.
|
28
|
-
#
|
29
|
-
def loaded!
|
30
|
-
@loaded = true
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
@@ -1,128 +0,0 @@
|
|
1
|
-
class Acfs::Resource
|
2
|
-
# Provide methods for generation URLs for resources.
|
3
|
-
#
|
4
|
-
# @example
|
5
|
-
# class User
|
6
|
-
# service AccountService # With base URL `http://acc.svr`
|
7
|
-
# end
|
8
|
-
# User.url # => "http://acc.svr/users"
|
9
|
-
# User.url(5) # => "http://acc.svr/users/5"
|
10
|
-
#
|
11
|
-
module Locatable
|
12
|
-
extend ActiveSupport::Concern
|
13
|
-
|
14
|
-
#
|
15
|
-
module ClassMethods
|
16
|
-
# @overload url(suffix)
|
17
|
-
# @deprecated
|
18
|
-
# Return URL for this class of resource. Given suffix
|
19
|
-
# will be appended.
|
20
|
-
#
|
21
|
-
# @example
|
22
|
-
# User.url # => "http://users.srv.org/users"
|
23
|
-
# User.url(5) # => "http://users.srv.org/users/5"
|
24
|
-
#
|
25
|
-
# @param suffix [String] Suffix to append to URL.
|
26
|
-
# @return [String] Generated URL.
|
27
|
-
#
|
28
|
-
# @overload url(opts = {})
|
29
|
-
# Return URL for this class of resources. Given options
|
30
|
-
# will be used to replace URL path arguments and to
|
31
|
-
# determine the operation action.
|
32
|
-
#
|
33
|
-
# @example
|
34
|
-
# User.url(id: 5, action: :read) # => "http://users.srv.org/users/5"
|
35
|
-
# User.url(action: :list) # => "http://users.srv.org/users"
|
36
|
-
#
|
37
|
-
# @param opts [Hash] Options.
|
38
|
-
# @option opts [Symbol] :action Operation action,
|
39
|
-
# usually `:list`, `:create`, `:read`, `:update` or`:delete`.
|
40
|
-
# @return [String] Generated URL.
|
41
|
-
#
|
42
|
-
def url(suffix = nil, opts = {})
|
43
|
-
opts, suffix = suffix, nil if suffix.is_a? Hash
|
44
|
-
|
45
|
-
opts[:action] = :list if suffix
|
46
|
-
|
47
|
-
url = location(opts).build(opts).str
|
48
|
-
url += "/#{suffix}" if suffix.to_s.present?
|
49
|
-
url
|
50
|
-
end
|
51
|
-
|
52
|
-
# Return a location object able to build the URL for this
|
53
|
-
# resource and given action.
|
54
|
-
#
|
55
|
-
# @example
|
56
|
-
# class Identity < ::Acfs::Resource
|
57
|
-
# service MyService, path: 'users/:user_id/identities'
|
58
|
-
# end
|
59
|
-
#
|
60
|
-
# location = Identity.location(action: :read)
|
61
|
-
# location.arguments
|
62
|
-
# => [:user_id, :id]
|
63
|
-
#
|
64
|
-
# location.raw_url
|
65
|
-
# => 'http://service/users/:user_id/identities/:id'
|
66
|
-
#
|
67
|
-
# location = Identity.location(action: :list)
|
68
|
-
# location.arguments
|
69
|
-
# => [:user_id]
|
70
|
-
#
|
71
|
-
# location.build(user_id: 42)
|
72
|
-
# => 'http://service/users/42/identities'
|
73
|
-
#
|
74
|
-
# @param opts [Hash] Options.
|
75
|
-
# @option opts [Symbol] :action Operation action,
|
76
|
-
# usually `:list`, `:create`, `:read`, `:update` or`:delete`.
|
77
|
-
#
|
78
|
-
# @return [Location] Location object.
|
79
|
-
#
|
80
|
-
def location(opts = {})
|
81
|
-
service.location(self, opts)
|
82
|
-
end
|
83
|
-
|
84
|
-
# @api private
|
85
|
-
def location_default_path(action, path)
|
86
|
-
case action
|
87
|
-
when :list, :create
|
88
|
-
return path
|
89
|
-
when :read, :update, :delete
|
90
|
-
return "#{path}/:id"
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
# Return URL for this resource. Resource if will be appended
|
96
|
-
# as suffix if present.
|
97
|
-
#
|
98
|
-
# @example
|
99
|
-
# user.new.url # => "http://users.srv.org/users"
|
100
|
-
#
|
101
|
-
# user = User.find 5
|
102
|
-
# Acfs.run
|
103
|
-
# user.url # => "http://users.srv.org/users/5"
|
104
|
-
#
|
105
|
-
# @return [ String ] Generated URL.
|
106
|
-
# @see ClassMethods#url
|
107
|
-
#
|
108
|
-
def url(opts = {})
|
109
|
-
return nil if need_primary_key? && !primary_key?
|
110
|
-
|
111
|
-
self.class.service
|
112
|
-
.location(self.class, opts.reverse_merge(action: :read))
|
113
|
-
.build(attributes).str
|
114
|
-
end
|
115
|
-
|
116
|
-
# @api private
|
117
|
-
# Return true if resource needs a primary key (id) for singular actions.
|
118
|
-
def need_primary_key?
|
119
|
-
true
|
120
|
-
end
|
121
|
-
|
122
|
-
# @api private
|
123
|
-
# Return true if resource has a primary key (id) set.
|
124
|
-
def primary_key?
|
125
|
-
respond_to?(:id) && !id.nil?
|
126
|
-
end
|
127
|
-
end
|
128
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
class Acfs::Resource
|
2
|
-
# @api private
|
3
|
-
#
|
4
|
-
# Provide methods for creating and processing CRUD operations and
|
5
|
-
# handling responses. That includes error handling as well as
|
6
|
-
# handling stubbed resources.
|
7
|
-
#
|
8
|
-
# Should only be used internal.
|
9
|
-
#
|
10
|
-
module Operational
|
11
|
-
extend ActiveSupport::Concern
|
12
|
-
delegate :operation, to: :'self.class'
|
13
|
-
|
14
|
-
#
|
15
|
-
module ClassMethods
|
16
|
-
# Invoke CRUD operation.
|
17
|
-
def operation(action, opts = {}, &block)
|
18
|
-
Acfs.runner.process ::Acfs::Operation.new self, action, opts, &block
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
@@ -1,257 +0,0 @@
|
|
1
|
-
class Acfs::Resource
|
2
|
-
#
|
3
|
-
# Allow to track the persistence state of a model.
|
4
|
-
#
|
5
|
-
module Persistence
|
6
|
-
extend ActiveSupport::Concern
|
7
|
-
|
8
|
-
# @api public
|
9
|
-
#
|
10
|
-
# Check if the model is persisted. A model is persisted if
|
11
|
-
# it is saved after being created
|
12
|
-
#
|
13
|
-
# @example Newly created resource:
|
14
|
-
# user = User.new name: "John"
|
15
|
-
# user.persisted? # => false
|
16
|
-
# user.save
|
17
|
-
# user.persisted? # => true
|
18
|
-
#
|
19
|
-
# @example Modified resource:
|
20
|
-
# user2 = User.find 5
|
21
|
-
# user2.persisted? # => true
|
22
|
-
# user2.name = 'Amy'
|
23
|
-
# user2.persisted? # => true
|
24
|
-
# user2.save
|
25
|
-
# user2.persisted? # => true
|
26
|
-
#
|
27
|
-
# @return [Boolean] True if resource has been saved
|
28
|
-
#
|
29
|
-
def persisted?
|
30
|
-
!new?
|
31
|
-
end
|
32
|
-
|
33
|
-
# @api public
|
34
|
-
#
|
35
|
-
# Return true if model is a new record and was not saved yet.
|
36
|
-
#
|
37
|
-
# @return [Boolean] True if resource is newly created,
|
38
|
-
# false otherwise.
|
39
|
-
#
|
40
|
-
def new?
|
41
|
-
!loaded?
|
42
|
-
end
|
43
|
-
alias_method :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,
|
55
|
-
# false otherwise.
|
56
|
-
# @see #save! See {#save!} for available options.
|
57
|
-
#
|
58
|
-
def save(*args)
|
59
|
-
save!(*args)
|
60
|
-
true
|
61
|
-
rescue Acfs::Error
|
62
|
-
false
|
63
|
-
end
|
64
|
-
|
65
|
-
# @api public
|
66
|
-
#
|
67
|
-
# Saves the resource. Raises an error if something happens.
|
68
|
-
#
|
69
|
-
# Saving a resource is a synchronous operation.
|
70
|
-
#
|
71
|
-
# @param opts [Hash] Hash with additional options.
|
72
|
-
# @option opts [Hash] :data Data to send to remote service.
|
73
|
-
# Default will be resource attributes.
|
74
|
-
#
|
75
|
-
# @raise [Acfs::InvalidResource]
|
76
|
-
# If remote services respond with 422 response. Will fill
|
77
|
-
# errors with data from response
|
78
|
-
# @raise [Acfs::ErroneousResponse]
|
79
|
-
# If remote service respond with not successful response.
|
80
|
-
#
|
81
|
-
# @see #save
|
82
|
-
#
|
83
|
-
def save!(opts = {})
|
84
|
-
opts[:data] = attributes unless opts[:data]
|
85
|
-
|
86
|
-
operation((new? ? :create : :update), opts) do |data|
|
87
|
-
update_with data
|
88
|
-
end
|
89
|
-
rescue ::Acfs::InvalidResource => err
|
90
|
-
self.remote_errors = err.errors
|
91
|
-
raise err
|
92
|
-
end
|
93
|
-
|
94
|
-
# @api public
|
95
|
-
#
|
96
|
-
# Update attributes with given data and save resource.
|
97
|
-
#
|
98
|
-
# Saving a resource is a synchronous operation.
|
99
|
-
#
|
100
|
-
# @param attrs [Hash] Hash with attributes to write.
|
101
|
-
# @param opts [Hash] Options passed to `save`.
|
102
|
-
#
|
103
|
-
# @return [Boolean]
|
104
|
-
# True if save operation was successful, false otherwise.
|
105
|
-
#
|
106
|
-
# @see #save
|
107
|
-
# @see #attributes=
|
108
|
-
# @see #update_attributes!
|
109
|
-
#
|
110
|
-
def update_attributes(attrs, opts = {})
|
111
|
-
check_loaded! opts
|
112
|
-
|
113
|
-
self.attributes = attrs
|
114
|
-
save opts
|
115
|
-
end
|
116
|
-
|
117
|
-
# @api public
|
118
|
-
#
|
119
|
-
# Update attributes with given data and save resource.
|
120
|
-
#
|
121
|
-
# Saving a resource is a synchronous operation.
|
122
|
-
#
|
123
|
-
# @param [Hash] attrs Hash with attributes to write.
|
124
|
-
# @param [Hash] opts Options passed to `save!`.
|
125
|
-
#
|
126
|
-
# @raise [Acfs::InvalidResource]
|
127
|
-
# If remote services respond with 422 response. Will fill
|
128
|
-
# errors with data from response
|
129
|
-
#
|
130
|
-
# @raise [Acfs::ErroneousResponse]
|
131
|
-
# If remote service respond with not successful response.
|
132
|
-
#
|
133
|
-
# @see #save!
|
134
|
-
# @see #attributes=
|
135
|
-
# @see #update_attributes
|
136
|
-
#
|
137
|
-
def update_attributes!(attrs, opts = {})
|
138
|
-
check_loaded! opts
|
139
|
-
|
140
|
-
self.attributes = attrs
|
141
|
-
save! opts
|
142
|
-
end
|
143
|
-
|
144
|
-
# @api public
|
145
|
-
#
|
146
|
-
# Destroy resource by sending a DELETE request.
|
147
|
-
#
|
148
|
-
# Deleting a resource is a synchronous operation.
|
149
|
-
#
|
150
|
-
# @return [Boolean]
|
151
|
-
# @see #delete!
|
152
|
-
#
|
153
|
-
def delete(opts = {})
|
154
|
-
delete! opts
|
155
|
-
true
|
156
|
-
rescue Acfs::Error
|
157
|
-
false
|
158
|
-
end
|
159
|
-
|
160
|
-
# @api public
|
161
|
-
#
|
162
|
-
# Destroy resource by sending a DELETE request.
|
163
|
-
# Will raise an error in case something goes wrong.
|
164
|
-
#
|
165
|
-
# Deleting a resource is a synchronous operation.
|
166
|
-
|
167
|
-
# @raise [Acfs::ErroneousResponse]
|
168
|
-
# If remote service respond with not successful response.
|
169
|
-
#
|
170
|
-
# @return [undefined]
|
171
|
-
# @see #delete
|
172
|
-
#
|
173
|
-
def delete!(opts = {})
|
174
|
-
opts[:params] ||= {}
|
175
|
-
opts[:params] = attributes_for_url(:delete).merge opts[:params]
|
176
|
-
|
177
|
-
operation :delete, opts do |data|
|
178
|
-
update_with data
|
179
|
-
freeze
|
180
|
-
end
|
181
|
-
end
|
182
|
-
|
183
|
-
private
|
184
|
-
|
185
|
-
def attributes_for_url(action)
|
186
|
-
arguments_for_url = self.class.location(action: action).arguments
|
187
|
-
attributes.slice(*arguments_for_url)
|
188
|
-
end
|
189
|
-
|
190
|
-
module ClassMethods
|
191
|
-
# @api public
|
192
|
-
#
|
193
|
-
# Create a new resource sending given data. If resource cannot be
|
194
|
-
# created an error will be thrown.
|
195
|
-
#
|
196
|
-
# Saving a resource is a synchronous operation.
|
197
|
-
#
|
198
|
-
# @param data [Hash{Symbol, String => Object}]
|
199
|
-
# Data to send in create request.
|
200
|
-
#
|
201
|
-
# @return [self] Newly resource object.
|
202
|
-
#
|
203
|
-
# @raise [Acfs::InvalidResource]
|
204
|
-
# If remote services respond with 422 response. Will fill
|
205
|
-
# errors with data from response
|
206
|
-
#
|
207
|
-
# @raise [Acfs::ErroneousResponse]
|
208
|
-
# If remote service respond with not successful response.
|
209
|
-
#
|
210
|
-
# @see Acfs::Model::Persistence#save! Available options. `:data`
|
211
|
-
# will be overridden with provided data hash.
|
212
|
-
# @see #create
|
213
|
-
#
|
214
|
-
def create!(data, _opts = {})
|
215
|
-
new(data).tap(&:save!)
|
216
|
-
end
|
217
|
-
|
218
|
-
# @api public
|
219
|
-
#
|
220
|
-
# Create a new resource sending given data. If resource cannot be
|
221
|
-
# create model will be returned and error hash contains response
|
222
|
-
# errors if available.
|
223
|
-
#
|
224
|
-
# Saving a resource is a synchronous operation.
|
225
|
-
#
|
226
|
-
# @param data [Hash{Symbol, String => Object}]
|
227
|
-
# Data to send in create request.
|
228
|
-
#
|
229
|
-
# @return [self] Newly resource object.
|
230
|
-
#
|
231
|
-
# @raise [Acfs::ErroneousResponse]
|
232
|
-
# If remote service respond with not successful response.
|
233
|
-
#
|
234
|
-
# @see Acfs::Model::Persistence#save! Available options. `:data`
|
235
|
-
# will be overridden with provided data hash.
|
236
|
-
# @see #create!
|
237
|
-
#
|
238
|
-
def create(data, _opts = {})
|
239
|
-
model = new data
|
240
|
-
model.save
|
241
|
-
model
|
242
|
-
end
|
243
|
-
end
|
244
|
-
|
245
|
-
private
|
246
|
-
|
247
|
-
def update_with(data)
|
248
|
-
self.attributes = data
|
249
|
-
loaded!
|
250
|
-
end
|
251
|
-
|
252
|
-
def check_loaded!(opts = {})
|
253
|
-
return if loaded? || opts[:force]
|
254
|
-
raise ::Acfs::ResourceNotLoaded.new resource: self
|
255
|
-
end
|
256
|
-
end
|
257
|
-
end
|