Verifier

The library to verify variables are satisfy given condititions and raise ArgumentError otherwise.

Defines a #verify instance method with a block to be yielded in a variable's scope and raised an error if the block returns false or fails.

Installation

Add this line to your application's Gemfile:

gem "verifier"

And then execute:

$ bundle

Or install it yourself as:

$ gem install verifier

Usage

Include the module into class or Method to use it in the instance methods, or extend the class to check verifier of the class methods.

require "verifier"

class Greeter
  include Verifier
end

This will define the #verify instance method.

Call it from your method with the names of the verified variable and the condition, and the block to be called in the scope of a value, returned by the method.

class Greeter
  # ...
  def hello(name)
    verify(:name, :long_enough) { count > 1 }
    "Hello, #{ name }!"
  end
end

Then define i18n translations for error messages:

# config/locales/en.yml
---
en:
  verifier: # the module's scope
    greeter:  # the class name
      instance: # the scope for the method
        # the message to be raised when the method raises an exception
        fails: "The method fails: %{value}"
        # the message to be raised when the method isn't defined
        undefined: "The method undefined: %{value}"
        name: # the name of the method to be verified
          # the message to be raised when the block fails or returns false
          long_enough: "The name to greet seems damn short: %{value}"

The block is yielded in the scope of a corresponding variable.

An ArgumentError will be raised in any case when: * the verified method is undefined; * the method raises an exception; * the block raises an exception; * the block returns a value, whose negation is true (see the motivation [below]{#negation}).

greeter = Greeter.new

greeter.hello "Al"
# => "Hello, Al!"

greeter.hello "A"
# => <ArgumentError @message="The name to greet seems damn short: A" ...>

Compatibility

Tested under MRI rubies >= 2.1

RSpec 3.0+ used for testing

Collection of used testing, debugging and code metric tools is defined in the hexx-suit gem.

Contributing

License

See MIT LICENSE.