dry-types 1.4.0 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +78 -0
  3. data/LICENSE +1 -1
  4. data/README.md +1 -1
  5. data/dry-types.gemspec +2 -3
  6. data/lib/dry-types.rb +1 -1
  7. data/lib/dry/types.rb +55 -31
  8. data/lib/dry/types/any.rb +2 -2
  9. data/lib/dry/types/array.rb +2 -2
  10. data/lib/dry/types/array/constructor.rb +1 -1
  11. data/lib/dry/types/array/member.rb +1 -1
  12. data/lib/dry/types/builder.rb +66 -18
  13. data/lib/dry/types/builder_methods.rb +1 -2
  14. data/lib/dry/types/coercions/json.rb +5 -5
  15. data/lib/dry/types/coercions/params.rb +3 -3
  16. data/lib/dry/types/compiler.rb +10 -10
  17. data/lib/dry/types/constrained.rb +6 -9
  18. data/lib/dry/types/constraints.rb +3 -3
  19. data/lib/dry/types/constructor.rb +40 -6
  20. data/lib/dry/types/constructor/function.rb +32 -2
  21. data/lib/dry/types/constructor/wrapper.rb +94 -0
  22. data/lib/dry/types/container.rb +1 -1
  23. data/lib/dry/types/core.rb +12 -12
  24. data/lib/dry/types/decorator.rb +2 -2
  25. data/lib/dry/types/default.rb +14 -1
  26. data/lib/dry/types/enum.rb +4 -3
  27. data/lib/dry/types/errors.rb +1 -1
  28. data/lib/dry/types/extensions.rb +2 -2
  29. data/lib/dry/types/extensions/maybe.rb +5 -4
  30. data/lib/dry/types/extensions/monads.rb +1 -1
  31. data/lib/dry/types/fn_container.rb +1 -1
  32. data/lib/dry/types/hash.rb +9 -15
  33. data/lib/dry/types/hash/constructor.rb +1 -1
  34. data/lib/dry/types/inflector.rb +1 -1
  35. data/lib/dry/types/json.rb +15 -15
  36. data/lib/dry/types/lax.rb +2 -2
  37. data/lib/dry/types/map.rb +2 -2
  38. data/lib/dry/types/meta.rb +1 -1
  39. data/lib/dry/types/module.rb +6 -6
  40. data/lib/dry/types/nominal.rb +11 -11
  41. data/lib/dry/types/params.rb +30 -28
  42. data/lib/dry/types/predicate_inferrer.rb +51 -11
  43. data/lib/dry/types/predicate_registry.rb +1 -1
  44. data/lib/dry/types/primitive_inferrer.rb +1 -1
  45. data/lib/dry/types/printer.rb +25 -25
  46. data/lib/dry/types/result.rb +1 -1
  47. data/lib/dry/types/schema.rb +5 -13
  48. data/lib/dry/types/schema/key.rb +4 -4
  49. data/lib/dry/types/spec/types.rb +57 -45
  50. data/lib/dry/types/sum.rb +4 -3
  51. data/lib/dry/types/type.rb +1 -1
  52. data/lib/dry/types/version.rb +1 -1
  53. metadata +9 -22
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/types/options'
3
+ require "dry/types/options"
4
4
 
5
5
  module Dry
6
6
  module Types
@@ -100,7 +100,7 @@ module Dry
100
100
  #
101
101
  # @api private
102
102
  def __new__(type)
103
- self.class.new(type, *@__args__[1..-1], **@options)
103
+ self.class.new(type, *@__args__.drop(1), **@options)
104
104
  end
105
105
  end
106
106
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/types/decorator'
3
+ require "dry/core/equalizer"
4
+ require "dry/types/decorator"
4
5
 
5
6
  module Dry
6
7
  module Types
@@ -17,6 +18,11 @@ module Dry
17
18
  def evaluate
18
19
  value.call(type)
19
20
  end
21
+
22
+ # @return [true]
23
+ def callable?
24
+ true
25
+ end
20
26
  end
21
27
 
22
28
  include Type
@@ -111,6 +117,13 @@ module Dry
111
117
  Undefined.default(type.call_safe(input, &block)) { evaluate }
112
118
  end
113
119
  end
120
+
121
+ # @return [false]
122
+ #
123
+ # @api private
124
+ def callable?
125
+ false
126
+ end
114
127
  end
115
128
  end
116
129
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/types/decorator'
3
+ require "dry/core/equalizer"
4
+ require "dry/types/decorator"
4
5
 
5
6
  module Dry
6
7
  module Types
@@ -58,8 +59,8 @@ module Dry
58
59
 
59
60
  # @api private
60
61
  def default(*)
61
- raise '.enum(*values).default(value) is not supported. Call '\
62
- '.default(value).enum(*values) instead'
62
+ raise ".enum(*values).default(value) is not supported. Call "\
63
+ ".default(value).enum(*values) instead"
63
64
  end
64
65
 
65
66
  # Check whether a value is in the enum
@@ -56,7 +56,7 @@ module Dry
56
56
 
57
57
  # @return string
58
58
  def message
59
- errors.map(&:message).join(', ')
59
+ errors.map(&:message).join(", ")
60
60
  end
61
61
 
62
62
  # @return [Array]
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Dry::Types.register_extension(:maybe) do
4
- require 'dry/types/extensions/maybe'
4
+ require "dry/types/extensions/maybe"
5
5
  end
6
6
 
7
7
  Dry::Types.register_extension(:monads) do
8
- require 'dry/types/extensions/monads'
8
+ require "dry/types/extensions/monads"
9
9
  end
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/monads/maybe'
4
- require 'dry/types/decorator'
3
+ require "dry/core/equalizer"
4
+ require "dry/monads/maybe"
5
+ require "dry/types/decorator"
5
6
 
6
7
  module Dry
7
8
  module Types
@@ -79,7 +80,7 @@ module Dry
79
80
  # @api public
80
81
  def default(value)
81
82
  if value.nil?
82
- raise ArgumentError, 'nil cannot be used as a default of a maybe type'
83
+ raise ArgumentError, "nil cannot be used as a default of a maybe type"
83
84
  else
84
85
  super
85
86
  end
@@ -93,7 +94,7 @@ module Dry
93
94
  #
94
95
  # @api public
95
96
  def maybe
96
- Maybe.new(Types['nil'] | self)
97
+ Maybe.new(Types["nil"] | self)
97
98
  end
98
99
  end
99
100
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/monads/result'
3
+ require "dry/monads/result"
4
4
 
5
5
  module Dry
6
6
  module Types
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/types/container'
3
+ require "dry/types/container"
4
4
 
5
5
  module Dry
6
6
  module Types
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/types/hash/constructor'
3
+ require "dry/types/hash/constructor"
4
4
 
5
5
  module Dry
6
6
  module Types
@@ -8,7 +8,7 @@ module Dry
8
8
  #
9
9
  # @api public
10
10
  class Hash < Nominal
11
- NOT_REQUIRED = { required: false }.freeze
11
+ NOT_REQUIRED = {required: false}.freeze
12
12
 
13
13
  # @overload schema(type_map, meta = EMPTY_HASH)
14
14
  # @param [{Symbol => Dry::Types::Nominal}] type_map
@@ -50,8 +50,8 @@ module Dry
50
50
 
51
51
  # @api private
52
52
  def weak(*)
53
- raise 'Support for old hash schemas was removed, please refer to the CHANGELOG '\
54
- 'on how to proceed with the new API https://github.com/dry-rb/dry-types/blob/master/CHANGELOG.md'
53
+ raise "Support for old hash schemas was removed, please refer to the CHANGELOG "\
54
+ "on how to proceed with the new API https://github.com/dry-rb/dry-types/blob/master/CHANGELOG.md"
55
55
  end
56
56
  alias_method :permissive, :weak
57
57
  alias_method :strict, :weak
@@ -69,7 +69,7 @@ module Dry
69
69
  def with_type_transform(proc = nil, &block)
70
70
  fn = proc || block
71
71
 
72
- raise ArgumentError, 'a block or callable argument is required' if fn.nil?
72
+ raise ArgumentError, "a block or callable argument is required" if fn.nil?
73
73
 
74
74
  handle = Dry::Types::FnContainer.register(fn)
75
75
  with(type_transform_fn: handle)
@@ -95,13 +95,7 @@ module Dry
95
95
  #
96
96
  # @api public
97
97
  def to_ast(meta: true)
98
- opts = if RUBY_VERSION >= '2.5'
99
- options.slice(:type_transform_fn)
100
- else
101
- options.select { |k, _| k == :type_transform_fn }
102
- end
103
-
104
- [:hash, [opts, meta ? self.meta : EMPTY_HASH]]
98
+ [:hash, [options.slice(:type_transform_fn), meta ? self.meta : EMPTY_HASH]]
105
99
  end
106
100
 
107
101
  private
@@ -129,7 +123,7 @@ module Dry
129
123
 
130
124
  # @api private
131
125
  def key_name(key)
132
- if key.to_s.end_with?('?')
126
+ if key.to_s.end_with?("?")
133
127
  [key.to_s.chop.to_sym, NOT_REQUIRED]
134
128
  else
135
129
  [key, EMPTY_HASH]
@@ -139,5 +133,5 @@ module Dry
139
133
  end
140
134
  end
141
135
 
142
- require 'dry/types/schema/key'
143
- require 'dry/types/schema'
136
+ require "dry/types/schema/key"
137
+ require "dry/types/schema"
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/types/constructor'
3
+ require "dry/types/constructor"
4
4
 
5
5
  module Dry
6
6
  module Types
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/inflector'
3
+ require "dry/inflector"
4
4
 
5
5
  module Dry
6
6
  module Types
@@ -1,35 +1,35 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/types/coercions/json'
3
+ require "dry/types/coercions/json"
4
4
 
5
5
  module Dry
6
6
  module Types
7
- register('json.nil') do
8
- self['nominal.nil'].constructor(Coercions::JSON.method(:to_nil))
7
+ register("json.nil") do
8
+ self["nominal.nil"].constructor(Coercions::JSON.method(:to_nil))
9
9
  end
10
10
 
11
- register('json.date') do
12
- self['nominal.date'].constructor(Coercions::JSON.method(:to_date))
11
+ register("json.date") do
12
+ self["nominal.date"].constructor(Coercions::JSON.method(:to_date))
13
13
  end
14
14
 
15
- register('json.date_time') do
16
- self['nominal.date_time'].constructor(Coercions::JSON.method(:to_date_time))
15
+ register("json.date_time") do
16
+ self["nominal.date_time"].constructor(Coercions::JSON.method(:to_date_time))
17
17
  end
18
18
 
19
- register('json.time') do
20
- self['nominal.time'].constructor(Coercions::JSON.method(:to_time))
19
+ register("json.time") do
20
+ self["nominal.time"].constructor(Coercions::JSON.method(:to_time))
21
21
  end
22
22
 
23
- register('json.decimal') do
24
- self['nominal.decimal'].constructor(Coercions::JSON.method(:to_decimal))
23
+ register("json.decimal") do
24
+ self["nominal.decimal"].constructor(Coercions::JSON.method(:to_decimal))
25
25
  end
26
26
 
27
- register('json.symbol') do
28
- self['nominal.symbol'].constructor(Coercions::JSON.method(:to_symbol))
27
+ register("json.symbol") do
28
+ self["nominal.symbol"].constructor(Coercions::JSON.method(:to_symbol))
29
29
  end
30
30
 
31
- register('json.array') { self['array'] }
31
+ register("json.array") { self["array"] }
32
32
 
33
- register('json.hash') { self['hash'] }
33
+ register("json.hash") { self["hash"] }
34
34
  end
35
35
  end
data/lib/dry/types/lax.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/core/deprecations'
4
- require 'dry/types/decorator'
3
+ require "dry/core/deprecations"
4
+ require "dry/types/decorator"
5
5
 
6
6
  module Dry
7
7
  module Types
data/lib/dry/types/map.rb CHANGED
@@ -21,7 +21,7 @@ module Dry
21
21
  #
22
22
  # @api public
23
23
  class Map < Nominal
24
- def initialize(_primitive, key_type: Types['any'], value_type: Types['any'], meta: EMPTY_HASH)
24
+ def initialize(_primitive, key_type: Types["any"], value_type: Types["any"], meta: EMPTY_HASH)
25
25
  super(_primitive, key_type: key_type, value_type: value_type, meta: meta)
26
26
  end
27
27
 
@@ -43,7 +43,7 @@ module Dry
43
43
  #
44
44
  # @api public
45
45
  def name
46
- 'Map'
46
+ "Map"
47
47
  end
48
48
 
49
49
  # @param [Hash] hash
@@ -11,7 +11,7 @@ module Dry
11
11
  @meta = meta.freeze
12
12
  end
13
13
 
14
- # @param [Hash] new_options
14
+ # @param options [Hash] new_options
15
15
  #
16
16
  # @return [Type]
17
17
  #
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/core/deprecations'
4
- require 'dry/types/builder_methods'
3
+ require "dry/core/deprecations"
4
+ require "dry/types/builder_methods"
5
5
 
6
6
  module Dry
7
7
  module Types
@@ -31,7 +31,7 @@ module Dry
31
31
  base.instance_exec(const_get(:Nominal, false)) do |nominal|
32
32
  extend Dry::Core::Deprecations[:'dry-types']
33
33
  const_set(:Definition, nominal)
34
- deprecate_constant(:Definition, message: 'Nominal')
34
+ deprecate_constant(:Definition, message: "Nominal")
35
35
  end
36
36
  end
37
37
  end
@@ -69,7 +69,7 @@ module Dry
69
69
  def registry_tree
70
70
  @registry_tree ||= @registry.keys.each_with_object({}) { |key, tree|
71
71
  type = @registry[key]
72
- *modules, const_name = key.split('.').map { |part|
72
+ *modules, const_name = key.split(".").map { |part|
73
73
  Inflector.camelize(part).to_sym
74
74
  }
75
75
  next if modules.empty?
@@ -87,14 +87,14 @@ module Dry
87
87
  referenced.concat(aliases.keys)
88
88
 
89
89
  known = @registry.keys.map { |k|
90
- ns, *path = k.split('.')
90
+ ns, *path = k.split(".")
91
91
  ns.to_sym unless path.empty?
92
92
  }.compact.uniq
93
93
 
94
94
  (referenced.uniq - known).each do |name|
95
95
  raise ArgumentError,
96
96
  "#{name.inspect} is not a known type namespace. "\
97
- "Supported options are #{known.map(&:inspect).join(', ')}"
97
+ "Supported options are #{known.map(&:inspect).join(", ")}"
98
98
  end
99
99
  end
100
100
 
@@ -1,10 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/core/deprecations'
4
- require 'dry/types/builder'
5
- require 'dry/types/result'
6
- require 'dry/types/options'
7
- require 'dry/types/meta'
3
+ require "dry/core/deprecations"
4
+ require "dry/core/equalizer"
5
+ require "dry/types/builder"
6
+ require "dry/types/result"
7
+ require "dry/types/options"
8
+ require "dry/types/meta"
8
9
 
9
10
  module Dry
10
11
  module Types
@@ -98,7 +99,6 @@ module Dry
98
99
  end
99
100
 
100
101
  # @param [Object] input
101
- # @param [#call,nil] block
102
102
  #
103
103
  # @yieldparam [Failure] failure
104
104
  # @yieldreturn [Result]
@@ -126,7 +126,7 @@ module Dry
126
126
  #
127
127
  # @api public
128
128
  def failure(input, error)
129
- raise ArgumentError, 'error must be a CoercionError' unless error.is_a?(CoercionError)
129
+ raise ArgumentError, "error must be a CoercionError" unless error.is_a?(CoercionError)
130
130
 
131
131
  Result::Failure.new(input, error)
132
132
  end
@@ -201,10 +201,10 @@ module Dry
201
201
 
202
202
  extend Dry::Core::Deprecations[:'dry-types']
203
203
  Definition = Nominal
204
- deprecate_constant(:Definition, message: 'Nominal')
204
+ deprecate_constant(:Definition, message: "Nominal")
205
205
  end
206
206
  end
207
207
 
208
- require 'dry/types/array'
209
- require 'dry/types/hash'
210
- require 'dry/types/map'
208
+ require "dry/types/array"
209
+ require "dry/types/hash"
210
+ require "dry/types/map"
@@ -1,65 +1,67 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/types/coercions/params'
3
+ require "dry/types/coercions/params"
4
4
 
5
5
  module Dry
6
6
  module Types
7
- register('params.nil') do
8
- self['nominal.nil'].constructor(Coercions::Params.method(:to_nil))
7
+ register("params.nil") do
8
+ self["nominal.nil"].constructor(Coercions::Params.method(:to_nil))
9
9
  end
10
10
 
11
- register('params.date') do
12
- self['nominal.date'].constructor(Coercions::Params.method(:to_date))
11
+ register("params.date") do
12
+ self["nominal.date"].constructor(Coercions::Params.method(:to_date))
13
13
  end
14
14
 
15
- register('params.date_time') do
16
- self['nominal.date_time'].constructor(Coercions::Params.method(:to_date_time))
15
+ register("params.date_time") do
16
+ self["nominal.date_time"].constructor(Coercions::Params.method(:to_date_time))
17
17
  end
18
18
 
19
- register('params.time') do
20
- self['nominal.time'].constructor(Coercions::Params.method(:to_time))
19
+ register("params.time") do
20
+ self["nominal.time"].constructor(Coercions::Params.method(:to_time))
21
21
  end
22
22
 
23
- register('params.true') do
24
- self['nominal.true'].constructor(Coercions::Params.method(:to_true))
23
+ register("params.true") do
24
+ self["nominal.true"].constructor(Coercions::Params.method(:to_true))
25
25
  end
26
26
 
27
- register('params.false') do
28
- self['nominal.false'].constructor(Coercions::Params.method(:to_false))
27
+ register("params.false") do
28
+ self["nominal.false"].constructor(Coercions::Params.method(:to_false))
29
29
  end
30
30
 
31
- register('params.bool') do
32
- self['params.true'] | self['params.false']
31
+ register("params.bool") do
32
+ self["params.true"] | self["params.false"]
33
33
  end
34
34
 
35
- register('params.integer') do
36
- self['nominal.integer'].constructor(Coercions::Params.method(:to_int))
35
+ register("params.integer") do
36
+ self["nominal.integer"].constructor(Coercions::Params.method(:to_int))
37
37
  end
38
38
 
39
- register('params.float') do
40
- self['nominal.float'].constructor(Coercions::Params.method(:to_float))
39
+ register("params.float") do
40
+ self["nominal.float"].constructor(Coercions::Params.method(:to_float))
41
41
  end
42
42
 
43
- register('params.decimal') do
44
- self['nominal.decimal'].constructor(Coercions::Params.method(:to_decimal))
43
+ register("params.decimal") do
44
+ self["nominal.decimal"].constructor(Coercions::Params.method(:to_decimal))
45
45
  end
46
46
 
47
- register('params.array') do
48
- self['nominal.array'].constructor(Coercions::Params.method(:to_ary))
47
+ register("params.array") do
48
+ self["nominal.array"].constructor(Coercions::Params.method(:to_ary))
49
49
  end
50
50
 
51
- register('params.hash') do
52
- self['nominal.hash'].constructor(Coercions::Params.method(:to_hash))
51
+ register("params.hash") do
52
+ self["nominal.hash"].constructor(Coercions::Params.method(:to_hash))
53
53
  end
54
54
 
55
- register('params.symbol') do
56
- self['nominal.symbol'].constructor(Coercions::Params.method(:to_symbol))
55
+ register("params.symbol") do
56
+ self["nominal.symbol"].constructor(Coercions::Params.method(:to_symbol))
57
57
  end
58
58
 
59
+ register("params.string", self["string"])
60
+
59
61
  COERCIBLE.each_key do |name|
60
62
  next if name.equal?(:string)
61
63
 
62
- register("optional.params.#{name}", self['params.nil'] | self["params.#{name}"])
64
+ register("optional.params.#{name}", self["params.nil"] | self["params.#{name}"])
63
65
  end
64
66
  end
65
67
  end