module Assertion::DSL::Builder

Provides methods to build assertions and guards

Public Instance Methods

about(*attributes, &block) click to toggle source

Builds the subclass of ‘Assertion::Base` with predefined `attributes` and implementation of the `#check` method.

@example

IsMan = Assertion.about :age, :gender do
  (age >= 18) && (gender == :male)
end

# This is the same as:
class IsMan < Assertion::Base
  attribute :age, :gender

  def check
    (age >= 18) && (gender == :male)
  end
end

@param [Symbol, Array<Symbol>] attributes

The list of attributes for the new assertion

@param [Proc] block

The content for the `check` method

@return [Class] The specific assertion class

# File lib/assertion/dsl/builder.rb, line 35
def about(*attributes, &block)
  __build__(Base, attributes, :check, &block)
end
guards(attribute = nil, &block) click to toggle source

Builds the subclass of ‘Assertion::Guard` with given attribute (alias for the `object`) and implementation of the `#state` method.

@example

VoterOnly = Assertion.guards :user do
  IsAdult[user.attributes] & IsCitizen[user.attributes]
end

# This is the same as:
class VoterOnly < Assertion::Guard
  alias_method :user, :object

  def state
    IsAdult[user.attributes] & IsCitizen[user.attributes]
  end
end

@param [Symbol] attribute

The alias for the `object` attribute

@param [Proc] block

The content for the `state` method

@return [Class] The specific guard class

# File lib/assertion/dsl/builder.rb, line 63
def guards(attribute = nil, &block)
  __build__(Guard, attribute, :state, &block)
end

Private Instance Methods

__build__(type, attributes, name, &block) click to toggle source
# File lib/assertion/dsl/builder.rb, line 69
def __build__(type, attributes, name, &block)
  klass = Class.new(type)
  klass.public_send(:attribute, attributes) if attributes
  klass.__send__(:define_method, name, &block) if block_given?

  klass
end