class Assertion::Guard
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.
@example
class AdultOnly < Assertion::Guard alias_method :user, :object def state IsAdult[user.attributes] end end jack = User.new name: "Jack", age: 10 john = User.new name: "John", age: 59 AdultOnly[jack] # => #<Assertion::InvalidError @messages=["Jack is a child (age 10)"]> AdultOnly[john] # => #<User @name="John", @age=59>
Attributes
object[R]
@!attribute [r] object
@return [Object] The object to be guarded
Public Class Methods
new(object)
click to toggle source
@private
# File lib/assertion/guard.rb, line 53 def initialize(object) @object = object IceNine.deep_freeze(self) end
Public Instance Methods
call()
click to toggle source
Validates the state of the [#object] and returns valid object back
@return (see object
)
@raise [Assertion::InvalidError] if the [#object] is invalid
# File lib/assertion/guard.rb, line 64 def call state.validate! object end