dry-types 0.13.2 → 1.5.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.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +763 -233
  3. data/LICENSE +17 -17
  4. data/README.md +15 -13
  5. data/dry-types.gemspec +28 -28
  6. data/lib/dry-types.rb +3 -1
  7. data/lib/dry/types.rb +156 -76
  8. data/lib/dry/types/any.rb +32 -12
  9. data/lib/dry/types/array.rb +19 -6
  10. data/lib/dry/types/array/constructor.rb +32 -0
  11. data/lib/dry/types/array/member.rb +75 -16
  12. data/lib/dry/types/builder.rb +131 -15
  13. data/lib/dry/types/builder_methods.rb +49 -20
  14. data/lib/dry/types/coercions.rb +76 -22
  15. data/lib/dry/types/coercions/json.rb +43 -7
  16. data/lib/dry/types/coercions/params.rb +118 -31
  17. data/lib/dry/types/compat.rb +0 -2
  18. data/lib/dry/types/compiler.rb +56 -41
  19. data/lib/dry/types/constrained.rb +81 -32
  20. data/lib/dry/types/constrained/coercible.rb +36 -6
  21. data/lib/dry/types/constraints.rb +18 -4
  22. data/lib/dry/types/constructor.rb +127 -54
  23. data/lib/dry/types/constructor/function.rb +216 -0
  24. data/lib/dry/types/constructor/wrapper.rb +94 -0
  25. data/lib/dry/types/container.rb +7 -0
  26. data/lib/dry/types/core.rb +54 -21
  27. data/lib/dry/types/decorator.rb +38 -17
  28. data/lib/dry/types/default.rb +61 -16
  29. data/lib/dry/types/enum.rb +43 -20
  30. data/lib/dry/types/errors.rb +75 -9
  31. data/lib/dry/types/extensions.rb +7 -1
  32. data/lib/dry/types/extensions/maybe.rb +74 -16
  33. data/lib/dry/types/extensions/monads.rb +29 -0
  34. data/lib/dry/types/fn_container.rb +6 -1
  35. data/lib/dry/types/hash.rb +86 -67
  36. data/lib/dry/types/hash/constructor.rb +33 -0
  37. data/lib/dry/types/inflector.rb +3 -1
  38. data/lib/dry/types/json.rb +18 -16
  39. data/lib/dry/types/lax.rb +75 -0
  40. data/lib/dry/types/map.rb +76 -33
  41. data/lib/dry/types/meta.rb +51 -0
  42. data/lib/dry/types/module.rb +120 -0
  43. data/lib/dry/types/nominal.rb +210 -0
  44. data/lib/dry/types/options.rb +13 -26
  45. data/lib/dry/types/params.rb +39 -25
  46. data/lib/dry/types/predicate_inferrer.rb +238 -0
  47. data/lib/dry/types/predicate_registry.rb +34 -0
  48. data/lib/dry/types/primitive_inferrer.rb +97 -0
  49. data/lib/dry/types/printable.rb +16 -0
  50. data/lib/dry/types/printer.rb +315 -0
  51. data/lib/dry/types/result.rb +29 -3
  52. data/lib/dry/types/schema.rb +408 -0
  53. data/lib/dry/types/schema/key.rb +156 -0
  54. data/lib/dry/types/spec/types.rb +103 -33
  55. data/lib/dry/types/sum.rb +84 -35
  56. data/lib/dry/types/type.rb +49 -0
  57. data/lib/dry/types/version.rb +3 -1
  58. metadata +68 -79
  59. data/.gitignore +0 -10
  60. data/.rspec +0 -2
  61. data/.travis.yml +0 -29
  62. data/.yardopts +0 -5
  63. data/CONTRIBUTING.md +0 -29
  64. data/Gemfile +0 -24
  65. data/Rakefile +0 -20
  66. data/benchmarks/hash_schemas.rb +0 -51
  67. data/lib/dry/types/compat/form_types.rb +0 -27
  68. data/lib/dry/types/compat/int.rb +0 -14
  69. data/lib/dry/types/definition.rb +0 -113
  70. data/lib/dry/types/hash/schema.rb +0 -199
  71. data/lib/dry/types/hash/schema_builder.rb +0 -75
  72. data/lib/dry/types/safe.rb +0 -59
@@ -1,75 +0,0 @@
1
- require 'dry/types/hash/schema'
2
- require 'dry/types/fn_container'
3
-
4
- module Dry
5
- module Types
6
- class Hash < Definition
7
- # A bulder for legacy schemas
8
- # @api private
9
- class SchemaBuilder
10
- NIL_TO_UNDEFINED = -> v { v.nil? ? Undefined : v }
11
- OMITTABLE_KEYS = %i(schema weak symbolized).freeze
12
- STRICT = %i(strict strict_with_defaults).freeze
13
-
14
- # @param primitive [Type]
15
- # @option options [Hash{Symbol => Definition}] :member_types
16
- # @option options [Symbol] :hash_type
17
- def call(primitive, options)
18
- hash_type = options.fetch(:hash_type)
19
- member_types = {}
20
-
21
- options.fetch(:member_types).each do |k, t|
22
- member_types[k] = build_type(hash_type, t)
23
- end
24
-
25
- instantiate(primitive, **options, member_types: member_types)
26
- end
27
-
28
- def instantiate(primitive, hash_type: :base, meta: EMPTY_HASH, **options)
29
- meta = meta.dup
30
-
31
- meta[:strict] = true if strict?(hash_type)
32
- meta[:key_transform_fn] = Schema::SYMBOLIZE_KEY if hash_type == :symbolized
33
-
34
- Schema.new(primitive, **options, meta: meta)
35
- end
36
-
37
- private
38
-
39
- def omittable?(constructor)
40
- OMITTABLE_KEYS.include?(constructor)
41
- end
42
-
43
- def strict?(constructor)
44
- STRICT.include?(constructor)
45
- end
46
-
47
- def build_type(constructor, type)
48
- type = safe(constructor, type)
49
- type = default(constructor, type) if type.default?
50
- type = type.meta(omittable: true) if omittable?(constructor)
51
- type
52
- end
53
-
54
- def safe(constructor, type)
55
- if constructor == :weak || constructor == :symbolized
56
- type.safe
57
- else
58
- type
59
- end
60
- end
61
-
62
- def default(constructor, type)
63
- case constructor
64
- when :strict_with_defaults
65
- type
66
- when :strict
67
- type.type
68
- else
69
- type.constructor(NIL_TO_UNDEFINED)
70
- end
71
- end
72
- end
73
- end
74
- end
75
- end
@@ -1,59 +0,0 @@
1
- require 'dry/types/decorator'
2
-
3
- module Dry
4
- module Types
5
- class Safe
6
- include Type
7
- include Dry::Equalizer(:type)
8
- include Decorator
9
- include Builder
10
- private :options, :meta
11
-
12
- # @param [Object] input
13
- # @return [Object]
14
- def call(input)
15
- result = try(input)
16
-
17
- if result.respond_to?(:input)
18
- result.input
19
- else
20
- input
21
- end
22
- end
23
- alias_method :[], :call
24
-
25
- # @param [Object] input
26
- # @param [#call,nil] block
27
- # @yieldparam [Failure] failure
28
- # @yieldreturn [Result]
29
- # @return [Result,Logic::Result]
30
- def try(input, &block)
31
- type.try(input, &block)
32
- rescue TypeError, ArgumentError => e
33
- result = failure(input, e.message)
34
- block ? yield(result) : result
35
- end
36
-
37
- # @api public
38
- #
39
- # @see Definition#to_ast
40
- def to_ast(meta: true)
41
- [:safe, [type.to_ast(meta: meta), EMPTY_HASH]]
42
- end
43
-
44
- # @api public
45
- # @return [Safe]
46
- def safe
47
- self
48
- end
49
-
50
- private
51
-
52
- # @param [Object, Dry::Types::Constructor] response
53
- # @return [Boolean]
54
- def decorate?(response)
55
- super || response.kind_of?(Constructor)
56
- end
57
- end
58
- end
59
- end