Sha256: 0571e2e999ba3944f89958d2b110fb7e512855fa2ac630bac4466cd4ab7ecda6

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

# encoding: utf-8

module ServiceObjects

  # The exception to be risen by invalid services
  #
  # @example
  #   object = ServiceObjects::Base.new
  #   fail ServiceObjects::Invalid.new object
  class Invalid < ::RuntimeError

    # @!scope class
    # @!method new(object)
    #
    # Constructs the exception for given service object
    #
    # @example (see ServiceObjects::Invalid)
    #
    # @param  [#messages] object
    #
    # @return [ServiceObjects::Invalid]
    def initialize(object)
      @object = object
      validate
    end

    # @!attribute [r] object
    # Invalid service object
    #
    # @return [#messages]
    attr_reader :object

    # The array of messages from the invalid {#object}
    #
    # @return [Array<ServiceObjects::Message>]
    def messages
      Array(object.messages).flatten
    end

    private

    # Checks if the {#object} respond to {#messages}
    #
    # @raise [TypeError]
    #   when the validation fails
    #
    # @return [undefined]
    def validate
      return if object.respond_to? :messages
      fail TypeError.new "#{ object.inspect } doesn't respond to #messages"
    end

  end # class Invalid

end # module ServiceObjects

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
service_objects-0.1.0 lib/service_objects/invalid.rb
service_objects-0.0.2 lib/service_objects/invalid.rb
service_objects-0.0.1 lib/service_objects/invalid.rb