reynard 0.8.1 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/reynard/model.rb +41 -11
- data/lib/reynard/version.rb +1 -1
- data/lib/reynard.rb +0 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 52cb53bbff626e0dce10485bf5cc023639b024024a237e34a73d09ae632ef9db
|
4
|
+
data.tar.gz: 1b66f322e4f7bfe5d0a5bd63096f218994bd55ed747a2fb2e1fa72c4af57ef30
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 15a3077ed0ee0c2331cab0182adae4b3970d83a4113f82d2d8aa4e528edd93678dd10a7e346d54e8d3e659dd5ad3509415f9de714a61a013e1937c3781eb686d
|
7
|
+
data.tar.gz: 80f49722dbde52f83aa9260aea1f71ccc6b97c7871f0818a71b4d42b4a79683cc404a0fa0ad8714f0d4fa4d3def54d2e140be1b35abe215699098da25c9b7b00
|
data/lib/reynard/model.rb
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
class Reynard
|
4
4
|
# Superclass for dynamic classes generated by the object builder.
|
5
|
-
class Model
|
6
|
-
extend Forwardable
|
7
|
-
def_delegators :@attributes, :[]
|
5
|
+
class Model < BasicObject
|
6
|
+
extend ::Forwardable
|
7
|
+
def_delegators :@attributes, :[], :empty?
|
8
8
|
|
9
9
|
class << self
|
10
10
|
# Holds references to the full schema for the model if available.
|
@@ -14,9 +14,26 @@ class Reynard
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def initialize(attributes)
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
if attributes.respond_to?(:each) && attributes.respond_to?(:keys)
|
18
|
+
@attributes = {}
|
19
|
+
@snake_cases = self.class.snake_cases(attributes.keys)
|
20
|
+
self.attributes = attributes
|
21
|
+
else
|
22
|
+
::Kernel.raise(
|
23
|
+
::ArgumentError,
|
24
|
+
self.class.attributes_error_message(attributes)
|
25
|
+
)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# We rely on these methods for various reasons so we re-introduce them at the expense of
|
30
|
+
# allowing them to be used as attribute name for the model.
|
31
|
+
%i[class is_a? nil? object_id kind_of? respond_to? send].each do |method|
|
32
|
+
define_method(method, ::Kernel.method(method))
|
33
|
+
end
|
34
|
+
|
35
|
+
def inspect
|
36
|
+
"#<#{self.class.name}:0x#{object_id.to_s(16)}>"
|
20
37
|
end
|
21
38
|
|
22
39
|
def attributes=(attributes)
|
@@ -27,14 +44,16 @@ class Reynard
|
|
27
44
|
|
28
45
|
# Until we can set accessors based on the schema
|
29
46
|
def method_missing(attribute_name, *)
|
47
|
+
return false unless @attributes
|
48
|
+
|
30
49
|
attribute_name = attribute_name.to_s
|
31
50
|
if @attributes.key?(attribute_name)
|
32
51
|
@attributes[attribute_name]
|
33
52
|
else
|
34
53
|
@attributes.fetch(@snake_cases.fetch(attribute_name))
|
35
54
|
end
|
36
|
-
rescue KeyError
|
37
|
-
raise NoMethodError, "undefined method `#{attribute_name}' for #{inspect}"
|
55
|
+
rescue ::KeyError
|
56
|
+
::Kernel.raise ::NoMethodError, "undefined method `#{attribute_name}' for #{inspect}"
|
38
57
|
end
|
39
58
|
|
40
59
|
def respond_to_missing?(attribute_name, *)
|
@@ -42,21 +61,26 @@ class Reynard
|
|
42
61
|
return true if @attributes.key?(attribute_name)
|
43
62
|
|
44
63
|
@snake_cases.key?(attribute_name) && @attributes.key?(@snake_cases[attribute_name])
|
45
|
-
rescue NameError
|
64
|
+
rescue ::NameError
|
46
65
|
false
|
47
66
|
end
|
48
67
|
|
68
|
+
def try(attribute_name)
|
69
|
+
respond_to_missing?(attribute_name) ? send(attribute_name) : nil
|
70
|
+
end
|
71
|
+
|
49
72
|
def self.cast(name, value)
|
73
|
+
return if value.nil?
|
50
74
|
return value unless schema
|
51
75
|
|
52
76
|
property = schema.property_schema(name)
|
53
77
|
return value unless property
|
54
78
|
|
55
|
-
Reynard::ObjectBuilder.new(schema: property, inflector: inflector, parsed_body: value).call
|
79
|
+
::Reynard::ObjectBuilder.new(schema: property, inflector: inflector, parsed_body: value).call
|
56
80
|
end
|
57
81
|
|
58
82
|
def self.inflector
|
59
|
-
@inflector ||= Inflector.new
|
83
|
+
@inflector ||= ::Reynard::Inflector.new
|
60
84
|
end
|
61
85
|
|
62
86
|
def self.snake_cases(property_names)
|
@@ -67,5 +91,11 @@ class Reynard
|
|
67
91
|
snake_cases[snake_case] = property_name
|
68
92
|
end
|
69
93
|
end
|
94
|
+
|
95
|
+
def self.attributes_error_message(attributes)
|
96
|
+
'Models must be intialized with an enumerable object that behaves like a Hash, got: ' \
|
97
|
+
"`#{attributes.inspect}'. Usually this means the schema defined in the OpenAPI " \
|
98
|
+
"specification doesn't fit the payload in the HTTP response."
|
99
|
+
end
|
70
100
|
end
|
71
101
|
end
|
data/lib/reynard/version.rb
CHANGED
data/lib/reynard.rb
CHANGED
@@ -20,7 +20,6 @@ class Reynard
|
|
20
20
|
autoload :GroupedParameters, 'reynard/grouped_parameters'
|
21
21
|
autoload :Http, 'reynard/http'
|
22
22
|
autoload :Inflector, 'reynard/inflector'
|
23
|
-
autoload :Logger, 'reynard/logger'
|
24
23
|
autoload :MediaType, 'reynard/media_type'
|
25
24
|
autoload :Model, 'reynard/model'
|
26
25
|
autoload :Models, 'reynard/models'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reynard
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Manfred Stienstra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-02-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: multi_json
|
@@ -103,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
103
103
|
- !ruby/object:Gem::Version
|
104
104
|
version: '0'
|
105
105
|
requirements: []
|
106
|
-
rubygems_version: 3.
|
106
|
+
rubygems_version: 3.5.6
|
107
107
|
signing_key:
|
108
108
|
specification_version: 4
|
109
109
|
summary: Minimal OpenAPI client.
|