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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 808d4d88d53c7e5c5cd273504f1faa146dfc75cf1965722abdc63bbfaad5a1a7
4
- data.tar.gz: a2a1658bebd743544e6d9246ed4644775b44064d5f77865c8821d0ff2fa51db8
3
+ metadata.gz: 4c4884e54e901a4aba218432c58deaf9f8f8afbef8b5efe4e4109b847cd9d8c4
4
+ data.tar.gz: 5d1757c74adcb2146a588d4754966c3eb2576269d46574ae8b73656d224802d3
5
5
  SHA512:
6
- metadata.gz: 3ec5d6a028bb3c8d65a456b292b08c8e92fe609c7fb909d19217fdd1deee69bc0e63d58f4ba167e7474e7467e212c3ef0c4b6d69bf15ffcb50d1d2e4c1d45468
7
- data.tar.gz: 604743c6c4d3327d2a3c462d431eeb3ac919210bce67387e665ac06592d0d94b08727fa065f48ac33e160cce09484258b2c261fa9312103aec83480d8f9ba7a6
6
+ metadata.gz: c3049a98a899acd4f49bcd4b21563480acededfef0f61153e4b7a548460df5d40ebbc2ae9d2b5acf316763e1405c049da03e5fde05f7ad68d3b37fc4d8b6a427
7
+ data.tar.gz: 98be5ff267faba06a8defbc554451a247346a3cdd8d24d728fc020af4d142a186bf38568f5f5053964ab2b628641a7ea9c0fb1f4ec6e3aeb4b9080260f8de850
@@ -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.
@@ -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) ? matcher.inspect : "Constraint(#{label || @matcher.inspect})"
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
- #  TODO: here we should filter out the metadata that is not relevant for JSON Schema
504
- props = props.merge(stringify_keys(node.metadata))
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Plumb
4
- VERSION = '0.4.1'
4
+ VERSION = '0.4.2'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plumb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ismael Celis