bm-typed 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/typed.rb +3 -3
- data/lib/typed/builder.rb +21 -19
- data/lib/typed/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f429141987a2845ff72402f8fb9b49e052f92da390b4c7922a35c14f936cbfc
|
4
|
+
data.tar.gz: bd6e44b3def8e439828aeedad4b64cc4397d3bfdd1b90fe54ca0b3b733fa4de3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7eea4da91207c22cda05f28bc37fd10990e0a582b1fae2acb238edbb5861dcc32a07dc94da543efa779b2309ffeaa0566f5aa088b00ebd811359cd9934a88660
|
7
|
+
data.tar.gz: a4f708f98dfcbb1e8cbb03188b6d6aa49969e8e22d6ea31a22d0cc95d1c7d4b7b32f030851a5e92f1988826a2d9150f58cb1c044becb879706202a4ed208bad0
|
data/lib/typed.rb
CHANGED
@@ -28,7 +28,7 @@ module Typed
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def value(expected_value)
|
31
|
-
|
31
|
+
constrained(eql: call(expected_value))
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
@@ -99,8 +99,8 @@ module Typed
|
|
99
99
|
}
|
100
100
|
.constructor(input: Int | Float, swallow: [TypeError, ArgumentError]) { |value| ::Time.at(value) }
|
101
101
|
|
102
|
-
UUID = String.
|
102
|
+
UUID = String.constrained(format: /\A[a-f\d]{8}(-[a-f\d]{4}){3}-[a-f\d]{12}\z/)
|
103
103
|
.constructor(input: String, &:downcase)
|
104
104
|
|
105
|
-
URL = String.
|
105
|
+
URL = String.constrained(format: URI::DEFAULT_PARSER.make_regexp(%w[http https]))
|
106
106
|
end
|
data/lib/typed/builder.rb
CHANGED
@@ -52,11 +52,11 @@ module Typed
|
|
52
52
|
end
|
53
53
|
|
54
54
|
def instance(expected_class)
|
55
|
-
|
55
|
+
constrained(type: expected_class)
|
56
56
|
end
|
57
57
|
|
58
58
|
def enum(*values)
|
59
|
-
|
59
|
+
constrained(included_in: values.map { |value| call(value) })
|
60
60
|
end
|
61
61
|
|
62
62
|
def |(other)
|
@@ -72,6 +72,25 @@ module Typed
|
|
72
72
|
CoerceType.new(input, self, swallow, &block)
|
73
73
|
end
|
74
74
|
|
75
|
+
def constrained(**dry_options, &constraint)
|
76
|
+
base = constraint ? ConstrainedType.new(self, &constraint) : self
|
77
|
+
base = base.dry_constrained(**dry_options) unless dry_options.empty?
|
78
|
+
base
|
79
|
+
end
|
80
|
+
|
81
|
+
def call(*args)
|
82
|
+
result = process((args + [Typed::Undefined]).first)
|
83
|
+
return result.value if result.ok
|
84
|
+
|
85
|
+
raise InvalidValue, result.message
|
86
|
+
end
|
87
|
+
|
88
|
+
def process(value)
|
89
|
+
Typed::Builder::Result.success(value)
|
90
|
+
end
|
91
|
+
|
92
|
+
protected
|
93
|
+
|
75
94
|
def dry_constrained(**options)
|
76
95
|
predicate = ::Dry::Logic::RuleCompiler.new(::Dry::Logic::Predicates).call(
|
77
96
|
options.map { |key, val|
|
@@ -86,23 +105,6 @@ module Typed
|
|
86
105
|
end
|
87
106
|
end
|
88
107
|
|
89
|
-
def constrained(&constraint)
|
90
|
-
return self unless constraint
|
91
|
-
|
92
|
-
ConstrainedType.new(self, &constraint)
|
93
|
-
end
|
94
|
-
|
95
|
-
def call(*args)
|
96
|
-
result = process((args + [Typed::Undefined]).first)
|
97
|
-
return result.value if result.ok
|
98
|
-
|
99
|
-
raise InvalidValue, result.message
|
100
|
-
end
|
101
|
-
|
102
|
-
def process(value)
|
103
|
-
Typed::Builder::Result.success(value)
|
104
|
-
end
|
105
|
-
|
106
108
|
private
|
107
109
|
|
108
110
|
def expected_type(type)
|
data/lib/typed/version.rb
CHANGED