Sha256: b844b68a532f80fcb2c6adccb1a07d711de63d05365f79b90a1badda88a487a0

Contents?: true

Size: 1.2 KB

Versions: 4

Compression:

Stored size: 1.2 KB

Contents

require 'happy/controller_ext/routing'
require 'happy/controller_ext/actions'
require 'happy/controller_ext/rackable'

module Happy
  class Controller
    include ControllerExtensions::Routing
    include ControllerExtensions::Actions
    include ControllerExtensions::Rackable

    attr_reader :options, :env

    delegate :request, :response, :remaining_path, :params, :session,
      :render, :url_for,
      :to => :context

    def initialize(env = {}, options = {}, &blk)
      @env = env
      @options = options
      instance_exec(&blk) if blk
    end

    def perform
      context.with_controller(self) do
        route
      end
    end

  private

    def context
      @env['happy.context'] ||= self.class.context_class.from_env(@env)
    end

    def route
      instance_exec(&self.class.route_blk) if self.class.route_blk
    end

    class << self
      attr_reader :route_blk

      def route(&blk)
        @route_blk = blk
      end

      def context(&blk)
        context_class.class_exec(&blk)
      end

      def context_class
        Happy::Context
      end

      def build(&blk)
        Class.new(self).tap do |klass|
          klass.instance_eval(&blk) if blk
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
happy-0.1.0.pre8 lib/happy/controller.rb
happy-0.1.0.pre7 lib/happy/controller.rb
happy-0.1.0.pre.6 lib/happy/controller.rb
happy-0.1.0.pre.5 lib/happy/controller.rb