Class: Matchi::Predicate
- Inherits:
-
Object
- Object
- Matchi::Predicate
- Defined in:
- lib/matchi/predicate.rb
Overview
Predicate matcher that checks if an object responds to a predicate method with a truthy value.
This matcher converts a predicate name (starting with ‘be_’ or ‘have_’) into a method call ending with ‘?’ and verifies that calling this method returns a boolean value. It’s useful for testing state-checking methods and collection properties. The matcher supports two types of predicate formats: ‘be_*’ which converts to ‘*?’ and ‘have_*’ which converts to ‘has_*?’.
Constant Summary collapse
- PREFIXES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Mapping of predicate prefixes to their method name transformations. Each entry defines how a prefix should be converted to its method form.
{ "be_" => ->(name) { "#{name.gsub(/\A(?:be_)/, "")}?" }, "have_" => ->(name) { "#{name.gsub(/\A(?:have_)/, "has_")}?" } }.freeze
Instance Method Summary collapse
-
#initialize(name, *args, **kwargs, &block) ⇒ Predicate
constructor
Initialize the matcher with a predicate name and optional arguments.
-
#match? { ... } ⇒ Boolean
Checks if the yielded object responds to and returns true for the predicate.
-
#to_s ⇒ String
Returns a human-readable description of the matcher.
Constructor Details
#initialize(name, *args, **kwargs, &block) ⇒ Predicate
Initialize the matcher with a predicate name and optional arguments.
74 75 76 77 78 79 80 81 |
# File 'lib/matchi/predicate.rb', line 74 def initialize(name, *args, **kwargs, &block) @name = String(name) raise ::ArgumentError, "invalid predicate name format" unless valid_name? @args = args @kwargs = kwargs @block = block end |
Instance Method Details
#match? { ... } ⇒ Boolean
Checks if the yielded object responds to and returns true for the predicate.
This method converts the predicate name into a method name according to the prefix mapping and calls it on the yielded object with any provided arguments. The method must return a boolean value, or a TypeError will be raised.
107 108 109 110 111 112 113 114 |
# File 'lib/matchi/predicate.rb', line 107 def match? raise ::ArgumentError, "a block must be provided" unless block_given? value = yield.send(method_name, *@args, **@kwargs, &@block) return value if [false, true].include?(value) raise ::TypeError, "Boolean expected, but #{value.class} instance returned." end |
#to_s ⇒ String
Returns a human-readable description of the matcher.
130 131 132 133 134 135 136 137 138 |
# File 'lib/matchi/predicate.rb', line 130 def to_s ( "#{@name.tr("_", " ")} " + [ @args.map(&:inspect).join(", "), @kwargs.map { |k, v| "#{k}: #{v.inspect}" }.join(", "), (@block.nil? ? "" : "&block") ].reject { |i| i.eql?("") }.join(", ") ).strip end |