Sha256: 902315d465c36c0caf0d88b01e5586b3abc446c3a58c2f308afdf3bb3be39e96

Contents?: true

Size: 1.69 KB

Versions: 3

Compression:

Stored size: 1.69 KB

Contents

# encoding: utf-8

module Assertion

  # Describes the state of the assertion applied to given arguments
  # (the result of the checkup)
  #
  # @api public
  #
  class State

    # @!scope class
    # @!method new(state, *messages)
    # Creates the immutable state instance with a corresponding error messages
    #
    # @param [Boolean] state
    # @param [String, Array<String>] messages
    #
    # @return [Assertion::State]

    # @private
    def initialize(state, *messages)
      @state = state
      @messages = (state ? [] : messages.flatten.uniq).freeze
      freeze
    end

    # @!attribute [r] messages
    #
    # @return [Array<String>] error messages
    #
    attr_reader :messages

    # Check whether a stated assertion is satisfied by its attributes
    #
    # @return [Boolean]
    #
    def valid?
      !invalid?
    end

    # Check whether a stated assertion is not satisfied by its attributes
    #
    # @return [Boolean]
    #
    def invalid?
      !@state
    end

    # Check whether a stated assertion is satisfied by its attributes
    #
    # @return [true]
    #
    # @raise [Assertion::InvalidError]
    #   When an assertion is not satisfied (validation fails)
    #
    def validate!
      invalid? ? fail(InvalidError.new messages) : true
    end

    # Composes the state with the other state
    #
    # @param [Assertion::State] other
    #
    # @return [Assertion::State]
    #   The composed state that carries messages from both the states
    #
    # @alias >>
    # @alias +
    #
    def &(other)
      self.class.new(valid? & other.valid?, messages + other.messages)
    end
    alias_method :>>, :&
    alias_method :+, :&

  end # class State

end # module Assertion

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
assertion-0.2.1 lib/assertion/state.rb
assertion-0.2.0 lib/assertion/state.rb
assertion-0.1.0 lib/assertion/state.rb