Class: Assertion::Base
- Inherits:
-
Object
- Object
- Assertion::Base
- 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.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes ⇒ Hash
readonly
The hash of the allowed attributes having been initialized.
Class Method Summary collapse
-
.call ⇒ Assertion::State
Calls the assertion checkup and returns the resulting state.
- .initialize(args = {}) ⇒ Object
-
.message(state = nil) ⇒ String
Returns the message describing the current state.
-
.new(args = {}) ⇒ Assertion::Base
Initializes an assertion for the current object.
-
.translator ⇒ Assertion::Translator
The translator of states for the current class.
Methods included from DSL::Attributes
Methods included from DSL::Inversion
Methods included from DSL::Caller
Instance Attribute Details
#attributes ⇒ Hash (readonly)
Returns The hash of the allowed attributes having been initialized.
63 64 65 |
# File 'lib/assertion/base.rb', line 63 def attributes @attributes end |
Class Method Details
.call ⇒ Assertion::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.
98 99 100 |
# File 'lib/assertion/base.rb', line 98 def call State.new check, end |
.initialize(args = {}) ⇒ Object
74 75 76 77 78 |
# File 'lib/assertion/base.rb', line 74 def initialize(args = {}) keys = self.class.attributes @attributes = Hash[keys.zip(args.values_at(*keys))] freeze end |
.message(state = nil) ⇒ String
Returns the message describing the current state
86 87 88 |
# File 'lib/assertion/base.rb', line 86 def (state = nil) self.class.translator.call(state, attributes) end |
.new(args = {}) ⇒ Assertion::Base
Initializes an assertion for the current object
|
# File 'lib/assertion/base.rb', line 65
|
.translator ⇒ Assertion::Translator
The translator of states for the current class
46 47 48 |
# File 'lib/assertion/base.rb', line 46 def self.translator @translator ||= Translator.new(self) end |