Sha256: 09131598f7c3bf21916cc509ed33c3c6e0cc1ddd008d470d10fa1cfdbe77136f

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

require 'active_support/concern'
require 'active_support/all'

module Praxis
  module Controller
    extend ::ActiveSupport::Concern

    # A Controller always requires the callbacks
    include Praxis::Callbacks

    included do
      attr_reader :request
      attr_accessor :response
    end

    module ClassMethods
      def implements(definition)
        define_singleton_method(:definition) do
          definition
        end

        definition.controller = self
        # `implements` should only be processed while the application initializes/setup
        # So we will use the `.instance` function to get the "current" application instance
        Application.instance.controllers << self
      end

      def id
        self.name.gsub('::'.freeze,'-'.freeze)
      end
    end

    def initialize(request, response=Responses::Ok.new)
      @request = request
      @response = response
    end

    def definition
      self.class.definition
    end

    def media_type
      if (response_definition = self.request.action.responses[self.response.name])
        return response_definition.media_type
      else
        self.definition.media_type
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
praxis-0.22.pre.2 lib/praxis/controller.rb
praxis-0.22.pre.1 lib/praxis/controller.rb