Module: Assertion::DSL::Builder

Included in:
Assertion
Defined in:
lib/assertion/dsl/builder.rb

Overview

Provides methods to build assertions and guards

Instance Method Summary collapse

Instance Method Details

#about(*attributes, &block) ⇒ Class

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

Examples:

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

Parameters:

  • attributes (Symbol, Array<Symbol>)

    The list of attributes for the new assertion

  • block (Proc)

    The content for the ‘check` method

Returns:

  • (Class)

    The specific assertion class



35
36
37
# File 'lib/assertion/dsl/builder.rb', line 35

def about(*attributes, &block)
  __build__(Base, attributes, :check, &block)
end

#guards(attribute = nil, &block) ⇒ Class

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

Examples:

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

Parameters:

  • attribute (Symbol) (defaults to: nil)

    The alias for the ‘object` attribute

  • block (Proc)

    The content for the ‘state` method

Returns:

  • (Class)

    The specific guard class



63
64
65
# File 'lib/assertion/dsl/builder.rb', line 63

def guards(attribute = nil, &block)
  __build__(Guard, attribute, :state, &block)
end