Class: Matchi::Satisfy

Inherits:
Object
  • Object
show all
Defined in:
lib/matchi/satisfy.rb

Overview

Custom predicate matcher that validates values against an arbitrary condition.

This matcher provides a flexible way to test values against custom conditions defined in a block. Unlike specific matchers that test for predetermined conditions, Satisfy allows you to define any custom validation logic at runtime. This makes it particularly useful for complex or composite conditions that aren’t covered by other matchers.

Examples:

Basic numeric validation

matcher = Matchi::Satisfy.new { |n| n.positive? && n.even? }
matcher.match? { 2 }             # => true
matcher.match? { -2 }            # => false
matcher.match? { 3 }             # => false

String pattern validation

matcher = Matchi::Satisfy.new { |s| s.start_with?("test") && s.length < 10 }
matcher.match? { "test_123" }    # => true
matcher.match? { "test_12345" }  # => false
matcher.match? { "other" }       # => false

Complex object validation

class User
  attr_reader :age, :name
  def initialize(name, age)
    @name, @age = name, age
  end
end

matcher = Matchi::Satisfy.new { |user|
  user.age >= 18 && user.name.length >= 2
}
matcher.match? { User.new("Alice", 25) }  # => true
matcher.match? { User.new("B", 20) }      # => false

Using with collections

matcher = Matchi::Satisfy.new { |arr|
  arr.all? { |x| x.is_a?(Integer) } && arr.sum.even?
}
matcher.match? { [2, 4, 6] }      # => true
matcher.match? { [1, 3, 5] }      # => false
matcher.match? { [1, "2", 3] }    # => false

See Also:

Instance Method Summary collapse

Constructor Details

#initialize {|Object| ... } ⇒ Satisfy

Initialize the matcher with a validation block.

Examples:

Simple numeric validation

Satisfy.new { |n| n > 0 }

Complex condition

Satisfy.new { |obj|
  obj.respond_to?(:length) && obj.length.between?(2, 10)
}

Yields:

  • (Object)

    Block that defines the validation condition

Yield Parameters:

  • value

    The value to validate

Yield Returns:

  • (Boolean)

    true if the value meets the condition

Raises:

  • (ArgumentError)

    if no block is provided



68
69
70
71
72
# File 'lib/matchi/satisfy.rb', line 68

def initialize(&block)
  raise ::ArgumentError, "a block must be provided" unless block_given?

  @expected = block
end

Instance Method Details

#match? { ... } ⇒ Boolean

Checks if the yielded value satisfies the validation block.

This method passes the value returned by the provided block to the validation block defined at initialization. The matcher succeeds if the validation block returns a truthy value.

Examples:

Using with direct values

matcher = Satisfy.new { |n| n.positive? }
matcher.match? { 42 }    # => true
matcher.match? { -1 }    # => false

Using with computed values

matcher = Satisfy.new { |s| s.length.even? }
matcher.match? { "test".upcase }  # => true
matcher.match? { "a" * 3 }        # => false

Yields:

  • Block that returns the value to validate

Yield Returns:

  • (Object)

    The value to check against the condition

Returns:

  • (Boolean)

    true if the value satisfies the condition

Raises:

  • (ArgumentError)

    if no block is provided



98
99
100
101
102
# File 'lib/matchi/satisfy.rb', line 98

def match?
  raise ::ArgumentError, "a block must be provided" unless block_given?

  @expected.call(yield)
end

#to_sString

Returns a human-readable description of the matcher.

Examples:

Satisfy.new { |n| n > 0 }.to_s  # => "satisfy &block"

Returns:

  • (String)

    A string describing what this matcher verifies



112
113
114
# File 'lib/matchi/satisfy.rb', line 112

def to_s
  "satisfy &block"
end