Sha256: abb23591fbcbec46b4ef39c9d5a2b9b45c0df85944f6cfc52aa8f1875634d03e

Contents?: true

Size: 1.91 KB

Versions: 4

Compression:

Stored size: 1.91 KB

Contents

require 'deas/runner'
require 'deas/template'

module Deas

  class SinatraRunner < Runner

    def self.run(*args)
      self.new(*args).run
    end

    def initialize(handler_class, sinatra_call)
      @sinatra_call  = sinatra_call
      @app_settings  = @sinatra_call.settings
      @logger        = @sinatra_call.settings.logger
      @params        = @sinatra_call.params
      @request       = @sinatra_call.request
      @response      = @sinatra_call.response
      @session       = @sinatra_call.session
      super(handler_class)
    end

    def run
      run_callbacks @handler_class.before_callbacks
      @handler.init
      response_data = @handler.run
      run_callbacks @handler_class.after_callbacks
      response_data
    end

    # Helpers

    def halt(*args)
      @sinatra_call.halt(*args)
    end

    def redirect(*args)
      @sinatra_call.redirect(*args)
    end

    def content_type(*args)
      return @sinatra_call.content_type if args.empty?

      opts, value = [
        args.last.kind_of?(::Hash) ? args.pop : {},
        args.first
      ]
      @sinatra_call.content_type(value, {
        :charset => @sinatra_call.settings.deas_default_charset
      }.merge(opts || {}))
    end

    def status(*args)
      @sinatra_call.status(*args)
    end

    def headers(*args)
      @sinatra_call.headers(*args)
    end

    def render(template_name, options = nil, &block)
      options ||= {}
      options[:locals] = { :view => @handler }.merge(options[:locals] || {})
      options[:layout] ||= @handler_class.layouts

      self.content_type(get_content_type(template_name)) if self.content_type.nil?
      Deas::Template.new(@sinatra_call, template_name, options).render(&block)
    end

    private

    def run_callbacks(callbacks)
      callbacks.each{|proc| @handler.instance_eval(&proc) }
    end

    def get_content_type(template_name)
      File.extname(template_name)[1..-1] || 'html'
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
deas-0.16.0 lib/deas/sinatra_runner.rb
deas-0.15.0 lib/deas/sinatra_runner.rb
deas-0.14.1 lib/deas/sinatra_runner.rb
deas-0.14.0 lib/deas/sinatra_runner.rb