dry-types 0.11.1 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6f665eeb69ceba9c2fd336bc2a489c1b4004c40c
4
- data.tar.gz: 155884224930392bcc2db1640b88ae2cdaa10fe3
3
+ metadata.gz: 336c6e4da78c6b66a32a43671d7961b5a9e26e78
4
+ data.tar.gz: 282770ba63247f594e89071ad36677689fe7ddc7
5
5
  SHA512:
6
- metadata.gz: '08136001e7fe28fd14260861a37b91ee9ca276e480990b6ac741f39a3ee9e4603b26d20077c9984a50bd21536c4247acbffc9cba6050587b3c23c6e1be61846f'
7
- data.tar.gz: 3fb2af20a9f42b4b025ff3ad1f460d4fbde466a3c459d1f894d09cc4266c62e7003058b6a931c8614e94c6173ecf5ae9a6a56c0ca7f82fcc2479366d63bfb7b4
6
+ metadata.gz: af9f5770f1ae7e750589b016bba706eb874cacb8d282b013493ec81c2344af35abde95eb3609c6f80fb907ca17abc7c964f25a9c290ee0fe9992f3cd131885e6
7
+ data.tar.gz: 9a7711e6c22eb5889448ce10adbcbb48cb02e1d6a9ee91c43707556e68b6e57e048507382a13d28a00940befca06d88329ec99ce7132dcefd4857a3435b92b8c
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ log/
@@ -1,3 +1,15 @@
1
+ # v0.12.0 2017-09-15
2
+
3
+ ## Added
4
+
5
+ * A bunch of shortcut methods for constructing types to the autogenerated module, e.g. `Types.Constructor(String, &:to_s)` (flash-gordon)
6
+
7
+ ## Deprecated
8
+
9
+ * `Types::Array#member` was deprecated in favor of `Types::Array#of` (flash-gordon)
10
+
11
+ [Compare v0.11.1...v0.12.0](https://github.com/dry-rb/dry-types/compare/v0.11.1...v0.12.0)
12
+
1
13
  # v0.11.1 2017-08-14
2
14
 
3
15
  ## Changed
data/Gemfile CHANGED
@@ -21,3 +21,5 @@ group :benchmarks do
21
21
  gem 'fast_attributes'
22
22
  gem 'attrio'
23
23
  end
24
+
25
+ gem 'dry-logic', git: 'https://github.com/dry-rb/dry-logic'
@@ -1,4 +1,3 @@
1
- # coding: utf-8
2
1
  lib = File.expand_path('../lib', __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'dry/types/version'
@@ -12,7 +11,7 @@ Gem::Specification.new do |spec|
12
11
 
13
12
  spec.summary = 'Type system for Ruby supporting coercions, constraints and complex types like structs, value objects, enums etc.'
14
13
  spec.description = spec.summary
15
- spec.homepage = "https://github.com/dryrb/dry-types"
14
+ spec.homepage = "https://github.com/dry-rb/dry-types"
16
15
 
17
16
  # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
17
  # delete this section to allow pushing this gem to any host.
@@ -33,7 +32,7 @@ Gem::Specification.new do |spec|
33
32
  spec.add_runtime_dependency 'dry-container', '~> 0.3'
34
33
  spec.add_runtime_dependency 'dry-equalizer', '~> 0.2'
35
34
  spec.add_runtime_dependency 'dry-configurable', '~> 0.1'
36
- spec.add_runtime_dependency 'dry-logic', '~> 0.4', '>= 0.4.0'
35
+ spec.add_runtime_dependency 'dry-logic', '~> 0.4', '>= 0.4.2'
37
36
  spec.add_runtime_dependency 'inflecto', '~> 0.0.0', '>= 0.0.2'
38
37
 
39
38
  spec.add_development_dependency "bundler", "~> 1.6"
@@ -16,6 +16,7 @@ require 'dry/types/type'
16
16
  require 'dry/types/definition'
17
17
  require 'dry/types/constructor'
18
18
  require 'dry/types/fn_container'
19
+ require 'dry/types/builder_methods'
19
20
 
20
21
  require 'dry/types/errors'
21
22
 
@@ -35,6 +36,7 @@ module Dry
35
36
  def self.module
36
37
  namespace = Module.new
37
38
  define_constants(namespace, type_keys)
39
+ namespace.extend(BuilderMethods)
38
40
  namespace
39
41
  end
40
42
 
@@ -84,7 +86,7 @@ module Dry
84
86
 
85
87
  if result
86
88
  type_id, member_id = result[1..2]
87
- container[type_id].member(self[member_id])
89
+ container[type_id].of(self[member_id])
88
90
  else
89
91
  container[name]
90
92
  end
@@ -132,7 +134,23 @@ module Dry
132
134
  # List of type keys defined in {Dry::Types.container}
133
135
  # @return [<String>]
134
136
  def self.type_keys
135
- container._container.keys
137
+ container.keys
138
+ end
139
+
140
+ private
141
+
142
+ # @api private
143
+ def self.const_missing(const)
144
+ underscored = Inflecto.underscore(const)
145
+
146
+ if type_keys.any? { |key| key.split('.')[0] == underscored }
147
+ raise NameError,
148
+ 'dry-types does not define constants for default types. '\
149
+ 'You can access the predefined types with [], e.g. Dry::Types["strict.int"] '\
150
+ 'or generate a module with types using Dry::Types.module'
151
+ else
152
+ super
153
+ end
136
154
  end
137
155
  end
138
156
  end
@@ -1,21 +1,24 @@
1
1
  require 'dry/types/array/member'
2
+ require 'dry/core/deprecations'
2
3
 
3
4
  module Dry
4
5
  module Types
5
6
  class Array < Definition
7
+ extend Dry::Core::Deprecations[:'dry-types']
8
+
6
9
  # @param [Type] type
7
10
  # @return [Array::Member]
8
- def member(type)
11
+ def of(type)
9
12
  member =
10
13
  case type
11
14
  when String, Class then Types[type]
12
15
  else type
13
16
  end
14
17
 
15
- Array::Member.new(primitive, options.merge(member: member))
18
+ Array::Member.new(primitive, **options, member: member)
16
19
  end
17
20
 
18
- alias_method :of, :member
21
+ deprecate :member, :of
19
22
  end
20
23
  end
21
24
  end
@@ -0,0 +1,91 @@
1
+ module Dry
2
+ module Types
3
+ module BuilderMethods
4
+ # @api private
5
+ def included(base)
6
+ super
7
+ base.extend(BuilderMethods)
8
+ end
9
+
10
+ # Build an array type.
11
+ # It is a shortcut for Array.of
12
+ #
13
+ # @example
14
+ # Types::Strings = Types.Array(Types::String)
15
+ #
16
+ # @param [Dry::Types::Type] type
17
+ #
18
+ # @return [Dry::Types::Array]
19
+ def Array(type)
20
+ self::Array.of(type)
21
+ end
22
+
23
+ # Build a hash schema
24
+ #
25
+ # @param [Symbol] schema Schema type
26
+ # @param [Hash{Symbol => Dry::Types::Type}] type_map
27
+ #
28
+ # @return [Dry::Types::Array]
29
+ # @api public
30
+ def Hash(schema, type_map)
31
+ self::Hash.public_send(schema, type_map)
32
+ end
33
+
34
+ # Build a type which values are instances of a given class
35
+ # Values are checked using `is_a?` call
36
+ #
37
+ # @param [Class,Module] klass Class or module
38
+ #
39
+ # @return [Dry::Types::Type]
40
+ # @api public
41
+ def Instance(klass)
42
+ Definition.new(klass).constrained(type: klass)
43
+ end
44
+
45
+ # Build a type with a single value
46
+ # The equality check done with `eql?`
47
+ #
48
+ # @param [Object] value
49
+ #
50
+ # @return [Dry::Types::Type]
51
+ # @api public
52
+ def Value(value)
53
+ Definition.new(value.class).constrained(eql: value)
54
+ end
55
+
56
+ # Build a type with a single value
57
+ # The equality check done with `equal?`
58
+ #
59
+ # @param [Object] object
60
+ #
61
+ # @return [Dry::Types::Type]
62
+ # @api public
63
+ def Constant(object)
64
+ Definition.new(object.class).constrained(is: object)
65
+ end
66
+
67
+ # Build a constructor type
68
+ # If no constructor block given it uses .new method
69
+ #
70
+ # @param [Class] klass
71
+ # @param [#call,nil] cons Value constructor
72
+ # @param [#call,nil] block Value constructor
73
+ #
74
+ # @return [Dry::Types::Type]
75
+ # @api public
76
+ def Constructor(klass, cons = nil, &block)
77
+ Definition.new(klass).constructor(cons || block || klass.method(:new))
78
+ end
79
+
80
+ # Build a definiton type
81
+ #
82
+ # @param [Class] klass
83
+ #
84
+ # @return [Dry::Types::Type]
85
+ # @api public
86
+ def Definition(klass)
87
+ Definition.new(klass)
88
+ end
89
+ end
90
+ end
91
+ end
@@ -45,7 +45,7 @@ module Dry
45
45
 
46
46
  def visit_array(node)
47
47
  member, meta = node
48
- registry['array'].member(visit(member)).meta(meta)
48
+ registry['array'].of(visit(member)).meta(meta)
49
49
  end
50
50
 
51
51
  def visit_hash(node)
@@ -60,7 +60,7 @@ module Dry
60
60
 
61
61
  def visit_json_array(node)
62
62
  member, meta = node
63
- registry['json.array'].member(visit(member)).meta(meta)
63
+ registry['json.array'].of(visit(member)).meta(meta)
64
64
  end
65
65
 
66
66
  def visit_form_hash(node)
@@ -70,7 +70,7 @@ module Dry
70
70
 
71
71
  def visit_form_array(node)
72
72
  member, meta = node
73
- registry['form.array'].member(visit(member)).meta(meta)
73
+ registry['form.array'].of(visit(member)).meta(meta)
74
74
  end
75
75
 
76
76
  def visit_member(node)
@@ -1,5 +1,5 @@
1
1
  module Dry
2
2
  module Types
3
- VERSION = '0.11.1'.freeze
3
+ VERSION = '0.12.0'.freeze
4
4
  end
5
5
  end
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.11.1
4
+ version: 0.12.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-08-14 00:00:00.000000000 Z
11
+ date: 2017-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -95,7 +95,7 @@ dependencies:
95
95
  version: '0.4'
96
96
  - - ">="
97
97
  - !ruby/object:Gem::Version
98
- version: 0.4.0
98
+ version: 0.4.2
99
99
  type: :runtime
100
100
  prerelease: false
101
101
  version_requirements: !ruby/object:Gem::Requirement
@@ -105,7 +105,7 @@ dependencies:
105
105
  version: '0.4'
106
106
  - - ">="
107
107
  - !ruby/object:Gem::Version
108
- version: 0.4.0
108
+ version: 0.4.2
109
109
  - !ruby/object:Gem::Dependency
110
110
  name: inflecto
111
111
  requirement: !ruby/object:Gem::Requirement
@@ -222,6 +222,7 @@ files:
222
222
  - lib/dry/types/array.rb
223
223
  - lib/dry/types/array/member.rb
224
224
  - lib/dry/types/builder.rb
225
+ - lib/dry/types/builder_methods.rb
225
226
  - lib/dry/types/coercions.rb
226
227
  - lib/dry/types/coercions/form.rb
227
228
  - lib/dry/types/coercions/json.rb
@@ -251,7 +252,8 @@ files:
251
252
  - lib/dry/types/type.rb
252
253
  - lib/dry/types/version.rb
253
254
  - lib/spec/dry/types.rb
254
- homepage: https://github.com/dryrb/dry-types
255
+ - log/.gitkeep
256
+ homepage: https://github.com/dry-rb/dry-types
255
257
  licenses:
256
258
  - MIT
257
259
  metadata: