Module: Ward::Matchers

Defined in:
lib/ward/matchers.rb,
lib/ward/matchers/has.rb,
lib/ward/matchers/nil.rb,
lib/ward/matchers/match.rb,
lib/ward/matchers/include.rb,
lib/ward/matchers/matcher.rb,
lib/ward/matchers/present.rb,
lib/ward/matchers/satisfy.rb,
lib/ward/matchers/close_to.rb,
lib/ward/matchers/equal_to.rb,
lib/ward/matchers/predicate.rb,
lib/ward/matchers/acceptance.rb

Overview

Matchers are used to determine whether a particular value is valid.

Any class instance can be a validator so long as it responds to #matches?; the #matches? method should take at least one argument which will be the value of the object being validated. The matcher should then return a true-like value if the match is successful or either nil, false, or an array whose first member is false, if the match was not successful.

In the event that your matcher returns an array, the second member will be used as the error message.

Defined Under Namespace

Classes: Acceptance, CloseTo, EqualTo, Has, Include, Match, Matcher, Nil, Predicate, Present, Satisfy

Class Method Summary collapse

Class Method Details

.matchersHash{Symbol => Ward::Matchers::Matcher}

Returns the registered matchers.

Returns:



76
77
78
# File 'lib/ward/matchers.rb', line 76

def self.matchers
  @matchers ||= {}
end

.register(slug, matcher) ⇒ Object

Registers a matcher and it’s slug.

A matcher can be registered with as many slugs as desired.

Examples:

Registering the Acceptance handler to be used with :accepted


Matchers.register(:accepted, Matchers::Acceptance)

# The Acceptance matcher can now be used like so...

validate do |form|
  form.acceptable_use_policy.is.accepted
  form.acceptable_use_policy.is_not.accepted
end

Registering the Has matcher twice.


Matchers.register(:has, Matchers::Acceptance)

validate do |post|
  post.has(1).author
end

# This provides access to the Has matcher, but "does_not.has" doesn't
# make much sense. So, we register the Has matcher a second time with
# a slug which make sense when used in the negative.

Matchers.register(:have, Matchers::Acceptance)

validate do |post|
  form.does_not.have(1).author
end

# Much better.

Parameters:

  • slug (Symbol, #to_sym)

    A slug which will be used by the DSL in order to assign a context to a matcher.

  • matcher (Ward::Matchers::Matcher)

    The matcher to be registered.



68
69
70
# File 'lib/ward/matchers.rb', line 68

def self.register(slug, matcher)
  matchers[slug.to_sym] = matcher
end