Class: CustomMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-custom-matchers.rb

Defined Under Namespace

Modules: Helper

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected = nil) ⇒ CustomMatcher

Returns a new instance of CustomMatcher.



11
12
13
# File 'lib/rspec-custom-matchers.rb', line 11

def initialize(expected = nil)
  @expected = expected
end

Class Method Details

.create(class_name, &block) ⇒ Object



5
6
7
8
9
# File 'lib/rspec-custom-matchers.rb', line 5

def self.create(class_name, &block)
  klass = Class.new(CustomMatcher)
  klass.send(:define_method, :matcher, &block) if block_given?
  Object.const_set(build_class_name(class_name), klass)
end

Instance Method Details

#description(*args) ⇒ Object



36
37
38
# File 'lib/rspec-custom-matchers.rb', line 36

def description *args
  class_display_name
end

#failure_messageObject



15
16
17
# File 'lib/rspec-custom-matchers.rb', line 15

def failure_message
  message
end

#matcher(target, expected) ⇒ Object



23
24
25
# File 'lib/rspec-custom-matchers.rb', line 23

def matcher(target, expected)
  target == expected
end

#matches?(target) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
# File 'lib/rspec-custom-matchers.rb', line 27

def matches?(target)
  @target = target
  if self.method(:matcher).arity == 2
    matcher(@target, @expected)
  else
    matcher(@target)
  end
end

#negative_failure_messageObject



19
20
21
# File 'lib/rspec-custom-matchers.rb', line 19

def negative_failure_message
  message(false)
end