Sha256: a64c9a37b7e979951efddffe565816509ef0a5c2ad9b4678e640e96e28600641

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

module Sinatra
  
  class Scope

    def initialize(app, pattern, &blk)
      @app = app
      @pattern = pattern || ''
      @block = blk
    end

    attr_reader :pattern, :block

    def create_action(http_method, pattern, &blk)
      scope = self
      @app.send(http_method, scope.pattern + pattern) do |*args|
        # run the scope block
        self.instance_exec(*args[0...scope.block.arity], &scope.block)
        # run the action block
        self.instance_exec(*args[scope.block.arity..-1], &blk)
      end
    end

    def get(pattern, &blk)
      create_action :get, pattern, &blk
    end

    def post(pattern, &blk)
      create_action :post, pattern, &blk
    end

  end
  
  module ScopeBuilder
    
    def scope(name, pattern = nil, &blk)
      @scopes ||= {}
      @scopes[name] = Scope.new Sinatra::Application, pattern, &blk
    end
    
    # hacky? I can't figure out which object to #define_method on
    def method_missing(name)
      return super unless @scopes.has_key? name
      @scopes[name]
    end
    
  end

  register ScopeBuilder
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sinatra-scopes-0.1.0 lib/sinatra/scopes.rb