Sha256: 645e13a7d596ae0bbcac9eed4cd57a51b382f5c544135d28b47871436f9ee80b

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

module Substation

  # Encapsulates the application environment and an input model instance
  class Request

    include Concord.new(:env, :input)
    include Adamantium::Flat

    # The application environment
    #
    # @example
    #
    #   class SomeUseCase
    #     def self.call(request)
    #       request.env
    #     end
    #   end
    #
    # @return [Object]
    #
    # @api public
    attr_reader :env

    # The input passed to an action
    #
    # @example
    #
    #   class SomeUseCase
    #     def self.call(request)
    #       request.input
    #     end
    #   end
    #
    # @return [Object]
    #
    # @api public
    attr_reader :input

    alias_method :data, :input

    # Create a new successful response
    #
    # @example
    #
    #   class SomeUseCase
    #     def self.call(request)
    #       data = perform_use_case
    #       request.success(data)
    #     end
    #   end
    #
    # @param [Object] output
    #   the data associated with the response
    #
    # @return [Response::Success]
    #
    # @api public
    def success(output)
      respond_with(Response::Success, output)
    end

    # Create a new failure response
    #
    # @example
    #
    #   class SomeUseCase
    #     def self.call(request)
    #       error = perform_use_case
    #       request.error(error)
    #     end
    #   end
    #
    # @param [Object] output
    #   the data associated with the response
    #
    # @return [Response::Failure]
    #
    # @api public
    def error(output)
      respond_with(Response::Failure, output)
    end

    private

    # Instantiate an instance of +klass+ and pass +output+
    #
    # @param [Response::Success, Response::Failure] klass
    #   the response class
    #
    # @param [Object] output
    #   the data associated with the response
    #
    # @return [Response::Success, Response::Failure]
    #
    # @api private
    def respond_with(klass, output)
      klass.new(self, output)
    end

  end # class Request
end # module Substation

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
substation-0.0.10.beta2 lib/substation/request.rb