plumb 0.4.1 → 0.4.2
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.
- checksums.yaml +4 -4
- data/lib/plumb/composable.rb +4 -2
- data/lib/plumb/constraint.rb +11 -3
- data/lib/plumb/json_schema_visitor.rb +6 -2
- data/lib/plumb/types.rb +6 -6
- data/lib/plumb/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: 4c4884e54e901a4aba218432c58deaf9f8f8afbef8b5efe4e4109b847cd9d8c4
|
|
4
|
+
data.tar.gz: 5d1757c74adcb2146a588d4754966c3eb2576269d46574ae8b73656d224802d3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c3049a98a899acd4f49bcd4b21563480acededfef0f61153e4b7a548460df5d40ebbc2ae9d2b5acf316763e1405c049da03e5fde05f7ad68d3b37fc4d8b6a427
|
|
7
|
+
data.tar.gz: 98be5ff267faba06a8defbc554451a247346a3cdd8d24d728fc020af4d142a186bf38568f5f5053964ab2b628641a7ea9c0fb1f4ec6e3aeb4b9080260f8de850
|
data/lib/plumb/composable.rb
CHANGED
|
@@ -516,12 +516,14 @@ module Plumb
|
|
|
516
516
|
# type = Types::String.check('must start with "Role:"') { |value| value.start_with?('Role:') }
|
|
517
517
|
#
|
|
518
518
|
# @param errors [String] error message to use when validation fails
|
|
519
|
+
# @param inspect [String, nil] how the check renders in #inspect, after the base type
|
|
520
|
+
# (eg. `'options(["a"])'` => `Types::String.options(["a"])`). Policies set this.
|
|
519
521
|
# @param block [Proc] a block that will be applied to the value
|
|
520
522
|
# @return [Constraint]
|
|
521
|
-
def check(errors = 'did not pass the check', &block)
|
|
523
|
+
def check(errors = 'did not pass the check', inspect: nil, &block)
|
|
522
524
|
# A refinement, not a sequence: build the matcher over `self` as its base so
|
|
523
525
|
# the checked value keeps `self`'s type (`User.check { … }` is still a User).
|
|
524
|
-
Constraint.new(block, base: self, error: errors, label: errors)
|
|
526
|
+
Constraint.new(block, base: self, error: errors, label: inspect || "check(#{errors.inspect})")
|
|
525
527
|
end
|
|
526
528
|
|
|
527
529
|
# Return a new Step with added metadata, or build step metadata if no argument is provided.
|
data/lib/plumb/constraint.rb
CHANGED
|
@@ -48,7 +48,13 @@ module Plumb
|
|
|
48
48
|
@matcher_node = SemanticMatcher.wrap(matcher)
|
|
49
49
|
@base = base
|
|
50
50
|
@error = error.nil? ? build_error(matcher) : (error % matcher)
|
|
51
|
-
@label = matcher.is_a?(Class)
|
|
51
|
+
@label = if matcher.is_a?(Class)
|
|
52
|
+
matcher.inspect
|
|
53
|
+
elsif base && matcher.is_a?(::Proc)
|
|
54
|
+
label || matcher.inspect # rendered as `base.label`, see #_inspect
|
|
55
|
+
else
|
|
56
|
+
"Constraint(#{label || @matcher.inspect})"
|
|
57
|
+
end
|
|
52
58
|
@children = [matcher].freeze
|
|
53
59
|
freeze
|
|
54
60
|
end
|
|
@@ -167,16 +173,17 @@ module Plumb
|
|
|
167
173
|
|
|
168
174
|
# Identity label for matchers without structural equality.
|
|
169
175
|
protected def label = @label
|
|
176
|
+
protected def error = @error
|
|
170
177
|
|
|
171
178
|
# Compares matchers and bases. Fresh Procs lack structural equality, so Proc
|
|
172
|
-
# matchers use their caller-supplied label as stable identity.
|
|
179
|
+
# matchers use their caller-supplied label and error as stable identity.
|
|
173
180
|
# @param other [Object]
|
|
174
181
|
# @return [Boolean]
|
|
175
182
|
def ==(other)
|
|
176
183
|
return false unless other.instance_of?(self.class) && other.base == base
|
|
177
184
|
|
|
178
185
|
if matcher.is_a?(::Proc)
|
|
179
|
-
other.matcher.is_a?(::Proc) && other.label == label
|
|
186
|
+
other.matcher.is_a?(::Proc) && other.label == label && other.error == error
|
|
180
187
|
else
|
|
181
188
|
other.matcher == matcher
|
|
182
189
|
end
|
|
@@ -223,6 +230,7 @@ module Plumb
|
|
|
223
230
|
|
|
224
231
|
def _inspect
|
|
225
232
|
return @label unless base
|
|
233
|
+
return "#{base.inspect}.#{@label}" if @matcher.is_a?(::Proc)
|
|
226
234
|
|
|
227
235
|
"#{base.inspect}[#{@matcher.inspect}]"
|
|
228
236
|
end
|
|
@@ -500,8 +500,12 @@ module Plumb
|
|
|
500
500
|
end
|
|
501
501
|
|
|
502
502
|
on(:metadata) do |node, props|
|
|
503
|
-
#
|
|
504
|
-
|
|
503
|
+
# Metadata#metadata includes the wrapped type's own (eg. `options` from a
|
|
504
|
+
# policy), which the wrapped type renders itself (`options` -> `enum`).
|
|
505
|
+
# Emit only what this node added or overrode.
|
|
506
|
+
inner = node.type.metadata
|
|
507
|
+
own = node.metadata.reject { |k, v| inner.key?(k) && inner[k] == v }
|
|
508
|
+
props = props.merge(stringify_keys(own))
|
|
505
509
|
props.merge(visit(node.type, props))
|
|
506
510
|
end
|
|
507
511
|
|
data/lib/plumb/types.rb
CHANGED
|
@@ -13,7 +13,7 @@ module Plumb
|
|
|
13
13
|
# Usage:
|
|
14
14
|
# type = Types::Array.options(['a', 'b'])
|
|
15
15
|
policy :options, helper: true, for_type: ::Array do |type, opts|
|
|
16
|
-
type.check("must be included in #{opts.inspect}") do |v|
|
|
16
|
+
type.check("must be included in #{opts.inspect}", inspect: "options(#{opts.inspect})") do |v|
|
|
17
17
|
v.all? { |val| opts.include?(val) }
|
|
18
18
|
end
|
|
19
19
|
end
|
|
@@ -28,7 +28,7 @@ module Plumb
|
|
|
28
28
|
# type = Types::Integer.options(10..20)
|
|
29
29
|
policy :options do |type, opts|
|
|
30
30
|
if opts.is_a?(::Array)
|
|
31
|
-
type.check("must be included in #{opts.inspect}") do |v|
|
|
31
|
+
type.check("must be included in #{opts.inspect}", inspect: "options(#{opts.inspect})") do |v|
|
|
32
32
|
opts.include?(v)
|
|
33
33
|
end
|
|
34
34
|
else
|
|
@@ -40,7 +40,7 @@ module Plumb
|
|
|
40
40
|
# Usage:
|
|
41
41
|
# type = Types::Array.policy(excluded_from: ['a', 'b'])
|
|
42
42
|
policy :excluded_from, for_type: ::Array do |type, opts|
|
|
43
|
-
type.check("must not be included in #{opts.inspect}") do |v|
|
|
43
|
+
type.check("must not be included in #{opts.inspect}", inspect: "excluded_from(#{opts.inspect})") do |v|
|
|
44
44
|
v.none? { |val| opts.include?(val) }
|
|
45
45
|
end
|
|
46
46
|
end
|
|
@@ -48,7 +48,7 @@ module Plumb
|
|
|
48
48
|
# Usage:
|
|
49
49
|
# type = Types::String.policy(excluded_from: ['a', 'b'])
|
|
50
50
|
policy :excluded_from do |type, opts|
|
|
51
|
-
type.check("must not be included in #{opts.inspect}") do |v|
|
|
51
|
+
type.check("must not be included in #{opts.inspect}", inspect: "excluded_from(#{opts.inspect})") do |v|
|
|
52
52
|
!opts.include?(v)
|
|
53
53
|
end
|
|
54
54
|
end
|
|
@@ -58,7 +58,7 @@ module Plumb
|
|
|
58
58
|
# Types::String.present
|
|
59
59
|
# Types::Array.present
|
|
60
60
|
policy :present, helper: true do |type, *_args|
|
|
61
|
-
type.check('must be present') do |v|
|
|
61
|
+
type.check('must be present', inspect: 'present') do |v|
|
|
62
62
|
if v.respond_to?(:empty?)
|
|
63
63
|
!v.empty?
|
|
64
64
|
else
|
|
@@ -82,7 +82,7 @@ module Plumb
|
|
|
82
82
|
# type = Types::Any.policy(respond_to: :upcase)
|
|
83
83
|
# type = Types::Any.policy(respond_to: [:upcase, :strip])
|
|
84
84
|
policy :respond_to do |type, method_names|
|
|
85
|
-
type.check("must respond to #{method_names.inspect}") do |value|
|
|
85
|
+
type.check("must respond to #{method_names.inspect}", inspect: "respond_to(#{method_names.inspect})") do |value|
|
|
86
86
|
Array(method_names).all? { |m| value.respond_to?(m) }
|
|
87
87
|
end
|
|
88
88
|
end
|
data/lib/plumb/version.rb
CHANGED