Class: Assertion::Base

Inherits:
Object
  • Object
show all
Extended by:
DSL::Attributes, DSL::Caller, DSL::Inversion
Defined in:
lib/assertion/base.rb

Overview

The base class for assertions about some attributes

Every assertion should define a list of attributes to be checked and the [#check] method to apply the assertion to those attributes

The assertion call method provides the object, describing the state of the assertion applied to its attributes. The provided state carries the result of the checkup and a corresponding <error> message. Later it can be composed with other states to provide complex validation.

The class DSL also defines shortcuts:

  • .[]

    can be used to initialize the assertion for given attributes and

    then apply it immediately with creation of the corresponding state.

  • .not

    can be used to provide the assertion opposite to the initial one.

Examples:

class IsAdult < Assertion::Base
  attribute :name, :age

  def check
    age >= 18
  end
end

child = IsAdult.not

jane = { name: "Jane", age: 12 }
IsAdult[jane].valid? # => false
child[jane].valid? # => true

API:

  • public

Direct Known Subclasses

Inversion

Instance Attribute Summary collapse

Class Method Summary collapse

Methods included from DSL::Attributes

attribute, extended

Methods included from DSL::Inversion

not

Methods included from DSL::Caller

[]

Instance Attribute Details

#attributesHash (readonly)

Returns The hash of the allowed attributes having been initialized.

Examples:

IsAdult = Assertion.about :name, :age do
  age >= 18
end

adult = IsAdult[name: "Joe", age: 15, gender: :male]
adult.attributes # => { name: "Joe", age: 15 }

Returns:

  • The hash of the allowed attributes having been initialized

API:

  • public



68
69
70
# File 'lib/assertion/base.rb', line 68

def attributes
  @attributes
end

Class Method Details

.callAssertion::State

Calls the assertion checkup and returns the resulting state

The state is a unified composable object, unaware of the structure and attributes of the specific assertion.

Returns:

  • The state of the assertion being applied to its attributes

API:

  • public



105
106
107
# File 'lib/assertion/base.rb', line 105

def call
  State.new check, message
end

.initialize(args = {}) ⇒ Object

API:

  • public



79
80
81
82
83
# File 'lib/assertion/base.rb', line 79

def initialize(args = {})
  keys = self.class.attributes
  @attributes = Hash[keys.zip(args.values_at(*keys))]
  IceNine.deep_freeze(self)
end

.message(state = nil) ⇒ String

Returns the message describing the current state

Parameters:

  • (defaults to: nil)

    The state to describe

Returns:

API:

  • public



91
92
93
94
95
# File 'lib/assertion/base.rb', line 91

def message(state = nil)
  msg = state ? :truthy : :falsey
  return public_send(msg) if respond_to? msg
  self.class.translate(msg, attributes)
end

.new(args = {}) ⇒ Assertion::Base

Initializes an assertion for the current object

Parameters:

  • (defaults to: {})

    The arguments to check

Returns:



# File 'lib/assertion/base.rb', line 70


.translate(state, attributes) ⇒ Object

API:

  • public



51
52
53
# File 'lib/assertion/base.rb', line 51

def self.translate(state, attributes)
  translator.call(state, attributes)
end

.translatorObject

The class-specific translator of assertion states

API:

  • public



46
47
48
# File 'lib/assertion/base.rb', line 46

def self.translator
  @translator ||= Translator.new(self)
end