Class: Matchi::Match

Inherits:
BasicObject
Defined in:
lib/matchi/match.rb

Overview

**Regular expressions** matcher.

Instance Method Summary collapse

Constructor Details

#initialize(expected) ⇒ Match

Initialize the matcher with an instance of Regexp.

Examples:

Username matcher.

Matchi::Match.new(/^[a-z0-9_-]{3,16}$/)

Parameters:

  • expected (#match)

    A regular expression.



10
11
12
# File 'lib/matchi/match.rb', line 10

def initialize(expected)
  @expected = expected
end

Instance Method Details

#matches?Boolean

Boolean comparison between the actual value and the expected value.

Examples:

Is it matching /^foo$/ regex?

match = Matchi::Match.new(/^foo$/)
match.matches? { 'foo' } # => true

Yield Returns:

  • (#object_id)

    the actual value to compare to the expected one.

Returns:

  • (Boolean)

    Comparison between actual and expected values.



23
24
25
# File 'lib/matchi/match.rb', line 23

def matches?
  @expected.match(yield).nil?.equal?(false)
end

#to_hHash

Returns a hash of one key-value pair with a key corresponding to the

matcher and a value corresponding to its initialize parameters.

Examples:

A FooBar matcher serialized into a hash.

matcher = Matchi::FooBar.new(42)
matcher.to_h # => { FooBar: 42 }

Returns:

  • (Hash)

    A hash of one key-value pair.



46
47
48
# File 'lib/matchi/match.rb', line 46

def to_h
  { Match: [@expected] }
end

#to_sString

Returns a string representing the matcher.

Examples:

The readable definition of a FooBar matcher.

matcher = Matchi::FooBar.new(42)
matcher.to_s # => "foo_bar 42"

Returns:

  • (String)

    A string representing the matcher.



34
35
36
# File 'lib/matchi/match.rb', line 34

def to_s
  "match #{@expected.inspect}"
end