Sha256: 4c5ff1049bc2411037bdaec43a7e5bfbf29bf1f94075ce83173a7626c9e625d6

Contents?: true

Size: 1.4 KB

Versions: 8

Compression:

Stored size: 1.4 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)
      @method = method
      @path   = path
      @handler_class_name = handler_class_name
      @handler_class      = nil
    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'] = @handler_class
        env['deas.logging'].call "  Handler: #{env['deas.handler_class']}"
        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

8 entries across 8 versions & 1 rubygems

Version Path
deas-0.9.0 lib/deas/route.rb
deas-0.8.0 lib/deas/route.rb
deas-0.7.0 lib/deas/route.rb
deas-0.6.0 lib/deas/route.rb
deas-0.5.0 lib/deas/route.rb
deas-0.4.1 lib/deas/route.rb
deas-0.4.0 lib/deas/route.rb
deas-0.3.0 lib/deas/route.rb