jsapi 0.9.1 → 1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/jsapi/dsl/definitions.rb +1 -1
- data/lib/jsapi/dsl/operation.rb +20 -0
- data/lib/jsapi/dsl/schema.rb +12 -2
- data/lib/jsapi/meta/callback/{model.rb → base.rb} +4 -6
- data/lib/jsapi/meta/callback/reference.rb +2 -6
- data/lib/jsapi/meta/callback.rb +3 -3
- data/lib/jsapi/meta/contact.rb +2 -2
- data/lib/jsapi/meta/defaults.rb +1 -1
- data/lib/jsapi/meta/definitions.rb +10 -8
- data/lib/jsapi/meta/example/{model.rb → base.rb} +5 -5
- data/lib/jsapi/meta/example/reference.rb +2 -6
- data/lib/jsapi/meta/example.rb +3 -3
- data/lib/jsapi/meta/external_documentation.rb +2 -2
- data/lib/jsapi/meta/header/{model.rb → base.rb} +3 -3
- data/lib/jsapi/meta/header/reference.rb +2 -6
- data/lib/jsapi/meta/header.rb +3 -3
- data/lib/jsapi/meta/info.rb +15 -7
- data/lib/jsapi/meta/license.rb +28 -5
- data/lib/jsapi/meta/link/{model.rb → base.rb} +2 -2
- data/lib/jsapi/meta/link/reference.rb +2 -6
- data/lib/jsapi/meta/link.rb +3 -3
- data/lib/jsapi/meta/{base → model}/attributes.rb +15 -18
- data/lib/jsapi/meta/{base/model.rb → model/base.rb} +2 -2
- data/lib/jsapi/meta/model/reference.rb +72 -0
- data/lib/jsapi/meta/{base → model}/type_caster.rb +1 -1
- data/lib/jsapi/meta/model.rb +6 -0
- data/lib/jsapi/meta/oauth_flow.rb +2 -2
- data/lib/jsapi/meta/openapi/extensions.rb +9 -25
- data/lib/jsapi/meta/openapi/version.rb +11 -0
- data/lib/jsapi/meta/openapi.rb +10 -0
- data/lib/jsapi/meta/operation.rb +16 -18
- data/lib/jsapi/meta/parameter/{model.rb → base.rb} +86 -69
- data/lib/jsapi/meta/parameter/reference.rb +4 -9
- data/lib/jsapi/meta/parameter.rb +3 -4
- data/lib/jsapi/meta/property.rb +7 -6
- data/lib/jsapi/meta/request_body/{model.rb → base.rb} +11 -9
- data/lib/jsapi/meta/request_body/reference.rb +2 -6
- data/lib/jsapi/meta/request_body.rb +3 -3
- data/lib/jsapi/meta/rescue_handler.rb +1 -1
- data/lib/jsapi/meta/response/{model.rb → base.rb} +3 -3
- data/lib/jsapi/meta/response/reference.rb +2 -9
- data/lib/jsapi/meta/response.rb +3 -3
- data/lib/jsapi/meta/schema/additional_properties.rb +2 -2
- data/lib/jsapi/meta/schema/array.rb +3 -5
- data/lib/jsapi/meta/schema/base.rb +10 -12
- data/lib/jsapi/meta/schema/conversion.rb +2 -2
- data/lib/jsapi/meta/schema/discriminator.rb +1 -1
- data/lib/jsapi/meta/schema/numeric.rb +3 -5
- data/lib/jsapi/meta/schema/object.rb +3 -5
- data/lib/jsapi/meta/schema/reference.rb +6 -6
- data/lib/jsapi/meta/schema/string.rb +3 -5
- data/lib/jsapi/meta/security_requirement.rb +3 -3
- data/lib/jsapi/meta/security_scheme/base.rb +1 -1
- data/lib/jsapi/meta/server.rb +2 -2
- data/lib/jsapi/meta/server_variable.rb +2 -2
- data/lib/jsapi/meta/tag.rb +2 -2
- data/lib/jsapi/meta.rb +1 -1
- data/lib/jsapi/version.rb +1 -1
- metadata +14 -15
- data/lib/jsapi/meta/base/reference.rb +0 -35
- data/lib/jsapi/meta/base.rb +0 -6
- data/lib/jsapi/meta/parameter/to_openapi.rb +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d62e2c954028d8795f7e82dc2f0cde0310120f7e2a545102f53acefb42bfc603
|
4
|
+
data.tar.gz: ed65cf2695f95d4ff4622680ac54c601620974997f5cd2b5408fbe3deee62aef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f2cca6b7b9a4d37979d7350ed24b171c0557e11d13968bea7cc7d7ca39418d4cfa39099b68fcf9a00249151f27362e677318ab8223c64d66cbf20ca9b6576045
|
7
|
+
data.tar.gz: cca09add16c901c819327e5391ab7a7d627434c3c1e4efdb6802fde5e053741b955748bce4708d0bcf8bee9ef46c9babf60949ef3d6f726e5f5f7feca7ef5d85
|
data/lib/jsapi/dsl/operation.rb
CHANGED
@@ -38,6 +38,15 @@ module Jsapi
|
|
38
38
|
#
|
39
39
|
# deprecated true
|
40
40
|
|
41
|
+
##
|
42
|
+
# :method: external_docs
|
43
|
+
# :args: **keywords, &block
|
44
|
+
# Specifies the external documentation.
|
45
|
+
#
|
46
|
+
# external_docs url: 'https://foo.bar/docs'
|
47
|
+
#
|
48
|
+
# See Meta::Operation#external_docs for further information.
|
49
|
+
|
41
50
|
##
|
42
51
|
# :method: description
|
43
52
|
# :args: arg
|
@@ -162,6 +171,17 @@ module Jsapi
|
|
162
171
|
end
|
163
172
|
end
|
164
173
|
|
174
|
+
##
|
175
|
+
# :method: security_requirement
|
176
|
+
# :args: **keywords, &block
|
177
|
+
# Specifies a security requirement.
|
178
|
+
#
|
179
|
+
# security_requirement do
|
180
|
+
# scheme 'basic_auth'
|
181
|
+
# end
|
182
|
+
#
|
183
|
+
# See Meta::Operation#security_requirements for further information.
|
184
|
+
|
165
185
|
##
|
166
186
|
# :method: summary
|
167
187
|
# :args: arg
|
data/lib/jsapi/dsl/schema.rb
CHANGED
@@ -5,6 +5,15 @@ module Jsapi
|
|
5
5
|
# Used to define a schema.
|
6
6
|
class Schema < Base
|
7
7
|
|
8
|
+
##
|
9
|
+
# :method: additional_properties
|
10
|
+
# :args: **keywords, &block
|
11
|
+
# Specifies the schema of properties that are not explicity specified.
|
12
|
+
#
|
13
|
+
# additional_properties type: 'string'
|
14
|
+
#
|
15
|
+
# See Meta::Schema::Object#additional_properties for further information.
|
16
|
+
|
8
17
|
# Includes all of the properties from +schemas+. Each argument must be the name of
|
9
18
|
# a schema defined by ClassMethods#api_schema or Definitions#schema.
|
10
19
|
def all_of(*schemas)
|
@@ -47,8 +56,9 @@ module Jsapi
|
|
47
56
|
#
|
48
57
|
# enum %w[foo bar]
|
49
58
|
|
50
|
-
##
|
51
|
-
|
59
|
+
##
|
60
|
+
# :method: external_docs
|
61
|
+
# :args: **keywords, &block
|
52
62
|
# Specifies the external documentation.
|
53
63
|
#
|
54
64
|
# See Meta::Schema::Base#external_docs for further information.
|
@@ -2,16 +2,14 @@
|
|
2
2
|
|
3
3
|
module Jsapi
|
4
4
|
module Meta
|
5
|
-
class Operation < Base
|
5
|
+
class Operation < Model::Base; end
|
6
6
|
|
7
7
|
module Callback
|
8
|
-
# Specifies a callback
|
9
|
-
class
|
8
|
+
# Specifies a callback. Applies to \OpenAPI 3.0 and higher.
|
9
|
+
class Base < Model::Base
|
10
10
|
##
|
11
11
|
# :attr: operations
|
12
|
-
attribute :operations, { String => Operation }
|
13
|
-
|
14
|
-
undef add_operation
|
12
|
+
attribute :operations, { String => Operation }, accessors: %i[reader writer]
|
15
13
|
|
16
14
|
# Adds a callback operation.
|
17
15
|
#
|
@@ -3,12 +3,8 @@
|
|
3
3
|
module Jsapi
|
4
4
|
module Meta
|
5
5
|
module Callback
|
6
|
-
|
7
|
-
|
8
|
-
def to_openapi(*)
|
9
|
-
{ '$ref': "#/components/callbacks/#{ref}" }
|
10
|
-
end
|
11
|
-
end
|
6
|
+
# Refers a reusable callback.
|
7
|
+
class Reference < Model::Reference; end
|
12
8
|
end
|
13
9
|
end
|
14
10
|
end
|
data/lib/jsapi/meta/callback.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative 'callback/
|
3
|
+
require_relative 'callback/base'
|
4
4
|
require_relative 'callback/reference'
|
5
5
|
|
6
6
|
module Jsapi
|
7
7
|
module Meta
|
8
8
|
module Callback
|
9
9
|
class << self
|
10
|
-
# Creates a
|
10
|
+
# Creates a Base or Reference.
|
11
11
|
def new(keywords = {})
|
12
12
|
return Reference.new(keywords) if keywords.key?(:ref)
|
13
13
|
|
14
|
-
|
14
|
+
Base.new(keywords)
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
data/lib/jsapi/meta/contact.rb
CHANGED
data/lib/jsapi/meta/defaults.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module Jsapi
|
4
4
|
module Meta
|
5
|
-
class Definitions < Base
|
5
|
+
class Definitions < Model::Base
|
6
6
|
include OpenAPI::Extensions
|
7
7
|
|
8
8
|
##
|
@@ -58,12 +58,12 @@ module Jsapi
|
|
58
58
|
##
|
59
59
|
# :attr: operations
|
60
60
|
# The Operation objects.
|
61
|
-
attribute :operations, { String => Operation }
|
61
|
+
attribute :operations, { String => Operation }, accessors: %i[reader writer]
|
62
62
|
|
63
63
|
##
|
64
64
|
# :attr: parameters
|
65
65
|
# The reusable Parameter objects.
|
66
|
-
attribute :parameters, { String => Parameter }
|
66
|
+
attribute :parameters, { String => Parameter }, accessors: %i[reader writer]
|
67
67
|
|
68
68
|
##
|
69
69
|
# :attr: rescue_handlers
|
@@ -138,8 +138,6 @@ module Jsapi
|
|
138
138
|
@parent&.inherited(self)
|
139
139
|
end
|
140
140
|
|
141
|
-
undef add_operation, add_parameter
|
142
|
-
|
143
141
|
def add_operation(name = nil, keywords = {}) # :nodoc:
|
144
142
|
name = name.nil? ? default_operation_name : name.to_s
|
145
143
|
keywords = keywords.reverse_merge(path: default_operation_path)
|
@@ -273,7 +271,7 @@ module Jsapi
|
|
273
271
|
else
|
274
272
|
{
|
275
273
|
# Order according to the OpenAPI specification 3.x
|
276
|
-
openapi: version.minor.zero? ? '3.0.3' : '3.1.
|
274
|
+
openapi: version.minor.zero? ? '3.0.3' : '3.1.1',
|
277
275
|
info: openapi_objects[:info],
|
278
276
|
servers: openapi_objects[:servers] ||
|
279
277
|
[default_server&.to_openapi].compact.presence,
|
@@ -382,10 +380,14 @@ module Jsapi
|
|
382
380
|
@objects ||= ancestors.each_with_object({}) do |ancestor, objects|
|
383
381
|
self.class.attribute_names.each do |key|
|
384
382
|
case value = ancestor.send(key)
|
385
|
-
when Hash
|
386
|
-
(objects[key] ||= {}).reverse_merge!(value)
|
387
383
|
when Array
|
388
384
|
(objects[key] ||= []).push(*value)
|
385
|
+
when Hash
|
386
|
+
if (hash = objects[key])
|
387
|
+
value.each { |k, v| hash[k] = v unless hash.key?(k) }
|
388
|
+
else
|
389
|
+
objects[key] = value.dup
|
390
|
+
end
|
389
391
|
else
|
390
392
|
objects[key] ||= value
|
391
393
|
end
|
@@ -3,8 +3,8 @@
|
|
3
3
|
module Jsapi
|
4
4
|
module Meta
|
5
5
|
module Example
|
6
|
-
# Specifies an example
|
7
|
-
class
|
6
|
+
# Specifies an example.
|
7
|
+
class Base < Model::Base
|
8
8
|
include OpenAPI::Extensions
|
9
9
|
|
10
10
|
##
|
@@ -29,11 +29,11 @@ module Jsapi
|
|
29
29
|
|
30
30
|
# Returns a hash representing the \OpenAPI example object.
|
31
31
|
def to_openapi(*)
|
32
|
-
with_openapi_extensions(summary: summary, description: description).tap do |
|
32
|
+
with_openapi_extensions(summary: summary, description: description).tap do |result|
|
33
33
|
if external?
|
34
|
-
|
34
|
+
result[:external_value] = value
|
35
35
|
else
|
36
|
-
|
36
|
+
result[:value] = value
|
37
37
|
end
|
38
38
|
end
|
39
39
|
end
|
@@ -3,12 +3,8 @@
|
|
3
3
|
module Jsapi
|
4
4
|
module Meta
|
5
5
|
module Example
|
6
|
-
|
7
|
-
|
8
|
-
def to_openapi(*)
|
9
|
-
{ '$ref': "#/components/examples/#{ref}" }
|
10
|
-
end
|
11
|
-
end
|
6
|
+
# Refers a reusable example.
|
7
|
+
class Reference < Model::Reference; end
|
12
8
|
end
|
13
9
|
end
|
14
10
|
end
|
data/lib/jsapi/meta/example.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative 'example/
|
3
|
+
require_relative 'example/base'
|
4
4
|
require_relative 'example/reference'
|
5
5
|
|
6
6
|
module Jsapi
|
7
7
|
module Meta
|
8
8
|
module Example
|
9
9
|
class << self
|
10
|
-
# Creates a
|
10
|
+
# Creates a Base or Reference.
|
11
11
|
def new(keywords = {})
|
12
12
|
return Reference.new(keywords) if keywords.key?(:ref)
|
13
13
|
|
14
|
-
|
14
|
+
Base.new(keywords)
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
@@ -3,8 +3,8 @@
|
|
3
3
|
module Jsapi
|
4
4
|
module Meta
|
5
5
|
module Header
|
6
|
-
# Specifies
|
7
|
-
class
|
6
|
+
# Specifies an HTTP header.
|
7
|
+
class Base < Model::Base
|
8
8
|
include OpenAPI::Extensions
|
9
9
|
|
10
10
|
delegate_missing_to :schema
|
@@ -39,7 +39,7 @@ module Jsapi
|
|
39
39
|
##
|
40
40
|
# :attr_reader: schema
|
41
41
|
# The Schema of the header.
|
42
|
-
attribute :schema,
|
42
|
+
attribute :schema, accessors: %i[reader]
|
43
43
|
|
44
44
|
def initialize(keywords = {})
|
45
45
|
raise ArgumentError, "type can't be object" if keywords[:type] == 'object'
|
@@ -3,12 +3,8 @@
|
|
3
3
|
module Jsapi
|
4
4
|
module Meta
|
5
5
|
module Header
|
6
|
-
|
7
|
-
|
8
|
-
def to_openapi(*)
|
9
|
-
{ '$ref': "#/components/headers/#{ref}" }
|
10
|
-
end
|
11
|
-
end
|
6
|
+
# Refers a reusable header.
|
7
|
+
class Reference < Model::Reference; end
|
12
8
|
end
|
13
9
|
end
|
14
10
|
end
|
data/lib/jsapi/meta/header.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative 'header/
|
3
|
+
require_relative 'header/base'
|
4
4
|
require_relative 'header/reference'
|
5
5
|
|
6
6
|
module Jsapi
|
7
7
|
module Meta
|
8
8
|
module Header
|
9
9
|
class << self
|
10
|
-
# Creates a
|
10
|
+
# Creates a Base or Reference.
|
11
11
|
def new(keywords = {})
|
12
12
|
return Reference.new(keywords) if keywords.key?(:ref)
|
13
13
|
|
14
|
-
|
14
|
+
Base.new(keywords)
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
data/lib/jsapi/meta/info.rb
CHANGED
@@ -2,13 +2,13 @@
|
|
2
2
|
|
3
3
|
module Jsapi
|
4
4
|
module Meta
|
5
|
-
# Specifies an
|
6
|
-
class Info < Base
|
5
|
+
# Specifies metadata about an API.
|
6
|
+
class Info < Model::Base
|
7
7
|
include OpenAPI::Extensions
|
8
8
|
|
9
9
|
##
|
10
10
|
# :attr: contact
|
11
|
-
# The Contact
|
11
|
+
# The Contact.
|
12
12
|
attribute :contact, Contact
|
13
13
|
|
14
14
|
##
|
@@ -18,9 +18,14 @@ module Jsapi
|
|
18
18
|
|
19
19
|
##
|
20
20
|
# :attr: license
|
21
|
-
# The License
|
21
|
+
# The License.
|
22
22
|
attribute :license, License
|
23
23
|
|
24
|
+
##
|
25
|
+
# :attr: summary
|
26
|
+
# The short summary of the API. Applies to \OpenAPI 3.1 and higher.
|
27
|
+
attribute :summary, String
|
28
|
+
|
24
29
|
##
|
25
30
|
# :attr: terms_of_service
|
26
31
|
# The URL pointing to the terms of service.
|
@@ -37,14 +42,17 @@ module Jsapi
|
|
37
42
|
attribute :version, String
|
38
43
|
|
39
44
|
# Returns a hash representing the \OpenAPI info object.
|
40
|
-
def to_openapi(*)
|
45
|
+
def to_openapi(version, *)
|
46
|
+
version = OpenAPI::Version.from(version)
|
47
|
+
|
41
48
|
with_openapi_extensions(
|
42
49
|
title: title,
|
43
|
-
|
50
|
+
summary: (summary if version >= OpenAPI::V3_1),
|
44
51
|
description: description,
|
45
52
|
termsOfService: terms_of_service,
|
46
53
|
contact: contact&.to_openapi,
|
47
|
-
license: license&.to_openapi
|
54
|
+
license: license&.to_openapi(version),
|
55
|
+
version: self.version
|
48
56
|
)
|
49
57
|
end
|
50
58
|
end
|
data/lib/jsapi/meta/license.rb
CHANGED
@@ -2,10 +2,15 @@
|
|
2
2
|
|
3
3
|
module Jsapi
|
4
4
|
module Meta
|
5
|
-
# Specifies a license
|
6
|
-
class License < Base
|
5
|
+
# Specifies a license.
|
6
|
+
class License < Model::Base
|
7
7
|
include OpenAPI::Extensions
|
8
8
|
|
9
|
+
##
|
10
|
+
# :attr: identifier
|
11
|
+
# The SDPX identifier of the license. Applies to \OpenAPI 3.1 and higher.
|
12
|
+
attribute :identifier, String, accessors: %i[reader]
|
13
|
+
|
9
14
|
##
|
10
15
|
# :attr: name
|
11
16
|
# The name of the license.
|
@@ -14,11 +19,29 @@ module Jsapi
|
|
14
19
|
##
|
15
20
|
# :attr: url
|
16
21
|
# The URL of the license.
|
17
|
-
attribute :url, String
|
22
|
+
attribute :url, String, accessors: %i[reader]
|
23
|
+
|
24
|
+
def identifier=(identifier) # :nodoc:
|
25
|
+
raise 'identifier and url are mutually exclusive' if url.present?
|
26
|
+
|
27
|
+
@identifier = identifier
|
28
|
+
end
|
29
|
+
|
30
|
+
def url=(url) # :nodoc:
|
31
|
+
raise 'identifier and url are mutually exclusive' if identifier.present?
|
32
|
+
|
33
|
+
@url = url
|
34
|
+
end
|
18
35
|
|
19
36
|
# Returns a hash representing the \OpenAPI license object.
|
20
|
-
def to_openapi(*)
|
21
|
-
|
37
|
+
def to_openapi(version, *)
|
38
|
+
version = OpenAPI::Version.from(version)
|
39
|
+
|
40
|
+
with_openapi_extensions(
|
41
|
+
name: name,
|
42
|
+
identifier: (identifier if version >= OpenAPI::V3_1),
|
43
|
+
url: url
|
44
|
+
)
|
22
45
|
end
|
23
46
|
end
|
24
47
|
end
|
@@ -3,12 +3,8 @@
|
|
3
3
|
module Jsapi
|
4
4
|
module Meta
|
5
5
|
module Link
|
6
|
-
|
7
|
-
|
8
|
-
def to_openapi(*)
|
9
|
-
{ '$ref': "#/components/links/#{ref}" }
|
10
|
-
end
|
11
|
-
end
|
6
|
+
# Refers a reusable link.
|
7
|
+
class Reference < Model::Reference; end
|
12
8
|
end
|
13
9
|
end
|
14
10
|
end
|
data/lib/jsapi/meta/link.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative 'link/
|
3
|
+
require_relative 'link/base'
|
4
4
|
require_relative 'link/reference'
|
5
5
|
|
6
6
|
module Jsapi
|
7
7
|
module Meta
|
8
8
|
module Link
|
9
9
|
class << self
|
10
|
-
# Creates a
|
10
|
+
# Creates a Base or Reference.
|
11
11
|
def new(keywords = {})
|
12
12
|
return Reference.new(keywords) if keywords.key?(:ref)
|
13
13
|
|
14
|
-
|
14
|
+
Base.new(keywords)
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
@@ -2,17 +2,17 @@
|
|
2
2
|
|
3
3
|
module Jsapi
|
4
4
|
module Meta
|
5
|
-
module
|
5
|
+
module Model
|
6
6
|
module Attributes
|
7
7
|
DEFAULT_ARRAY = [].freeze
|
8
8
|
DEFAULT_HASH = {}.freeze
|
9
9
|
|
10
10
|
# Defines an attribute.
|
11
11
|
def attribute(name, type = Object,
|
12
|
+
accessors: %i[add reader writer],
|
12
13
|
default: nil,
|
13
14
|
default_key: nil,
|
14
15
|
keys: nil,
|
15
|
-
read_only: false,
|
16
16
|
values: nil)
|
17
17
|
|
18
18
|
(@attribute_names ||= []) << name.to_sym
|
@@ -24,10 +24,9 @@ module Jsapi
|
|
24
24
|
# General default
|
25
25
|
default ||= DEFAULT_ARRAY
|
26
26
|
|
27
|
-
|
27
|
+
if accessors.include?(:add) || accessors.include?(:writer)
|
28
28
|
singular_name = name.to_s.singularize
|
29
29
|
add_method = "add_#{singular_name}"
|
30
|
-
|
31
30
|
type_caster = TypeCaster.new(type.first, values: values, name: singular_name)
|
32
31
|
|
33
32
|
# Attribute writer
|
@@ -35,7 +34,7 @@ module Jsapi
|
|
35
34
|
instance_variable_set(instance_variable_name, []).tap do
|
36
35
|
Array.wrap(argument).each { |element| send(add_method, element) }
|
37
36
|
end
|
38
|
-
end
|
37
|
+
end if accessors.include?(:writer)
|
39
38
|
|
40
39
|
# Add method
|
41
40
|
define_method(add_method) do |argument = nil|
|
@@ -47,26 +46,24 @@ module Jsapi
|
|
47
46
|
end << casted_argument
|
48
47
|
attribute_changed(name)
|
49
48
|
end
|
50
|
-
end
|
49
|
+
end if accessors.include?(:add)
|
51
50
|
end
|
52
51
|
when Hash
|
53
|
-
|
52
|
+
# General default
|
53
|
+
default ||= DEFAULT_HASH
|
54
54
|
|
55
|
+
singular_name = name.to_s.singularize
|
55
56
|
key_type, value_type = type.first
|
56
57
|
key_type_caster = TypeCaster.new(key_type, values: keys, name: 'key')
|
57
58
|
|
58
|
-
# General default
|
59
|
-
default ||= DEFAULT_HASH
|
60
|
-
|
61
59
|
# Lookup method
|
62
60
|
define_method(singular_name) do |key = nil|
|
63
61
|
key = default_key if key.to_s.empty?
|
64
62
|
send(name)[key_type_caster.cast(key)]
|
65
|
-
end
|
63
|
+
end if accessors.include?(:reader)
|
66
64
|
|
67
|
-
|
65
|
+
if accessors.include?(:add) || accessors.include?(:writer)
|
68
66
|
add_method = "add_#{singular_name}"
|
69
|
-
|
70
67
|
value_type_caster = TypeCaster.new(value_type, values: values)
|
71
68
|
|
72
69
|
# Attribute writer
|
@@ -74,7 +71,7 @@ module Jsapi
|
|
74
71
|
instance_variable_set(instance_variable_name, {}).tap do
|
75
72
|
Hash(argument).each { |key, value| send(add_method, key, value) }
|
76
73
|
end
|
77
|
-
end
|
74
|
+
end if accessors.include?(:writer)
|
78
75
|
|
79
76
|
# Add method
|
80
77
|
define_method(add_method) do |key_or_value, value = nil|
|
@@ -98,16 +95,16 @@ module Jsapi
|
|
98
95
|
|
99
96
|
attribute_changed(name)
|
100
97
|
casted_value
|
101
|
-
end
|
98
|
+
end if accessors.include?(:add)
|
102
99
|
end
|
103
100
|
else
|
104
101
|
# Predicate method
|
105
102
|
define_method("#{name}?") do
|
106
103
|
value = instance_variable_get(instance_variable_name)
|
107
104
|
value.nil? ? default || false : value
|
108
|
-
end if values == [true, false]
|
105
|
+
end if values == [true, false] && accessors.include?(:reader)
|
109
106
|
|
110
|
-
|
107
|
+
if accessors.include?(:writer)
|
111
108
|
type_caster = TypeCaster.new(type, values: values, name: name)
|
112
109
|
|
113
110
|
# Attribute writer
|
@@ -124,7 +121,7 @@ module Jsapi
|
|
124
121
|
define_method(name) do
|
125
122
|
value = instance_variable_get(instance_variable_name)
|
126
123
|
value.nil? ? default : value
|
127
|
-
end
|
124
|
+
end if accessors.include?(:reader)
|
128
125
|
end
|
129
126
|
|
130
127
|
def attribute_names
|