reynard 0.8.2 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cf9800ba5e9413a1b15f0e7ba1a670b445c3bd3ad17d4d4229b68f9edbb1d2ce
4
- data.tar.gz: 28dffe4ecfc842642f88d8d091e0acfb7b11f706a80504f2e9caf0fd983a0695
3
+ metadata.gz: 52cb53bbff626e0dce10485bf5cc023639b024024a237e34a73d09ae632ef9db
4
+ data.tar.gz: 1b66f322e4f7bfe5d0a5bd63096f218994bd55ed747a2fb2e1fa72c4af57ef30
5
5
  SHA512:
6
- metadata.gz: 272402519908855f09f368efa5d3e0e85791e48b5f86bd2ddee66b8418aea9f686192ca7bacc1eae233b87fc6b821303f3771a8e784bc762a028d22bc2520058
7
- data.tar.gz: 17481196746340a8caed347dc249cd4e2c59b421e999edc50700631741d4aa4ce2a8c4577e8b41fc61ba2feeb0cc22f2b4d86c321d532d26c8d3c1291606ba6a
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,19 +14,28 @@ class Reynard
14
14
  end
15
15
 
16
16
  def initialize(attributes)
17
- if attributes.respond_to?(:each)
17
+ if attributes.respond_to?(:each) && attributes.respond_to?(:keys)
18
18
  @attributes = {}
19
19
  @snake_cases = self.class.snake_cases(attributes.keys)
20
20
  self.attributes = attributes
21
21
  else
22
- raise(
23
- ArgumentError,
24
- 'Models must be initialized with an enumerable object that behaves like a hash, got: ' \
25
- "`#{attributes.inspect}'"
22
+ ::Kernel.raise(
23
+ ::ArgumentError,
24
+ self.class.attributes_error_message(attributes)
26
25
  )
27
26
  end
28
27
  end
29
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)}>"
37
+ end
38
+
30
39
  def attributes=(attributes)
31
40
  attributes.each do |name, value|
32
41
  @attributes[name.to_s] = self.class.cast(name, value)
@@ -35,14 +44,16 @@ class Reynard
35
44
 
36
45
  # Until we can set accessors based on the schema
37
46
  def method_missing(attribute_name, *)
47
+ return false unless @attributes
48
+
38
49
  attribute_name = attribute_name.to_s
39
50
  if @attributes.key?(attribute_name)
40
51
  @attributes[attribute_name]
41
52
  else
42
53
  @attributes.fetch(@snake_cases.fetch(attribute_name))
43
54
  end
44
- rescue KeyError
45
- raise NoMethodError, "undefined method `#{attribute_name}' for #{inspect}"
55
+ rescue ::KeyError
56
+ ::Kernel.raise ::NoMethodError, "undefined method `#{attribute_name}' for #{inspect}"
46
57
  end
47
58
 
48
59
  def respond_to_missing?(attribute_name, *)
@@ -50,10 +61,14 @@ class Reynard
50
61
  return true if @attributes.key?(attribute_name)
51
62
 
52
63
  @snake_cases.key?(attribute_name) && @attributes.key?(@snake_cases[attribute_name])
53
- rescue NameError
64
+ rescue ::NameError
54
65
  false
55
66
  end
56
67
 
68
+ def try(attribute_name)
69
+ respond_to_missing?(attribute_name) ? send(attribute_name) : nil
70
+ end
71
+
57
72
  def self.cast(name, value)
58
73
  return if value.nil?
59
74
  return value unless schema
@@ -61,11 +76,11 @@ class Reynard
61
76
  property = schema.property_schema(name)
62
77
  return value unless property
63
78
 
64
- Reynard::ObjectBuilder.new(schema: property, inflector: inflector, parsed_body: value).call
79
+ ::Reynard::ObjectBuilder.new(schema: property, inflector: inflector, parsed_body: value).call
65
80
  end
66
81
 
67
82
  def self.inflector
68
- @inflector ||= Inflector.new
83
+ @inflector ||= ::Reynard::Inflector.new
69
84
  end
70
85
 
71
86
  def self.snake_cases(property_names)
@@ -76,5 +91,11 @@ class Reynard
76
91
  snake_cases[snake_case] = property_name
77
92
  end
78
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
79
100
  end
80
101
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Reynard
4
- VERSION = '0.8.2'
4
+ VERSION = '0.9.0'
5
5
  end
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.8.2
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: 2023-07-26 00:00:00.000000000 Z
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.3.7
106
+ rubygems_version: 3.5.6
107
107
  signing_key:
108
108
  specification_version: 4
109
109
  summary: Minimal OpenAPI client.