anyway_config 2.2.0 → 2.2.1
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/CHANGELOG.md +5 -1
- data/README.md +7 -0
- data/lib/.rbnext/2.7/anyway/type_casting.rb +19 -6
- data/lib/anyway/type_casting.rb +19 -6
- data/lib/anyway/version.rb +1 -1
- data/sig/anyway_config.rbs +7 -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: a0f3278ebf47801b6fdd2836c3f9f63d9a8fc1fe324f04e30f74d2dca179f471
|
4
|
+
data.tar.gz: 8be597f8b2c6065f0ea96e5c58ec54d6ef3532f00d7a8639a963e99b2bba65c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8174b86b24e86de09a55e473bcec74fc37aeca1c38bb2a75e83e71dbeebf512b68fdb93ae23d247cb6a8ae6c5d1e2fbb86d659bf611f83ad219fccabad1ba7b6
|
7
|
+
data.tar.gz: dcf73ae92079a78b8c518cc3059a85848dd4934633269d016b402114279a0303108ab7962477ea2172ff15a1abc9c6401cea9106a83ba9b4c46a02220264eb23
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -602,6 +602,13 @@ Type coercion is especially useful to deal with array values:
|
|
602
602
|
coerce_types list: {type: :string, array: true}
|
603
603
|
```
|
604
604
|
|
605
|
+
You can use `type: nil` in case you don't want to coerce values, just convert a value into an array:
|
606
|
+
|
607
|
+
```ruby
|
608
|
+
# From AnyCable config (sentinels could be represented via strings or hashes)
|
609
|
+
coerce_types redis_sentinels: {type: nil, array: true}
|
610
|
+
```
|
611
|
+
|
605
612
|
It's also could be useful to explicitly define non-array types (to avoid confusion):
|
606
613
|
|
607
614
|
```ruby
|
@@ -22,8 +22,10 @@ module Anyway
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def deserialize(raw, type_id, array: false)
|
25
|
+
return if raw.nil?
|
26
|
+
|
25
27
|
caster =
|
26
|
-
if type_id.is_a?(Symbol)
|
28
|
+
if type_id.is_a?(Symbol) || type_id.nil?
|
27
29
|
registry.fetch(type_id) { raise ArgumentError, "Unknown type: #{type_id}" }
|
28
30
|
else
|
29
31
|
raise ArgumentError, "Type must implement #call(val): #{type_id}" unless type_id.respond_to?(:call)
|
@@ -31,7 +33,7 @@ module Anyway
|
|
31
33
|
end
|
32
34
|
|
33
35
|
if array
|
34
|
-
raw_arr = raw.is_a?(
|
36
|
+
raw_arr = raw.is_a?(String) ? raw.split(/\s*,\s*/) : Array(raw)
|
35
37
|
raw_arr.map { |_1| caster.call(_1) }
|
36
38
|
else
|
37
39
|
caster.call(raw)
|
@@ -50,6 +52,7 @@ module Anyway
|
|
50
52
|
end
|
51
53
|
|
52
54
|
TypeRegistry.default.tap do |obj|
|
55
|
+
obj.accept(nil, &:itself)
|
53
56
|
obj.accept(:string, &:to_s)
|
54
57
|
obj.accept(:integer, &:to_i)
|
55
58
|
obj.accept(:float, &:to_f)
|
@@ -57,23 +60,33 @@ module Anyway
|
|
57
60
|
obj.accept(:date) do |_1|
|
58
61
|
require "date" unless defined?(::Date)
|
59
62
|
|
60
|
-
|
63
|
+
next _1 if _1.is_a?(::Date)
|
64
|
+
|
65
|
+
next _1.to_date if _1.respond_to?(:to_date)
|
66
|
+
|
67
|
+
::Date.parse(_1)
|
61
68
|
end
|
62
69
|
|
63
70
|
obj.accept(:datetime) do |_1|
|
64
71
|
require "date" unless defined?(::Date)
|
65
72
|
|
66
|
-
|
73
|
+
next _1 if _1.is_a?(::DateTime)
|
74
|
+
|
75
|
+
next _1.to_datetime if _1.respond_to?(:to_datetime)
|
76
|
+
|
77
|
+
::DateTime.parse(_1)
|
67
78
|
end
|
68
79
|
|
69
80
|
obj.accept(:uri) do |_1|
|
70
81
|
require "uri" unless defined?(::URI)
|
71
82
|
|
72
|
-
|
83
|
+
next _1 if _1.is_a?(::URI)
|
84
|
+
|
85
|
+
::URI.parse(_1)
|
73
86
|
end
|
74
87
|
|
75
88
|
obj.accept(:boolean) do |_1|
|
76
|
-
_1.match?(/\A(true|t|yes|y|1)\z/i)
|
89
|
+
_1.to_s.match?(/\A(true|t|yes|y|1)\z/i)
|
77
90
|
end
|
78
91
|
end
|
79
92
|
|
data/lib/anyway/type_casting.rb
CHANGED
@@ -22,8 +22,10 @@ module Anyway
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def deserialize(raw, type_id, array: false)
|
25
|
+
return if raw.nil?
|
26
|
+
|
25
27
|
caster =
|
26
|
-
if type_id.is_a?(Symbol)
|
28
|
+
if type_id.is_a?(Symbol) || type_id.nil?
|
27
29
|
registry.fetch(type_id) { raise ArgumentError, "Unknown type: #{type_id}" }
|
28
30
|
else
|
29
31
|
raise ArgumentError, "Type must implement #call(val): #{type_id}" unless type_id.respond_to?(:call)
|
@@ -31,7 +33,7 @@ module Anyway
|
|
31
33
|
end
|
32
34
|
|
33
35
|
if array
|
34
|
-
raw_arr = raw.is_a?(
|
36
|
+
raw_arr = raw.is_a?(String) ? raw.split(/\s*,\s*/) : Array(raw)
|
35
37
|
raw_arr.map { caster.call(_1) }
|
36
38
|
else
|
37
39
|
caster.call(raw)
|
@@ -50,6 +52,7 @@ module Anyway
|
|
50
52
|
end
|
51
53
|
|
52
54
|
TypeRegistry.default.tap do |obj|
|
55
|
+
obj.accept(nil, &:itself)
|
53
56
|
obj.accept(:string, &:to_s)
|
54
57
|
obj.accept(:integer, &:to_i)
|
55
58
|
obj.accept(:float, &:to_f)
|
@@ -57,23 +60,33 @@ module Anyway
|
|
57
60
|
obj.accept(:date) do
|
58
61
|
require "date" unless defined?(::Date)
|
59
62
|
|
60
|
-
|
63
|
+
next _1 if _1.is_a?(::Date)
|
64
|
+
|
65
|
+
next _1.to_date if _1.respond_to?(:to_date)
|
66
|
+
|
67
|
+
::Date.parse(_1)
|
61
68
|
end
|
62
69
|
|
63
70
|
obj.accept(:datetime) do
|
64
71
|
require "date" unless defined?(::Date)
|
65
72
|
|
66
|
-
|
73
|
+
next _1 if _1.is_a?(::DateTime)
|
74
|
+
|
75
|
+
next _1.to_datetime if _1.respond_to?(:to_datetime)
|
76
|
+
|
77
|
+
::DateTime.parse(_1)
|
67
78
|
end
|
68
79
|
|
69
80
|
obj.accept(:uri) do
|
70
81
|
require "uri" unless defined?(::URI)
|
71
82
|
|
72
|
-
|
83
|
+
next _1 if _1.is_a?(::URI)
|
84
|
+
|
85
|
+
::URI.parse(_1)
|
73
86
|
end
|
74
87
|
|
75
88
|
obj.accept(:boolean) do
|
76
|
-
_1.match?(/\A(true|t|yes|y|1)\z/i)
|
89
|
+
_1.to_s.match?(/\A(true|t|yes|y|1)\z/i)
|
77
90
|
end
|
78
91
|
end
|
79
92
|
|
data/lib/anyway/version.rb
CHANGED
data/sig/anyway_config.rbs
CHANGED
@@ -51,6 +51,12 @@ module Anyway
|
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
|
+
type valueType = Symbol | nil
|
55
|
+
type arrayType = {array: bool, type: valueType}
|
56
|
+
type hashType = Hash[Symbol, valueType | arrayType | hashType]
|
57
|
+
|
58
|
+
type mappingType = valueType | arrayType | hashType
|
59
|
+
|
54
60
|
class Config
|
55
61
|
extend RBSGenerator
|
56
62
|
extend DynamicConfig::ClassMethods
|
@@ -66,7 +72,7 @@ module Anyway
|
|
66
72
|
def self.on_load: (*Symbol callbacks) ?{ () -> void } -> void
|
67
73
|
def self.config_name: (?(Symbol | String) val) -> String?
|
68
74
|
def self.env_prefix: (?(Symbol | String) val) -> String
|
69
|
-
def self.coerce_types: (
|
75
|
+
def self.coerce_types: (**mappingType) -> void
|
70
76
|
def self.coercion_mapping: -> Hash[untyped, untyped]?
|
71
77
|
def self.disable_auto_cast!: -> void
|
72
78
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: anyway_config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.2.
|
4
|
+
version: 2.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vladimir Dementyev
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
19
|
+
version: 0.11.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.
|
26
|
+
version: 0.11.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: ammeter
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|