Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 99794d9

Browse files
committedMay 28, 2016
Drop support for non-dry-types
1 parent ac066aa commit 99794d9

File tree

8 files changed

+47
-145
lines changed

8 files changed

+47
-145
lines changed
 

‎lib/dry/initializer/errors.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ module Errors
55
require_relative "errors/order_error"
66
require_relative "errors/plugin_error"
77
require_relative "errors/redefinition_error"
8-
require_relative "errors/type_error"
98
require_relative "errors/type_constraint_error"
109
end
1110
end
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
class Dry::Initializer::Errors::TypeConstraintError < TypeError
22
def initialize(name, type)
3-
super "#{type} is inacceptable constraint for the argument '#{name}'." \
4-
" Use either plain Ruby module/class, or dry-type."
3+
super "#{type} used as constraint for argument '#{name}' is not a dry-type."
54
end
65
end

‎lib/dry/initializer/errors/type_error.rb

Lines changed: 0 additions & 6 deletions
This file was deleted.

‎lib/dry/initializer/plugins/type_constraint.rb

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,15 @@ module Dry::Initializer::Plugins
44
class TypeConstraint < Base
55
def call
66
return unless settings.key? :type
7-
dry_type_constraint || module_type_constraint || object_type_constraint
8-
end
9-
10-
private
11-
12-
def type
13-
@type ||= settings[:type]
14-
end
15-
16-
def dry_type?
17-
type.respond_to? :call
18-
end
19-
20-
def plain_type?
21-
Module === type
22-
end
23-
24-
def module_type_constraint
25-
return unless plain_type?
26-
27-
warn "[DEPRECATION] support for ruby modules as type constraint" \
28-
" is deprecated. Use dry-types instead."
29-
30-
"fail #{TypeError}.new(:#{name}, #{type}, @#{name})" \
31-
" unless @#{name} == Dry::Initializer::UNDEFINED ||" \
32-
" #{type} === @#{name}"
33-
end
347

35-
def dry_type_constraint
36-
return unless dry_type?
8+
type = settings[:type]
9+
fail TypeConstraintError.new(name, type) unless type.respond_to? :call
3710

3811
ivar = :"@#{name}"
39-
constraint = type
40-
41-
lambda do |*|
42-
value = instance_variable_get(ivar)
43-
return if value == Dry::Initializer::UNDEFINED
44-
45-
instance_variable_set ivar, constraint[value]
46-
end
47-
end
48-
49-
def object_type_constraint
50-
warn "[DEPRECATION] support for case equality (===) as type constraint" \
51-
" is deprecated. Use dry-types instead."
52-
53-
ivar = :"@#{name}"
54-
constraint = type
55-
5612
lambda do |*|
5713
value = instance_variable_get(ivar)
5814
return if value == Dry::Initializer::UNDEFINED
59-
60-
fail TypeError.new(ivar, constraint, value) unless constraint === value
15+
instance_variable_set ivar, type.call(value)
6116
end
6217
end
6318
end

‎spec/dry/dry_type_constraint_spec.rb

Lines changed: 0 additions & 30 deletions
This file was deleted.

‎spec/dry/object_type_constraint_spec.rb

Lines changed: 0 additions & 25 deletions
This file was deleted.

‎spec/dry/plain_type_constraint_spec.rb

Lines changed: 0 additions & 33 deletions
This file was deleted.

‎spec/dry/type_constraint_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require "dry-types"
2+
3+
describe "type constraint" do
4+
context "by dry-type" do
5+
before do
6+
module Test::Types
7+
include Dry::Types.module
8+
end
9+
10+
class Test::Foo
11+
extend Dry::Initializer::Mixin
12+
param :foo, type: Test::Types::Strict::String
13+
end
14+
end
15+
16+
context "in case of mismatch" do
17+
subject { Test::Foo.new 1 }
18+
19+
it "raises TypeError" do
20+
expect { subject }.to raise_error TypeError, /1/
21+
end
22+
end
23+
24+
context "in case of match" do
25+
subject { Test::Foo.new "foo" }
26+
27+
it "completes the initialization" do
28+
expect { subject }.not_to raise_error
29+
end
30+
end
31+
end
32+
33+
context "by invalid constraint" do
34+
it "raises TypeError" do
35+
expect {
36+
class Test::Foo
37+
extend Dry::Initializer::Mixin
38+
param :foo, type: String
39+
end
40+
}.to raise_error(TypeError)
41+
end
42+
end
43+
end

0 commit comments

Comments
 (0)
Please sign in to comment.