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,268 +0,0 @@
|
|
1
|
-
class Acfs::Resource
|
2
|
-
#
|
3
|
-
# = Acfs Attributes
|
4
|
-
#
|
5
|
-
# Allows to specify attributes of a class with default
|
6
|
-
# values and type safety.
|
7
|
-
#
|
8
|
-
# @example
|
9
|
-
# class User < Acfs::Resource
|
10
|
-
# attribute :name, :string, default: 'Anon'
|
11
|
-
# attribute :age, :integer
|
12
|
-
# attribute :special, My::Special::Type
|
13
|
-
# end
|
14
|
-
#
|
15
|
-
# For each attribute a setter and getter will be created and values will be
|
16
|
-
# type casted when set.
|
17
|
-
#
|
18
|
-
module Attributes
|
19
|
-
extend ActiveSupport::Concern
|
20
|
-
include ActiveModel::AttributeMethods
|
21
|
-
|
22
|
-
# @api public
|
23
|
-
#
|
24
|
-
# Write default attributes defined in resource class.
|
25
|
-
#
|
26
|
-
# @see #write_attributes
|
27
|
-
# @see ClassMethods#attributes
|
28
|
-
#
|
29
|
-
def initialize(*attrs)
|
30
|
-
write_attributes self.class.attributes
|
31
|
-
reset_changes
|
32
|
-
super
|
33
|
-
end
|
34
|
-
|
35
|
-
# @api public
|
36
|
-
#
|
37
|
-
# Returns ActiveModel compatible list of attributes and values.
|
38
|
-
#
|
39
|
-
# @example
|
40
|
-
# class User < Acfs::Resource
|
41
|
-
# attribute :name, type: String, default: 'Anon'
|
42
|
-
# end
|
43
|
-
# user = User.new(name: 'John')
|
44
|
-
# user.attributes # => { "name" => "John" }
|
45
|
-
#
|
46
|
-
# @return [HashWithIndifferentAccess{Symbol => Object}]
|
47
|
-
# Attributes and their values.
|
48
|
-
#
|
49
|
-
def attributes
|
50
|
-
@_attrs ||= HashWithIndifferentAccess.new
|
51
|
-
end
|
52
|
-
|
53
|
-
# @api public
|
54
|
-
#
|
55
|
-
# Update all attributes with given hash. Attribute values will be casted
|
56
|
-
# to defined attribute type.
|
57
|
-
#
|
58
|
-
# @example
|
59
|
-
# user.attributes = { :name => 'Adam' }
|
60
|
-
# user.name # => 'Adam'
|
61
|
-
#
|
62
|
-
# @param [Hash{String, Symbol => Object}, #each{|key, value|}]
|
63
|
-
# Attributes to set in resource.
|
64
|
-
# @see #write_attributes Delegates attributes hash to {#write_attributes}.
|
65
|
-
#
|
66
|
-
def attributes=(attributes)
|
67
|
-
write_attributes attributes
|
68
|
-
end
|
69
|
-
|
70
|
-
# @api public
|
71
|
-
#
|
72
|
-
# Read an attribute from instance variable.
|
73
|
-
#
|
74
|
-
# @param [Symbol, String] name Attribute name.
|
75
|
-
# @return [Object] Attribute value.
|
76
|
-
#
|
77
|
-
def read_attribute(name)
|
78
|
-
attributes[name.to_s]
|
79
|
-
end
|
80
|
-
|
81
|
-
# @api public
|
82
|
-
#
|
83
|
-
# Write a hash of attributes and values.
|
84
|
-
#
|
85
|
-
# If attribute value is a `Proc` it will be evaluated in the context
|
86
|
-
# of the resource after all non-proc attribute values are set. Values
|
87
|
-
# will be casted to defined attribute type.
|
88
|
-
#
|
89
|
-
# The behavior is used to apply default attributes from resource
|
90
|
-
# class definition.
|
91
|
-
#
|
92
|
-
# @example
|
93
|
-
# user.write_attributes name: 'john', email: ->{ "#{name}@example.org" }
|
94
|
-
# user.name # => 'john'
|
95
|
-
# user.email # => 'john@example.org'
|
96
|
-
#
|
97
|
-
# @param [Hash{String, Symbol => Object, Proc}, #each{|key, value|}]
|
98
|
-
# Attributes to write.
|
99
|
-
#
|
100
|
-
# @see #write_attribute Delegates attribute values to `#write_attribute`.
|
101
|
-
#
|
102
|
-
def write_attributes(attributes, opts = {})
|
103
|
-
unless attributes.respond_to?(:each) && attributes.respond_to?(:keys)
|
104
|
-
return false
|
105
|
-
end
|
106
|
-
|
107
|
-
if opts.fetch(:unknown, :ignore) == :raise
|
108
|
-
if (attributes.keys.map(&:to_s) - self.class.attributes.keys).any?
|
109
|
-
missing = attributes.keys - self.class.attributes.keys
|
110
|
-
missing.map!(&:inspect)
|
111
|
-
raise ArgumentError.new "Unknown attributes: #{missing.join(', ')}"
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
procs = {}
|
116
|
-
|
117
|
-
attributes.each do |key, _|
|
118
|
-
if attributes[key].is_a? Proc
|
119
|
-
procs[key] = attributes[key]
|
120
|
-
else
|
121
|
-
write_local_attribute key, attributes[key], opts
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
procs.each do |key, proc|
|
126
|
-
write_local_attribute key, instance_exec(&proc), opts
|
127
|
-
end
|
128
|
-
|
129
|
-
true
|
130
|
-
end
|
131
|
-
|
132
|
-
# @api private
|
133
|
-
#
|
134
|
-
# Check if a public getter for attribute exists that should be called to
|
135
|
-
# write it or of {#write_attribute} should be called directly. This is
|
136
|
-
# necessary as {#write_attribute} should go though setters but can also
|
137
|
-
# handle unknown attribute that will not have a generated setter method.
|
138
|
-
#
|
139
|
-
def write_local_attribute(name, value, opts = {})
|
140
|
-
method = "#{name}="
|
141
|
-
if respond_to? method, true
|
142
|
-
public_send method, value
|
143
|
-
else
|
144
|
-
write_attribute name, value, opts
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
# @api public
|
149
|
-
#
|
150
|
-
# Write single attribute with given value. Value will be casted
|
151
|
-
# to defined attribute type.
|
152
|
-
#
|
153
|
-
# @param [String, Symbol] name Attribute name.
|
154
|
-
# @param [Object] value Value to write.
|
155
|
-
# @raise [ArgumentError] If no attribute with given name is defined.
|
156
|
-
#
|
157
|
-
def write_attribute(name, value, opts = {})
|
158
|
-
attr_type = self.class.defined_attributes[name.to_s]
|
159
|
-
if attr_type
|
160
|
-
write_raw_attribute name, attr_type.cast(value), opts
|
161
|
-
else
|
162
|
-
write_raw_attribute name, value, opts
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
# @api private
|
167
|
-
#
|
168
|
-
# Write an attribute without checking type or existence or casting
|
169
|
-
# value to attributes type. Value be stored in an instance variable
|
170
|
-
# named after attribute name.
|
171
|
-
#
|
172
|
-
# @param [String, Symbol] name Attribute name.
|
173
|
-
# @param [Object] value Attribute value.
|
174
|
-
#
|
175
|
-
def write_raw_attribute(name, value, _ = {})
|
176
|
-
attributes[name.to_s] = value
|
177
|
-
end
|
178
|
-
|
179
|
-
#
|
180
|
-
module ClassMethods
|
181
|
-
ATTR_CLASS_BASE = '::Acfs::Resource::Attributes'.freeze
|
182
|
-
|
183
|
-
#
|
184
|
-
# @api public
|
185
|
-
#
|
186
|
-
# Define a model attribute by name and type. Will create getter and
|
187
|
-
# setter for given attribute name. Existing methods will be overridden.
|
188
|
-
#
|
189
|
-
# Available types can be found in `Acfs::Model::Attributes::*`.
|
190
|
-
#
|
191
|
-
# @example
|
192
|
-
# class User < Acfs::Resource
|
193
|
-
# attribute :name, :string, default: 'Anon'
|
194
|
-
# attribute :email, :string, default: lambda{ "#{name}@example.org"}
|
195
|
-
# end
|
196
|
-
#
|
197
|
-
# @param [#to_sym] name Attribute name.
|
198
|
-
# @param [Symbol, String, Class] type Attribute
|
199
|
-
# type identifier or type class.
|
200
|
-
#
|
201
|
-
def attribute(name, type, opts = {})
|
202
|
-
if type.is_a?(Symbol) || type.is_a?(String)
|
203
|
-
type = "#{ATTR_CLASS_BASE}::#{type.to_s.classify}".constantize
|
204
|
-
end
|
205
|
-
|
206
|
-
define_attribute name.to_sym, type, opts
|
207
|
-
end
|
208
|
-
|
209
|
-
# @api public
|
210
|
-
#
|
211
|
-
# Return list of possible attributes and default
|
212
|
-
# values for this model class.
|
213
|
-
#
|
214
|
-
# @example
|
215
|
-
# class User < Acfs::Resource
|
216
|
-
# attribute :name, :string
|
217
|
-
# attribute :age, :integer, default: 25
|
218
|
-
# end
|
219
|
-
# User.attributes # => { "name": nil, "age": 25 }
|
220
|
-
#
|
221
|
-
# @return [Hash{String => Object, Proc}]
|
222
|
-
# Attributes with default values.
|
223
|
-
#
|
224
|
-
def attributes
|
225
|
-
defined_attributes.each_with_object({}) do |(key, attr), hash|
|
226
|
-
hash[key] = attr.default_value
|
227
|
-
end
|
228
|
-
end
|
229
|
-
|
230
|
-
def defined_attributes
|
231
|
-
if superclass.respond_to?(:defined_attributes)
|
232
|
-
superclass.defined_attributes.merge(local_attributes)
|
233
|
-
else
|
234
|
-
local_attributes
|
235
|
-
end
|
236
|
-
end
|
237
|
-
|
238
|
-
private
|
239
|
-
|
240
|
-
def local_attributes
|
241
|
-
@local_attributes ||= {}
|
242
|
-
end
|
243
|
-
|
244
|
-
def define_attribute(name, type, opts = {})
|
245
|
-
name = name.to_s
|
246
|
-
attribute = type.new opts
|
247
|
-
|
248
|
-
local_attributes[name] = attribute
|
249
|
-
define_attribute_method name
|
250
|
-
|
251
|
-
send :define_method, name do
|
252
|
-
read_attribute name
|
253
|
-
end
|
254
|
-
|
255
|
-
send :define_method, :"#{name}=" do |value|
|
256
|
-
write_attribute name, value
|
257
|
-
end
|
258
|
-
end
|
259
|
-
end
|
260
|
-
end
|
261
|
-
end
|
262
|
-
|
263
|
-
# Load attribute type classes.
|
264
|
-
#
|
265
|
-
Dir[File.dirname(__FILE__) + '/attributes/*.rb'].sort.each do |path|
|
266
|
-
filename = File.basename(path)
|
267
|
-
require "acfs/resource/attributes/#{filename}"
|
268
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
module Acfs::Resource::Attributes
|
2
|
-
#
|
3
|
-
class Base
|
4
|
-
attr_reader :default
|
5
|
-
|
6
|
-
def initialize(default: nil)
|
7
|
-
@default = default
|
8
|
-
end
|
9
|
-
|
10
|
-
def cast(value)
|
11
|
-
cast_value(value) unless value.nil?
|
12
|
-
end
|
13
|
-
|
14
|
-
def default_value
|
15
|
-
if default.respond_to? :call
|
16
|
-
default
|
17
|
-
else
|
18
|
-
cast default
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
def cast_value(_value)
|
25
|
-
raise NotImplementedError
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
module Acfs::Resource::Attributes
|
2
|
-
# @api public
|
3
|
-
#
|
4
|
-
# Boolean attribute type. Use it in your model as an attribute type:
|
5
|
-
#
|
6
|
-
# @example
|
7
|
-
# class User < Acfs::Resource
|
8
|
-
# attribute :name, :boolean
|
9
|
-
# end
|
10
|
-
#
|
11
|
-
# Given objects will be converted to string. The following strings
|
12
|
-
# are considered true, everything else false:
|
13
|
-
#
|
14
|
-
# true, on, yes
|
15
|
-
#
|
16
|
-
class Boolean < Base
|
17
|
-
FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF', 'no', 'NO'].to_set
|
18
|
-
|
19
|
-
# @api public
|
20
|
-
#
|
21
|
-
# Cast given object to boolean.
|
22
|
-
#
|
23
|
-
# @param [Object] value Object to cast.
|
24
|
-
# @return [TrueClass, FalseClass] Casted boolean.
|
25
|
-
#
|
26
|
-
def cast_value(value)
|
27
|
-
return true if value == true
|
28
|
-
return false if value == false
|
29
|
-
|
30
|
-
if value.blank?
|
31
|
-
nil
|
32
|
-
else
|
33
|
-
!FALSE_VALUES.include?(value)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
module Acfs::Resource::Attributes
|
2
|
-
# @api public
|
3
|
-
#
|
4
|
-
# DateTime attribute type. Use it in your model as
|
5
|
-
# an attribute type:
|
6
|
-
#
|
7
|
-
# @example
|
8
|
-
# class User < Acfs::Resource
|
9
|
-
# attribute :name, :date_time
|
10
|
-
# end
|
11
|
-
#
|
12
|
-
class DateTime < Base
|
13
|
-
|
14
|
-
# @api public
|
15
|
-
#
|
16
|
-
# Cast given object to DateTime.
|
17
|
-
#
|
18
|
-
# @param [Object] value Object to cast.
|
19
|
-
# @return [DateTime] Casted object as DateTime.
|
20
|
-
#
|
21
|
-
def cast_value(value)
|
22
|
-
if value.blank?
|
23
|
-
nil
|
24
|
-
elsif value.acts_like?(:time) || value.acts_like?(:date)
|
25
|
-
value.to_datetime
|
26
|
-
else
|
27
|
-
::DateTime.iso8601 value
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
module Acfs::Resource::Attributes
|
2
|
-
# @api public
|
3
|
-
#
|
4
|
-
# Dict attribute type. Use it in your model as an attribute type:
|
5
|
-
#
|
6
|
-
# @example
|
7
|
-
# class User
|
8
|
-
# include Acfs::Model
|
9
|
-
# attribute :opts, :dict
|
10
|
-
# end
|
11
|
-
#
|
12
|
-
class Dict < Base
|
13
|
-
# @api public
|
14
|
-
#
|
15
|
-
# Cast given object to a dict/hash.
|
16
|
-
#
|
17
|
-
# @param [Object] value Object to cast.
|
18
|
-
# @return [Hash] Casted object as hash.
|
19
|
-
# @raise [TypeError] If object cannot be casted to a hash.
|
20
|
-
#
|
21
|
-
def cast_value(value)
|
22
|
-
return {} if value.blank?
|
23
|
-
|
24
|
-
if value.is_a?(Hash)
|
25
|
-
value
|
26
|
-
elsif value.respond_to?(:serializable_hash)
|
27
|
-
value.serializable_hash
|
28
|
-
elsif value.respond_to?(:to_hash)
|
29
|
-
value.to_hash
|
30
|
-
elsif value.respond_to?(:to_h)
|
31
|
-
value.to_h
|
32
|
-
else
|
33
|
-
Hash(value)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
module Acfs::Resource::Attributes
|
2
|
-
# @api public
|
3
|
-
#
|
4
|
-
# Float attribute type. Use it in your model as an attribute type:
|
5
|
-
#
|
6
|
-
# @example
|
7
|
-
# class User < Acfs::Resource
|
8
|
-
# attribute :name, :float
|
9
|
-
# end
|
10
|
-
#
|
11
|
-
class Float < Base
|
12
|
-
# @api public
|
13
|
-
#
|
14
|
-
# Cast given object to float.
|
15
|
-
#
|
16
|
-
# @param [Object] value Object to cast.
|
17
|
-
# @return [Float] Casted object as float.
|
18
|
-
#
|
19
|
-
def cast_value(value)
|
20
|
-
return 0.0 if value.blank?
|
21
|
-
|
22
|
-
case value
|
23
|
-
when ::Float then value
|
24
|
-
when "Infinity" then ::Float::INFINITY
|
25
|
-
when "-Infinity" then -::Float::INFINITY
|
26
|
-
when "NaN" then ::Float::NAN
|
27
|
-
else Float(value)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module Acfs::Resource::Attributes
|
2
|
-
# @api public
|
3
|
-
#
|
4
|
-
# Integer attribute type. Use it in your model as an attribute type:
|
5
|
-
#
|
6
|
-
# @example
|
7
|
-
# class User < Acfs::Resource
|
8
|
-
# attribute :name, :integer
|
9
|
-
# end
|
10
|
-
#
|
11
|
-
class Integer < Base
|
12
|
-
# @api public
|
13
|
-
#
|
14
|
-
# Cast given object to integer.
|
15
|
-
#
|
16
|
-
# @param [Object] value Object to cast.
|
17
|
-
# @return [Fixnum] Casted object as fixnum.
|
18
|
-
#
|
19
|
-
def cast_value(value)
|
20
|
-
if value.blank?
|
21
|
-
0
|
22
|
-
else
|
23
|
-
Integer(value)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|