Sha256: 3828227c080a3c83a48230062c2aad028274efd531d69be14cb0111bcf97d11e

Contents?: true

Size: 1.45 KB

Versions: 10

Compression:

Stored size: 1.45 KB

Contents

require 'deas/sinatra_runner'

module Deas

  class Route
    attr_reader :method, :path, :handler_class_name, :handler_class

    def initialize(method, path, handler_class_name, handler_class = nil)
      @method = method
      @path   = path
      @handler_class_name = handler_class_name
      @handler_class      = handler_class
    end

    def constantize!
      @handler_class ||= constantize_name(handler_class_name)
      raise(NoHandlerClassError.new(handler_class_name)) if !@handler_class
    end

    def run(sinatra_call)
      sinatra_call.request.env.tap do |env|
        env['sinatra.params']          = sinatra_call.params
        env['deas.handler_class_name'] = @handler_class_name
        env['deas.logging'].call "  Handler: #{env['deas.handler_class_name']}"
        env['deas.logging'].call "  Params:  #{env['sinatra.params'].inspect}"
      end
      Deas::SinatraRunner.run(@handler_class, sinatra_call)
    end

    private

    def constantize_name(class_name)
      names = class_name.to_s.split('::').reject{|name| name.empty? }
      klass = names.inject(Object) do |constant, name|
        constant.const_get(name)
      end
      klass == Object ? false : klass
    rescue NameError
      false
    end

  end

  class NoHandlerClassError < RuntimeError
    def initialize(handler_class_name)
      super "Deas couldn't find the view handler '#{handler_class_name}'. " \
        "It doesn't exist or hasn't been required in yet."
    end
  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
deas-0.17.0 lib/deas/route.rb
deas-0.16.0 lib/deas/route.rb
deas-0.15.0 lib/deas/route.rb
deas-0.14.1 lib/deas/route.rb
deas-0.14.0 lib/deas/route.rb
deas-0.13.1 lib/deas/route.rb
deas-0.13.0 lib/deas/route.rb
deas-0.12.0 lib/deas/route.rb
deas-0.11.0 lib/deas/route.rb
deas-0.10.0 lib/deas/route.rb