Class: Assertion::Base

Inherits:
Object
  • Object
show all
Extended by:
Attributes
Includes:
Messages
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 Adult < Assertion::Base
  attribute :name, :age

  def check
    age >= 18
  end
end

child = Adult.not

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

Direct Known Subclasses

Inversion

Constant Summary

Constants included from Messages

Messages::DICTIONARY, Messages::ROOT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Attributes

attribute

Methods included from Messages

#message

Constructor Details

#initialize(args = {}) ⇒ Base

Returns a new instance of Base.



102
103
104
105
106
# File 'lib/assertion/base.rb', line 102

def initialize(args = {})
  @attributes = {}
  self.class.attributes.each { |name| __set_attribute__ name, args[name] }
  freeze
end

Instance Attribute Details

#attributesHash (readonly)

Returns The hash of the allowed attributes having been initialized.

Examples:

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

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

Returns:

  • (Hash)

    The hash of the allowed attributes having been initialized



99
100
101
# File 'lib/assertion/base.rb', line 99

def attributes
  @attributes
end

Class Method Details

.[](hash = {}) ⇒ Assertion::State

Initializes an assertion with some attributes (data) and then calls it

Parameters:

  • hash (Hash) (defaults to: {})

Returns:

  • (Assertion::State)

    The object that describes the state of the assertion applied to given attributes



53
54
55
# File 'lib/assertion/base.rb', line 53

def [](hash = {})
  new(hash).call
end

.notAssertion::Inverter

Initializes the intermediate inverter with new and [] methods

The inverter can be used to initialize the assertion, that describes just the opposite statement to the current one

Examples:

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

joe = { name: 'Joe', age: 19 }

Adult[joe].valid?     # => true
Adult.not[joe].valid? # => false

Returns:



74
75
76
# File 'lib/assertion/base.rb', line 74

def not
  Inverter.new(self)
end

Instance Method Details

#callCheck::State

Calls the assertion checkup and returns the state of the assertion having been applied to the current attributes

Returns:

  • (Check::State)

    The state of the assertion being applied to its attributes



114
115
116
117
# File 'lib/assertion/base.rb', line 114

def call
  state = check
  State.new state, message(false)
end