Class: Ward::Matchers::Include

Inherits:
Matcher
  • Object
show all
Defined in:
lib/ward/matchers/include.rb

Overview

Tests whether the validation value is contained in the expected value.

The expected value can be anything which responds to include?; if it returns true, the matcher will pass.

Examples:

Person role is either :admin or :staff


class Person
  validate do |person|
    person.role.is.in([:admin, :staff])
  end
end

Instance Attribute Summary

Attributes inherited from Matcher

#expected, #extra_args

Instance Method Summary collapse

Methods inherited from Matcher

error_id

Constructor Details

#initialize(expected = nil) ⇒ Include

Creates a new Include matcher instance.

Parameters:

  • expected (#include?) (defaults to: nil)

    The expected value for the matcher.

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
# File 'lib/ward/matchers/include.rb', line 23

def initialize(expected = nil)
  raise ArgumentError,
    'The Include matcher requires that a value which responds ' \
    'to #include? is supplied' unless expected.respond_to?(:include?)

  super
end

Instance Method Details

#customise_error_values(values) ⇒ String

Adds extra information to the error message.

Parameters:

  • error (String)

Returns:

  • (String)


47
48
49
50
# File 'lib/ward/matchers/include.rb', line 47

def customise_error_values(values)
  values[:expected] = Ward::Errors.format_exclusive_list(@expected)
  values
end

#matches?(actual) ⇒ Boolean

Returns whether the given value is included in the expected value.

Parameters:

  • actual (#include?)

    The validation value.

Returns:

  • (Boolean)


38
39
40
# File 'lib/ward/matchers/include.rb', line 38

def matches?(actual)
  @expected.include?(actual)
end