Sha256: 2e0d4a71434e112a91844d6bb3d99f81078e094d76034f588035da8e780215ca

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

# encoding: utf-8

module Assertion

  # 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>
  #
  class Guard

    extend DSL::Attribute
    extend DSL::Caller

    # @!scope class
    # @!method new(object)
    # Creates the guard instance for the provided object
    #
    # @param [Object] object
    #
    # @return [Assertion::Guard]

    # @private
    def initialize(object)
      @object = object
      IceNine.deep_freeze(self)
    end

    # Validates the state of the [#object] and returns valid object back
    #
    # @return (see #object)
    #
    # @raise [Assertion::InvalidError] if the [#object] is invalid
    #
    def call
      state.validate!
      object
    end

  end # class Guard

end # module Assertion

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
assertion-0.2.3 lib/assertion/guard.rb
assertion-0.2.2 lib/assertion/guard.rb