Class: Assertion::Guard
- Inherits:
-
Object
- Object
- Assertion::Guard
- Extended by:
- Attributes
- Defined in:
- lib/assertion/guard.rb
Overview
The base class for object guards
The guard defines a desired state for the object and checks if that state is valid.
Its ‘call` method either returns the guarded object, or (when its state is invalid) raises an exception
The class DSL also defines a ‘.[]` shortcut to initialize and call the guard for given object immediately.
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
The object whose state should be tested.
Class Method Summary collapse
-
.[](object) ⇒ Object
Initializes and guard for the provided object and calls it immediately.
-
.call ⇒ Object
Validates the state of the [#object] and returns valid object back.
- .initialize(object) ⇒ Object
-
.new(object) ⇒ Assertion::Guard
Creates the guard instance for the provided object.
Methods included from Attributes
Instance Attribute Details
#object ⇒ Object (readonly)
Returns The object whose state should be tested.
63 64 65 |
# File 'lib/assertion/guard.rb', line 63 def object @object end |
Class Method Details
.[](object) ⇒ Object
Initializes and guard for the provided object and calls it immediately
47 48 49 |
# File 'lib/assertion/guard.rb', line 47 def [](object) new(object).call end |
.call ⇒ Object
Validates the state of the [#object] and returns valid object back
86 87 88 89 |
# File 'lib/assertion/guard.rb', line 86 def call state.validate! object end |
.initialize(object) ⇒ Object
74 75 76 77 78 |
# File 'lib/assertion/guard.rb', line 74 def initialize(object) @object = object self.class.attributes.each(&method(:__set_attribute__)) freeze end |
.new(object) ⇒ Assertion::Guard
Creates the guard instance for the provided object
|
|
# File 'lib/assertion/guard.rb', line 65
|