Class: Ward::Matchers::Present

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

Overview

TODO:

Once the predicate matcher is available, amend the class documentation to provide an example of how to call blank? explicitly.

Tests whether the validation value is present.

A “present” value is one which:

* is a non-blank string, containing more than just whitespace
* responds to #empty? and returns false
* does not evaluate to false (i.e. not nil or false)

This is equivalent to ActiveSupport’s blank? extension methods but note that blank? is not actually used; if you define blank? on a class which is provided to the matcher it will not be called.

Examples:

Validating that the name attribute is present


class Person
  validate do |person|
    person.name.is.present
  end
end

Validating that the job attribute is not present :(


class Person
  validate do |person|
    person.job.is_not.present
  end
end

Instance Attribute Summary

Attributes inherited from Matcher

#expected, #extra_args

Instance Method Summary collapse

Methods inherited from Matcher

#customise_error_values, error_id, #initialize

Constructor Details

This class inherits a constructor from Ward::Matchers::Matcher

Instance Method Details

#matches?(actual) ⇒ Boolean

Returns whether the given value is present.

Parameters:

  • actual (Object)

    The validation value.

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
# File 'lib/ward/matchers/present.rb', line 44

def matches?(actual)
  if actual.kind_of?(String)
    actual.match(/\S/)
  elsif actual.respond_to?(:empty?)
    not actual.empty?
  else
    actual
  end
end