dry-types 0.14.0 → 0.15.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.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +15 -0
  3. data/.rubocop.yml +43 -0
  4. data/.travis.yml +2 -3
  5. data/CHANGELOG.md +119 -1
  6. data/Gemfile +2 -2
  7. data/benchmarks/hash_schemas.rb +5 -5
  8. data/dry-types.gemspec +2 -2
  9. data/lib/dry/types.rb +67 -45
  10. data/lib/dry/types/any.rb +11 -2
  11. data/lib/dry/types/array.rb +1 -4
  12. data/lib/dry/types/array/member.rb +2 -2
  13. data/lib/dry/types/builder.rb +23 -3
  14. data/lib/dry/types/builder_methods.rb +10 -11
  15. data/lib/dry/types/coercions/params.rb +2 -0
  16. data/lib/dry/types/compat.rb +0 -2
  17. data/lib/dry/types/compiler.rb +25 -33
  18. data/lib/dry/types/constrained.rb +5 -4
  19. data/lib/dry/types/constructor.rb +33 -12
  20. data/lib/dry/types/container.rb +2 -0
  21. data/lib/dry/types/core.rb +22 -12
  22. data/lib/dry/types/default.rb +4 -4
  23. data/lib/dry/types/enum.rb +10 -3
  24. data/lib/dry/types/errors.rb +1 -1
  25. data/lib/dry/types/extensions/maybe.rb +11 -1
  26. data/lib/dry/types/hash.rb +70 -63
  27. data/lib/dry/types/hash/constructor.rb +20 -0
  28. data/lib/dry/types/json.rb +7 -7
  29. data/lib/dry/types/map.rb +6 -1
  30. data/lib/dry/types/module.rb +115 -0
  31. data/lib/dry/types/{definition.rb → nominal.rb} +10 -4
  32. data/lib/dry/types/options.rb +2 -2
  33. data/lib/dry/types/params.rb +11 -11
  34. data/lib/dry/types/printable.rb +12 -0
  35. data/lib/dry/types/printer.rb +309 -0
  36. data/lib/dry/types/result.rb +2 -2
  37. data/lib/dry/types/safe.rb +4 -2
  38. data/lib/dry/types/schema.rb +298 -0
  39. data/lib/dry/types/schema/key.rb +130 -0
  40. data/lib/dry/types/spec/types.rb +12 -4
  41. data/lib/dry/types/sum.rb +14 -15
  42. data/lib/dry/types/version.rb +1 -1
  43. metadata +22 -12
  44. data/lib/dry/types/compat/form_types.rb +0 -27
  45. data/lib/dry/types/compat/int.rb +0 -14
  46. data/lib/dry/types/hash/schema.rb +0 -199
  47. data/lib/dry/types/hash/schema_builder.rb +0 -75
@@ -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