dry-types 0.9.4 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +6 -6
- data/CHANGELOG.md +17 -0
- data/lib/dry/types.rb +6 -5
- data/lib/dry/types/any.rb +20 -0
- data/lib/dry/types/array.rb +1 -1
- data/lib/dry/types/array/member.rb +5 -5
- data/lib/dry/types/builder.rb +5 -5
- data/lib/dry/types/coercions.rb +4 -3
- data/lib/dry/types/coercions/form.rb +8 -8
- data/lib/dry/types/coercions/json.rb +1 -1
- data/lib/dry/types/constrained.rb +11 -3
- data/lib/dry/types/constrained/coercible.rb +4 -2
- data/lib/dry/types/constructor.rb +9 -8
- data/lib/dry/types/core.rb +4 -1
- data/lib/dry/types/decorator.rb +6 -5
- data/lib/dry/types/default.rb +4 -3
- data/lib/dry/types/definition.rb +11 -8
- data/lib/dry/types/enum.rb +2 -1
- data/lib/dry/types/errors.rb +2 -2
- data/lib/dry/types/extensions/maybe.rb +1 -0
- data/lib/dry/types/hash.rb +2 -2
- data/lib/dry/types/hash/schema.rb +16 -10
- data/lib/dry/types/options.rb +10 -6
- data/lib/dry/types/safe.rb +3 -2
- data/lib/dry/types/sum.rb +6 -5
- data/lib/dry/types/type.rb +6 -0
- data/lib/dry/types/version.rb +1 -1
- data/lib/spec/dry/types.rb +5 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2df871ef8f32a388ed641c5025ab9838a090f460
|
4
|
+
data.tar.gz: 2766ba15e0358b3ac14b30cbb3027c8887fa609d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 83951c294c86aeeb1bed5ed85bb8e28b6e898ff72170b246012494d892ae9fb19a3d60ed635348e0e45d5048290f7d5bc8c2c0976bd0e42cebc13648537c19c9
|
7
|
+
data.tar.gz: 6a09c6f25ab11725d0c18adffb72eb93d3be87cffba4bb40bdd259a55dd3427953dc1026857540f8b83e0c8099c6748893bbbe9b1bca5dd536a95f7a4bc07836
|
data/.travis.yml
CHANGED
@@ -4,14 +4,14 @@ sudo: required
|
|
4
4
|
cache: bundler
|
5
5
|
bundler_args: --without benchmarks tools
|
6
6
|
after_success:
|
7
|
-
- '[
|
7
|
+
- '[ -d coverage ] && bundle exec codeclimate-test-reporter'
|
8
8
|
script:
|
9
9
|
- bundle exec rake
|
10
10
|
rvm:
|
11
|
-
- 2.
|
12
|
-
- 2.3
|
13
|
-
- 2.
|
14
|
-
- jruby-9.1.
|
11
|
+
- 2.2.7
|
12
|
+
- 2.3.3
|
13
|
+
- 2.4.1
|
14
|
+
- jruby-9.1.8.0
|
15
15
|
- rbx-3
|
16
16
|
env:
|
17
17
|
global:
|
@@ -20,7 +20,7 @@ env:
|
|
20
20
|
matrix:
|
21
21
|
allow_failures:
|
22
22
|
- rvm: rbx-3
|
23
|
-
- rvm: jruby-9.1.
|
23
|
+
- rvm: jruby-9.1.8.0
|
24
24
|
notifications:
|
25
25
|
email: false
|
26
26
|
webhooks:
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,20 @@
|
|
1
|
+
# v0.10.0 2017-04-26
|
2
|
+
|
3
|
+
## Added
|
4
|
+
|
5
|
+
* Types can be used in `case` statements now (GustavoCaso)
|
6
|
+
|
7
|
+
## Fixed
|
8
|
+
|
9
|
+
* Return original value when Date.parse raises a RangeError (jviney)
|
10
|
+
|
11
|
+
## Changed
|
12
|
+
|
13
|
+
* Meta data are now stored separately from options (flash-gordon)
|
14
|
+
* `Types::Object` was renamed to `Types::Any` (flash-gordon)
|
15
|
+
|
16
|
+
[Compare v0.9.4...v0.10.0](https://github.com/dryrb/dry-types/compare/v0.9.4...v0.10.0)
|
17
|
+
|
1
18
|
# v0.9.4 2017-01-24
|
2
19
|
|
3
20
|
## Added
|
data/lib/dry/types.rb
CHANGED
@@ -12,6 +12,7 @@ require 'dry/core/constants'
|
|
12
12
|
|
13
13
|
require 'dry/types/version'
|
14
14
|
require 'dry/types/container'
|
15
|
+
require 'dry/types/type'
|
15
16
|
require 'dry/types/definition'
|
16
17
|
require 'dry/types/constructor'
|
17
18
|
|
@@ -51,8 +52,8 @@ module Dry
|
|
51
52
|
end
|
52
53
|
|
53
54
|
# @param [String] name
|
54
|
-
# @param [
|
55
|
-
# @param [#call] block
|
55
|
+
# @param [Type] type
|
56
|
+
# @param [#call,nil] block
|
56
57
|
# @return [Container{String => Definition}]
|
57
58
|
def self.register(name, type = nil, &block)
|
58
59
|
container.register(name, type || block.call)
|
@@ -67,8 +68,8 @@ module Dry
|
|
67
68
|
container.register(identifier(klass), type)
|
68
69
|
end
|
69
70
|
|
70
|
-
# @param [String] name
|
71
|
-
# @return [
|
71
|
+
# @param [String,Class] name
|
72
|
+
# @return [Type,Class]
|
72
73
|
def self.[](name)
|
73
74
|
type_map.fetch_or_store(name) do
|
74
75
|
case name
|
@@ -93,7 +94,7 @@ module Dry
|
|
93
94
|
end
|
94
95
|
end
|
95
96
|
|
96
|
-
# @param [
|
97
|
+
# @param [Module] namespace
|
97
98
|
# @param [<String>] identifiers
|
98
99
|
# @return [<Definition>]
|
99
100
|
def self.define_constants(namespace, identifiers)
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Dry
|
2
|
+
module Types
|
3
|
+
Any = Class.new(Definition) do
|
4
|
+
def initialize
|
5
|
+
super(::Object)
|
6
|
+
end
|
7
|
+
|
8
|
+
# @return [String]
|
9
|
+
def name
|
10
|
+
'Any'
|
11
|
+
end
|
12
|
+
|
13
|
+
# @param [Object] any input is valid
|
14
|
+
# @return [true]
|
15
|
+
def valid?(_)
|
16
|
+
true
|
17
|
+
end
|
18
|
+
end.new
|
19
|
+
end
|
20
|
+
end
|
data/lib/dry/types/array.rb
CHANGED
@@ -2,12 +2,12 @@ module Dry
|
|
2
2
|
module Types
|
3
3
|
class Array < Definition
|
4
4
|
class Member < Array
|
5
|
-
# @return [
|
5
|
+
# @return [Type]
|
6
6
|
attr_reader :member
|
7
7
|
|
8
8
|
# @param [Class] primitive
|
9
9
|
# @param [Hash] options
|
10
|
-
# @option options [
|
10
|
+
# @option options [Type] :member
|
11
11
|
def initialize(primitive, options = {})
|
12
12
|
@member = options.fetch(:member)
|
13
13
|
super
|
@@ -21,17 +21,17 @@ module Dry
|
|
21
21
|
end
|
22
22
|
alias_method :[], :call
|
23
23
|
|
24
|
-
# @param [Array, #all
|
24
|
+
# @param [Array, #all?, Object] value
|
25
25
|
# @return [Boolean]
|
26
26
|
def valid?(value)
|
27
27
|
super && value.all? { |el| member.valid?(el) }
|
28
28
|
end
|
29
29
|
|
30
30
|
# @param [Array, Object] input
|
31
|
-
# @param [#call] block
|
31
|
+
# @param [#call,nil] block
|
32
32
|
# @yieldparam [Failure] failure
|
33
33
|
# @yieldreturn [Result]
|
34
|
-
# @return [Result]
|
34
|
+
# @return [Result,Logic::Result]
|
35
35
|
def try(input, &block)
|
36
36
|
if input.is_a?(::Array)
|
37
37
|
result = call(input, :try)
|
data/lib/dry/types/builder.rb
CHANGED
@@ -3,12 +3,12 @@ module Dry
|
|
3
3
|
module Builder
|
4
4
|
include Dry::Core::Constants
|
5
5
|
|
6
|
-
# @return [
|
6
|
+
# @return [Class]
|
7
7
|
def constrained_type
|
8
8
|
Constrained
|
9
9
|
end
|
10
10
|
|
11
|
-
# @param [
|
11
|
+
# @param [Type] other
|
12
12
|
# @return [Sum, Sum::Constrained]
|
13
13
|
def |(other)
|
14
14
|
klass = constrained? && other.constrained? ? Sum::Constrained : Sum
|
@@ -27,7 +27,7 @@ module Dry
|
|
27
27
|
end
|
28
28
|
|
29
29
|
# @param [Object] input
|
30
|
-
# @param [#call] block
|
30
|
+
# @param [#call,nil] block
|
31
31
|
# @raise [ConstraintError]
|
32
32
|
# @return [Default]
|
33
33
|
def default(input = Undefined, &block)
|
@@ -51,9 +51,9 @@ module Dry
|
|
51
51
|
Safe.new(self)
|
52
52
|
end
|
53
53
|
|
54
|
-
# @param [#call] constructor
|
54
|
+
# @param [#call,nil] constructor
|
55
55
|
# @param [Hash] options
|
56
|
-
# @param [#call] block
|
56
|
+
# @param [#call,nil] block
|
57
57
|
# @return [Constructor]
|
58
58
|
def constructor(constructor = nil, **options, &block)
|
59
59
|
Constructor.new(with(options), fn: constructor || block)
|
data/lib/dry/types/coercions.rb
CHANGED
@@ -3,8 +3,9 @@ module Dry
|
|
3
3
|
module Coercions
|
4
4
|
include Dry::Core::Constants
|
5
5
|
|
6
|
-
# @param [String] input
|
7
|
-
# @return [
|
6
|
+
# @param [String, Object] input
|
7
|
+
# @return [nil] if the input is an empty string
|
8
|
+
# @return [Object] otherwise the input object is returned
|
8
9
|
def to_nil(input)
|
9
10
|
input unless empty_str?(input)
|
10
11
|
end
|
@@ -15,7 +16,7 @@ module Dry
|
|
15
16
|
def to_date(input)
|
16
17
|
return input unless input.respond_to?(:to_str)
|
17
18
|
Date.parse(input)
|
18
|
-
rescue ArgumentError
|
19
|
+
rescue ArgumentError, RangeError
|
19
20
|
input
|
20
21
|
end
|
21
22
|
|
@@ -12,7 +12,7 @@ module Dry
|
|
12
12
|
extend Coercions
|
13
13
|
|
14
14
|
# @param [String, Object] input
|
15
|
-
# @return [Boolean
|
15
|
+
# @return [Boolean,Object]
|
16
16
|
# @see TRUE_VALUES
|
17
17
|
# @see FALSE_VALUES
|
18
18
|
def self.to_true(input)
|
@@ -20,7 +20,7 @@ module Dry
|
|
20
20
|
end
|
21
21
|
|
22
22
|
# @param [String, Object] input
|
23
|
-
# @return [Boolean
|
23
|
+
# @return [Boolean,Object]
|
24
24
|
# @see TRUE_VALUES
|
25
25
|
# @see FALSE_VALUES
|
26
26
|
def self.to_false(input)
|
@@ -28,7 +28,7 @@ module Dry
|
|
28
28
|
end
|
29
29
|
|
30
30
|
# @param [#to_int, #to_i, Object] input
|
31
|
-
# @return [Integer
|
31
|
+
# @return [Integer, nil, Object]
|
32
32
|
def self.to_int(input)
|
33
33
|
if empty_str?(input)
|
34
34
|
nil
|
@@ -40,7 +40,7 @@ module Dry
|
|
40
40
|
end
|
41
41
|
|
42
42
|
# @param [#to_f, Object] input
|
43
|
-
# @return [Float
|
43
|
+
# @return [Float, nil, Object]
|
44
44
|
def self.to_float(input)
|
45
45
|
if empty_str?(input)
|
46
46
|
nil
|
@@ -52,7 +52,7 @@ module Dry
|
|
52
52
|
end
|
53
53
|
|
54
54
|
# @param [#to_d, Object] input
|
55
|
-
# @return [BigDecimal
|
55
|
+
# @return [BigDecimal, nil, Object]
|
56
56
|
def self.to_decimal(input)
|
57
57
|
result = to_float(input)
|
58
58
|
|
@@ -63,14 +63,14 @@ module Dry
|
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
|
-
# @param [Array,
|
66
|
+
# @param [Array, String, Object] input
|
67
67
|
# @return [Array, Object]
|
68
68
|
def self.to_ary(input)
|
69
69
|
empty_str?(input) ? [] : input
|
70
70
|
end
|
71
71
|
|
72
|
-
# @param [Hash,
|
73
|
-
# @return [Hash]
|
72
|
+
# @param [Hash, String, Object] input
|
73
|
+
# @return [Hash, Object]
|
74
74
|
def self.to_hash(input)
|
75
75
|
empty_str?(input) ? {} : input
|
76
76
|
end
|
@@ -5,6 +5,7 @@ require 'dry/types/constrained/coercible'
|
|
5
5
|
module Dry
|
6
6
|
module Types
|
7
7
|
class Constrained
|
8
|
+
include Type
|
8
9
|
include Dry::Equalizer(:type, :options, :rule)
|
9
10
|
include Decorator
|
10
11
|
include Builder
|
@@ -12,7 +13,7 @@ module Dry
|
|
12
13
|
# @return [Dry::Logic::Rule]
|
13
14
|
attr_reader :rule
|
14
15
|
|
15
|
-
# @param [
|
16
|
+
# @param [Type] type
|
16
17
|
# @param [Hash] options
|
17
18
|
def initialize(type, options)
|
18
19
|
super
|
@@ -30,10 +31,11 @@ module Dry
|
|
30
31
|
alias_method :[], :call
|
31
32
|
|
32
33
|
# @param [Object] input
|
33
|
-
# @param [#call] block
|
34
|
+
# @param [#call,nil] block
|
34
35
|
# @yieldparam [Failure] failure
|
35
36
|
# @yieldreturn [Result]
|
36
|
-
# @return [Result]
|
37
|
+
# @return [Logic::Result, Result]
|
38
|
+
# @return [Object] if block given and try fails
|
37
39
|
def try(input, &block)
|
38
40
|
result = rule.(input)
|
39
41
|
|
@@ -65,6 +67,12 @@ module Dry
|
|
65
67
|
true
|
66
68
|
end
|
67
69
|
|
70
|
+
# @param [Object] value
|
71
|
+
# @return [Boolean]
|
72
|
+
def ===(value)
|
73
|
+
valid?(value)
|
74
|
+
end
|
75
|
+
|
68
76
|
private
|
69
77
|
|
70
78
|
# @param [Object] response
|
@@ -1,12 +1,14 @@
|
|
1
1
|
module Dry
|
2
2
|
module Types
|
3
3
|
class Constrained
|
4
|
+
include Type
|
5
|
+
|
4
6
|
class Coercible < Constrained
|
5
7
|
# @param [Object] input
|
6
|
-
# @param [#call] block
|
8
|
+
# @param [#call,nil] block
|
7
9
|
# @yieldparam [Failure] failure
|
8
10
|
# @yieldreturn [Result]
|
9
|
-
# @return [Result]
|
11
|
+
# @return [Result,Logic::Result,nil]
|
10
12
|
def try(input, &block)
|
11
13
|
result = type.try(input)
|
12
14
|
|
@@ -8,20 +8,20 @@ module Dry
|
|
8
8
|
# @return [#call]
|
9
9
|
attr_reader :fn
|
10
10
|
|
11
|
-
# @return [
|
11
|
+
# @return [Type]
|
12
12
|
attr_reader :type
|
13
13
|
|
14
14
|
# @param [Builder, Object] input
|
15
15
|
# @param [Hash] options
|
16
|
-
# @param [#call] block
|
16
|
+
# @param [#call, nil] block
|
17
17
|
def self.new(input, options = {}, &block)
|
18
18
|
type = input.is_a?(Builder) ? input : Definition.new(input)
|
19
19
|
super(type, options, &block)
|
20
20
|
end
|
21
21
|
|
22
|
-
# @param [
|
22
|
+
# @param [Type] type
|
23
23
|
# @param [Hash] options
|
24
|
-
# @param [#
|
24
|
+
# @param [#call, nil] block
|
25
25
|
def initialize(type, options = {}, &block)
|
26
26
|
@type = type
|
27
27
|
@fn = options.fetch(:fn, block)
|
@@ -41,8 +41,9 @@ module Dry
|
|
41
41
|
alias_method :[], :call
|
42
42
|
|
43
43
|
# @param [Object] input
|
44
|
-
# @param [#call] block
|
45
|
-
# @return [Result]
|
44
|
+
# @param [#call,nil] block
|
45
|
+
# @return [Logic::Result, Types::Result]
|
46
|
+
# @return [Object] if block given and try fails
|
46
47
|
def try(input, &block)
|
47
48
|
type.try(fn[input], &block)
|
48
49
|
rescue TypeError => e
|
@@ -51,7 +52,7 @@ module Dry
|
|
51
52
|
|
52
53
|
# @param [#call, nil] new_fn
|
53
54
|
# @param [Hash] options
|
54
|
-
# @param [#call] block
|
55
|
+
# @param [#call, nil] block
|
55
56
|
# @return [Constructor]
|
56
57
|
def constructor(new_fn = nil, **options, &block)
|
57
58
|
left = new_fn || block
|
@@ -83,7 +84,7 @@ module Dry
|
|
83
84
|
# Delegates missing methods to {#type}
|
84
85
|
# @param [Symbol] meth
|
85
86
|
# @param [Array] args
|
86
|
-
# @param [#call] block
|
87
|
+
# @param [#call, nil] block
|
87
88
|
def method_missing(meth, *args, &block)
|
88
89
|
if type.respond_to?(meth)
|
89
90
|
response = type.__send__(meth, *args, &block)
|
data/lib/dry/types/core.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'dry/types/any'
|
2
|
+
|
1
3
|
module Dry
|
2
4
|
module Types
|
3
5
|
COERCIBLE = {
|
@@ -53,7 +55,8 @@ module Dry
|
|
53
55
|
register("bool", self["true"] | self["false"])
|
54
56
|
register("strict.bool", self["strict.true"] | self["strict.false"])
|
55
57
|
|
56
|
-
register("
|
58
|
+
register("any", Any)
|
59
|
+
register("object", self['any'])
|
57
60
|
end
|
58
61
|
end
|
59
62
|
|
data/lib/dry/types/decorator.rb
CHANGED
@@ -5,10 +5,10 @@ module Dry
|
|
5
5
|
module Decorator
|
6
6
|
include Options
|
7
7
|
|
8
|
-
# @return [
|
8
|
+
# @return [Type]
|
9
9
|
attr_reader :type
|
10
10
|
|
11
|
-
# @param [
|
11
|
+
# @param [Type] type
|
12
12
|
def initialize(type, *)
|
13
13
|
super
|
14
14
|
@type = type
|
@@ -20,8 +20,9 @@ module Dry
|
|
20
20
|
end
|
21
21
|
|
22
22
|
# @param [Object] input
|
23
|
-
# @param [#call] block
|
24
|
-
# @return [Result]
|
23
|
+
# @param [#call, nil] block
|
24
|
+
# @return [Result,Logic::Result]
|
25
|
+
# @return [Object] if block given and try fails
|
25
26
|
def try(input, &block)
|
26
27
|
type.try(input, &block)
|
27
28
|
end
|
@@ -60,7 +61,7 @@ module Dry
|
|
60
61
|
# Delegates missing methods to {#type}
|
61
62
|
# @param [Symbol] meth
|
62
63
|
# @param [Array] args
|
63
|
-
# @param [#call] block
|
64
|
+
# @param [#call, nil] block
|
64
65
|
def method_missing(meth, *args, &block)
|
65
66
|
if type.respond_to?(meth)
|
66
67
|
response = type.__send__(meth, *args, &block)
|
data/lib/dry/types/default.rb
CHANGED
@@ -3,6 +3,7 @@ require 'dry/types/decorator'
|
|
3
3
|
module Dry
|
4
4
|
module Types
|
5
5
|
class Default
|
6
|
+
include Type
|
6
7
|
include Dry::Equalizer(:type, :options, :value)
|
7
8
|
include Decorator
|
8
9
|
include Builder
|
@@ -23,7 +24,7 @@ module Dry
|
|
23
24
|
alias_method :evaluate, :value
|
24
25
|
|
25
26
|
# @param [Object, #call] value
|
26
|
-
# @return [Default
|
27
|
+
# @return [Class] {Default} or {Default::Callable}
|
27
28
|
def self.[](value)
|
28
29
|
if value.respond_to?(:call)
|
29
30
|
Callable
|
@@ -32,7 +33,7 @@ module Dry
|
|
32
33
|
end
|
33
34
|
end
|
34
35
|
|
35
|
-
# @param [
|
36
|
+
# @param [Type] type
|
36
37
|
# @param [Object] value
|
37
38
|
def initialize(type, value, *)
|
38
39
|
super
|
@@ -51,7 +52,7 @@ module Dry
|
|
51
52
|
end
|
52
53
|
|
53
54
|
# @param [Object] input
|
54
|
-
# @return [Success]
|
55
|
+
# @return [Result::Success]
|
55
56
|
def try(input)
|
56
57
|
success(call(input))
|
57
58
|
end
|
data/lib/dry/types/definition.rb
CHANGED
@@ -5,6 +5,7 @@ require 'dry/types/options'
|
|
5
5
|
module Dry
|
6
6
|
module Types
|
7
7
|
class Definition
|
8
|
+
include Type
|
8
9
|
include Dry::Equalizer(:primitive, :options)
|
9
10
|
include Options
|
10
11
|
include Builder
|
@@ -16,7 +17,7 @@ module Dry
|
|
16
17
|
attr_reader :primitive
|
17
18
|
|
18
19
|
# @param [Class] primitive
|
19
|
-
# @return [
|
20
|
+
# @return [Type]
|
20
21
|
def self.[](primitive)
|
21
22
|
if primitive == ::Array
|
22
23
|
Types::Array
|
@@ -27,7 +28,7 @@ module Dry
|
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
30
|
-
# @param [Class] primitive
|
31
|
+
# @param [Type,Class] primitive
|
31
32
|
# @param [Hash] options
|
32
33
|
def initialize(primitive, options = {})
|
33
34
|
super
|
@@ -50,18 +51,19 @@ module Dry
|
|
50
51
|
false
|
51
52
|
end
|
52
53
|
|
53
|
-
# @param [
|
54
|
-
# @return [
|
54
|
+
# @param [BasicObject] input
|
55
|
+
# @return [BasicObject]
|
55
56
|
def call(input)
|
56
57
|
input
|
57
58
|
end
|
58
59
|
alias_method :[], :call
|
59
60
|
|
60
61
|
# @param [Object] input
|
61
|
-
# @param [#call] block
|
62
|
+
# @param [#call,nil] block
|
62
63
|
# @yieldparam [Failure] failure
|
63
64
|
# @yieldreturn [Result]
|
64
|
-
# @return [Result]
|
65
|
+
# @return [Result,Logic::Result] when a block is not provided
|
66
|
+
# @return [nil] otherwise
|
65
67
|
def try(input, &block)
|
66
68
|
if valid?(input)
|
67
69
|
success(input)
|
@@ -72,14 +74,14 @@ module Dry
|
|
72
74
|
end
|
73
75
|
|
74
76
|
# @param (see Dry::Types::Success#initialize)
|
75
|
-
# @return [Success]
|
77
|
+
# @return [Result::Success]
|
76
78
|
def success(input)
|
77
79
|
Result::Success.new(input)
|
78
80
|
end
|
79
81
|
|
80
82
|
|
81
83
|
# @param (see Failure#initialize)
|
82
|
-
# @return [Failure]
|
84
|
+
# @return [Result::Failure]
|
83
85
|
def failure(input, error)
|
84
86
|
Result::Failure.new(input, error)
|
85
87
|
end
|
@@ -98,6 +100,7 @@ module Dry
|
|
98
100
|
value.is_a?(primitive)
|
99
101
|
end
|
100
102
|
alias_method :valid?, :primitive?
|
103
|
+
alias_method :===, :primitive?
|
101
104
|
end
|
102
105
|
end
|
103
106
|
end
|
data/lib/dry/types/enum.rb
CHANGED
@@ -3,6 +3,7 @@ require 'dry/types/decorator'
|
|
3
3
|
module Dry
|
4
4
|
module Types
|
5
5
|
class Enum
|
6
|
+
include Type
|
6
7
|
include Dry::Equalizer(:type, :options, :values)
|
7
8
|
include Decorator
|
8
9
|
|
@@ -12,7 +13,7 @@ module Dry
|
|
12
13
|
# @return [Hash]
|
13
14
|
attr_reader :mapping
|
14
15
|
|
15
|
-
# @param [
|
16
|
+
# @param [Type] type
|
16
17
|
# @param [Hash] options
|
17
18
|
# @option options [Array] :values
|
18
19
|
def initialize(type, options)
|
data/lib/dry/types/errors.rb
CHANGED
@@ -7,7 +7,7 @@ module Dry
|
|
7
7
|
setting :namespace, self
|
8
8
|
|
9
9
|
class SchemaError < TypeError
|
10
|
-
# @param [String] key
|
10
|
+
# @param [String,Symbol] key
|
11
11
|
# @param [Object] value
|
12
12
|
def initialize(key, value)
|
13
13
|
super("#{value.inspect} (#{value.class}) has invalid type for :#{key}")
|
@@ -18,7 +18,7 @@ module Dry
|
|
18
18
|
private_constant(:SchemaKeyError)
|
19
19
|
|
20
20
|
class MissingKeyError < SchemaKeyError
|
21
|
-
# @param [String] key
|
21
|
+
# @param [String,Symbol] key
|
22
22
|
def initialize(key)
|
23
23
|
super(":#{key} is missing in Hash input")
|
24
24
|
end
|
data/lib/dry/types/hash.rb
CHANGED
@@ -17,7 +17,7 @@ module Dry
|
|
17
17
|
end
|
18
18
|
}
|
19
19
|
|
20
|
-
klass.new(primitive, options.merge(member_types: member_types))
|
20
|
+
klass.new(primitive, options.merge(member_types: member_types, meta: meta))
|
21
21
|
end
|
22
22
|
|
23
23
|
# @param [{Symbol => Definition}] type_map
|
@@ -54,7 +54,7 @@ module Dry
|
|
54
54
|
|
55
55
|
# @param [Hash] _result
|
56
56
|
# @param [Symbol] _key
|
57
|
-
# @param [
|
57
|
+
# @param [Type] _type
|
58
58
|
def resolve_missing_value(_result, _key, _type)
|
59
59
|
# noop
|
60
60
|
end
|
@@ -29,10 +29,11 @@ module Dry
|
|
29
29
|
alias_method :[], :call
|
30
30
|
|
31
31
|
# @param [Hash] hash
|
32
|
-
# @param [#call] block
|
32
|
+
# @param [#call,nil] block
|
33
33
|
# @yieldparam [Failure] failure
|
34
34
|
# @yieldreturn [Result]
|
35
|
-
# @return [Result]
|
35
|
+
# @return [Logic::Result]
|
36
|
+
# @return [Object] if coercion fails and a block is given
|
36
37
|
def try(hash, &block)
|
37
38
|
success = true
|
38
39
|
output = {}
|
@@ -95,7 +96,7 @@ module Dry
|
|
95
96
|
|
96
97
|
# @param [Hash] result
|
97
98
|
# @param [Symbol] key
|
98
|
-
# @param [
|
99
|
+
# @param [Type] type
|
99
100
|
# @return [Object]
|
100
101
|
# @see Dry::Types::Default#evaluate
|
101
102
|
# @see Dry::Types::Default::Callable#evaluate
|
@@ -135,6 +136,10 @@ module Dry
|
|
135
136
|
# @return [Hash{Symbol => Object}]
|
136
137
|
# @raise [UnknownKeysError]
|
137
138
|
# if there any unexpected key in given hash
|
139
|
+
# @raise [MissingKeyError]
|
140
|
+
# if a required key is not present
|
141
|
+
# @raise [SchemaError]
|
142
|
+
# if a value is the wrong type
|
138
143
|
def resolve(hash)
|
139
144
|
unexpected = hash.keys - member_types.keys
|
140
145
|
raise UnknownKeysError.new(*unexpected) unless unexpected.empty?
|
@@ -157,7 +162,7 @@ module Dry
|
|
157
162
|
|
158
163
|
# @param [Hash] result
|
159
164
|
# @param [Symbol] key
|
160
|
-
# @param [
|
165
|
+
# @param [Type] type
|
161
166
|
# @return [Object]
|
162
167
|
# @see Dry::Types::Default#evaluate
|
163
168
|
# @see Dry::Types::Default::Callable#evaluate
|
@@ -184,16 +189,17 @@ module Dry
|
|
184
189
|
super(primitive, options.merge(member_types: member_types))
|
185
190
|
end
|
186
191
|
|
187
|
-
# @param [
|
188
|
-
# @param [#call] block
|
192
|
+
# @param [Object] value
|
193
|
+
# @param [#call, nil] block
|
189
194
|
# @yieldparam [Failure] failure
|
190
195
|
# @yieldreturn [Result]
|
191
|
-
# @return [
|
192
|
-
|
193
|
-
|
196
|
+
# @return [Object] if block given
|
197
|
+
# @return [Result,Logic::Result] otherwise
|
198
|
+
def try(value, &block)
|
199
|
+
if value.is_a?(::Hash)
|
194
200
|
super
|
195
201
|
else
|
196
|
-
result = failure(
|
202
|
+
result = failure(value, "#{value} must be a hash")
|
197
203
|
block ? yield(result) : result
|
198
204
|
end
|
199
205
|
end
|
data/lib/dry/types/options.rb
CHANGED
@@ -5,20 +5,24 @@ module Dry
|
|
5
5
|
attr_reader :options
|
6
6
|
|
7
7
|
# @see Definition#initialize
|
8
|
-
def initialize(*args, **options)
|
8
|
+
def initialize(*args, meta: EMPTY_HASH, **options)
|
9
9
|
@__args__ = args
|
10
10
|
@options = options
|
11
|
-
@meta =
|
11
|
+
@meta = meta
|
12
12
|
end
|
13
13
|
|
14
14
|
# @param [Hash] new_options
|
15
|
-
# @return [
|
15
|
+
# @return [Type]
|
16
16
|
def with(new_options)
|
17
|
-
self.class.new(*@__args__, options
|
17
|
+
self.class.new(*@__args__, **options, meta: @meta, **new_options)
|
18
18
|
end
|
19
19
|
|
20
|
-
# @
|
21
|
-
#
|
20
|
+
# @overload meta
|
21
|
+
# @return [Hash] metadata associated with type
|
22
|
+
#
|
23
|
+
# @overload meta(data)
|
24
|
+
# @param [Hash] new metadata to merge into existing metadata
|
25
|
+
# @return [Type] new type with added metadata
|
22
26
|
def meta(data = nil)
|
23
27
|
data ? with(meta: @meta.merge(data)) : @meta
|
24
28
|
end
|
data/lib/dry/types/safe.rb
CHANGED
@@ -3,6 +3,7 @@ require 'dry/types/decorator'
|
|
3
3
|
module Dry
|
4
4
|
module Types
|
5
5
|
class Safe
|
6
|
+
include Type
|
6
7
|
include Dry::Equalizer(:type, :options)
|
7
8
|
include Decorator
|
8
9
|
include Builder
|
@@ -21,10 +22,10 @@ module Dry
|
|
21
22
|
alias_method :[], :call
|
22
23
|
|
23
24
|
# @param [Object] input
|
24
|
-
# @param [#call] block
|
25
|
+
# @param [#call,nil] block
|
25
26
|
# @yieldparam [Failure] failure
|
26
27
|
# @yieldreturn [Result]
|
27
|
-
# @return [Result]
|
28
|
+
# @return [Result,Logic::Result]
|
28
29
|
def try(input, &block)
|
29
30
|
type.try(input, &block)
|
30
31
|
rescue TypeError, ArgumentError => e
|
data/lib/dry/types/sum.rb
CHANGED
@@ -3,18 +3,19 @@ require 'dry/types/options'
|
|
3
3
|
module Dry
|
4
4
|
module Types
|
5
5
|
class Sum
|
6
|
+
include Type
|
6
7
|
include Dry::Equalizer(:left, :right, :options)
|
7
8
|
include Builder
|
8
9
|
include Options
|
9
10
|
|
10
|
-
# @return [
|
11
|
+
# @return [Type]
|
11
12
|
attr_reader :left
|
12
13
|
|
13
|
-
# @return [
|
14
|
+
# @return [Type]
|
14
15
|
attr_reader :right
|
15
16
|
|
16
17
|
class Constrained < Sum
|
17
|
-
# @return [Dry::Logic::
|
18
|
+
# @return [Dry::Logic::Operations::Or]
|
18
19
|
def rule
|
19
20
|
left.rule | right.rule
|
20
21
|
end
|
@@ -35,8 +36,8 @@ module Dry
|
|
35
36
|
alias_method :[], :call
|
36
37
|
end
|
37
38
|
|
38
|
-
# @param [
|
39
|
-
# @param [
|
39
|
+
# @param [Type] left
|
40
|
+
# @param [Type] right
|
40
41
|
# @param [Hash] options
|
41
42
|
def initialize(left, right, options = {})
|
42
43
|
super
|
data/lib/dry/types/version.rb
CHANGED
data/lib/spec/dry/types.rb
CHANGED
@@ -42,6 +42,11 @@ RSpec.shared_examples_for 'Dry::Types::Definition#meta' do
|
|
42
42
|
expect(with_meta).to be_instance_of(type.class)
|
43
43
|
expect(with_meta.meta).to eql(foo: :bar, baz: '1')
|
44
44
|
end
|
45
|
+
|
46
|
+
it "doesn't use meta in equality checks" do
|
47
|
+
expect(type.meta(foo: :bar)).to eql(type)
|
48
|
+
expect(type.meta(foo: :bar).hash).to eql(type.hash)
|
49
|
+
end
|
45
50
|
end
|
46
51
|
end
|
47
52
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry-types
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Solnica
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-04-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|
@@ -217,6 +217,7 @@ files:
|
|
217
217
|
- dry-types.gemspec
|
218
218
|
- lib/dry-types.rb
|
219
219
|
- lib/dry/types.rb
|
220
|
+
- lib/dry/types/any.rb
|
220
221
|
- lib/dry/types/array.rb
|
221
222
|
- lib/dry/types/array/member.rb
|
222
223
|
- lib/dry/types/builder.rb
|
@@ -245,6 +246,7 @@ files:
|
|
245
246
|
- lib/dry/types/result.rb
|
246
247
|
- lib/dry/types/safe.rb
|
247
248
|
- lib/dry/types/sum.rb
|
249
|
+
- lib/dry/types/type.rb
|
248
250
|
- lib/dry/types/version.rb
|
249
251
|
- lib/spec/dry/types.rb
|
250
252
|
homepage: https://github.com/dryrb/dry-types
|
@@ -268,7 +270,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
268
270
|
version: '0'
|
269
271
|
requirements: []
|
270
272
|
rubyforge_project:
|
271
|
-
rubygems_version: 2.
|
273
|
+
rubygems_version: 2.6.9
|
272
274
|
signing_key:
|
273
275
|
specification_version: 4
|
274
276
|
summary: Type system for Ruby supporting coercions, constraints and complex types
|