Class: Katachi::AnyOf
- Inherits:
-
Object
- Object
- Katachi::AnyOf
- Includes:
- Enumerable
- Defined in:
- lib/katachi/any_of.rb
Overview
AnyOf is used for allowing multiple shapes to be matched a single value. If any of the shapes match the value, the value is considered a match. If none of the shapes match the value, the value is considered a mismatch. AnyOf is used in the following way: Katachi::Comparator.compare(value, Katachi::AnyOf[shape1, shape2, shape3])
Class Method Summary collapse
-
.[] ⇒ Object
AnyOf[shape1, shape2, shape3] is a shortcut for AnyOf.new(shape1, shape2, shape3).
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize(*shapes) ⇒ AnyOf
constructor
A new instance of AnyOf.
-
#inspect ⇒ Object
normally this
.inspectredefinition would be for.to_sbut Hash.to_s calls inspect on the keys and values, so we have to redefine inspect instead if we want a user-friendly string representation of complex objects. -
#kt_compare(value) ⇒ Object
AnyOf is considered a match if any of the shapes match the value If none of the shapes match the value, AnyOf is considered a mismatch.
Constructor Details
#initialize(*shapes) ⇒ AnyOf
Returns a new instance of AnyOf.
16 |
# File 'lib/katachi/any_of.rb', line 16 def initialize(*shapes) = (@shapes = shapes) |
Class Method Details
.[] ⇒ Object
AnyOf[shape1, shape2, shape3] is a shortcut for AnyOf.new(shape1, shape2, shape3)
15 |
# File 'lib/katachi/any_of.rb', line 15 def self.[](...) = new(...) |
Instance Method Details
#each ⇒ Object
18 |
# File 'lib/katachi/any_of.rb', line 18 def each(&) = @shapes.each(&) |
#inspect ⇒ Object
normally this .inspect redefinition would be for .to_s but Hash.to_s calls
inspect on the keys and values, so we have to redefine inspect instead
if we want a user-friendly string representation of complex objects
35 |
# File 'lib/katachi/any_of.rb', line 35 def inspect = "AnyOf[#{@shapes.map(&:inspect).join(", ")}]" |
#kt_compare(value) ⇒ Object
AnyOf is considered a match if any of the shapes match the value If none of the shapes match the value, AnyOf is considered a mismatch
22 23 24 25 26 27 28 29 30 |
# File 'lib/katachi/any_of.rb', line 22 def kt_compare(value) child_results = @shapes.to_h { |shape| [shape, Katachi::Comparator.compare(value:, shape:)] } Katachi::ComparisonResult.new( value:, shape: @shapes, code: child_results.values.any?(&:match?) ? :any_of_match : :any_of_mismatch, child_results:, ) end |