module Sinatra::Ace::Dsl

Attributes

actions[R]

Public Instance Methods

action(action, options={}, &block) click to toggle source
# File lib/sinatra/ace.rb, line 58
def action(action, options={}, &block)
        @actions ||= Array.new
        raise 'Error: path was specified at two places' if options[:path] && @path
        raise 'Error: version was specified at two places' if options[:version] && @version
        path = @path || options[:path] || '/'
        version = @version || options[:version] || nil
        @actions << {action: action, block: block, path: path, version: version}
end
dispatch!() click to toggle source
# File lib/sinatra/ace.rb, line 81
def dispatch!
        paths = @actions.map {|action| action[:path] }.uniq
        paths.each do |path|
                [:get, :post].each do |x|
                        send(x, path) do
                                actions = self.class.actions.select do |action|
                                        action[:path] == env['sinatra.route'].split(' ').last &&
                                        action[:action] == requested_action &&
                                        [requested_version, nil].include?(action[:version])
                                end
                                raise 'Error: action was not found' if actions.empty? # TODO: raise custom error
                                action = actions.find {|action| action[:version] == requested_version } || actions.first
                                logger.info "calling action: #{action}"
                                instance_eval(&action[:block])
                        end
                end
        end
end
path(path) { || ... } click to toggle source
# File lib/sinatra/ace.rb, line 74
def path(path)
        original_path = @path
        @path = path
        yield
        @path = original_path
end
version(version) { || ... } click to toggle source
# File lib/sinatra/ace.rb, line 67
def version(version)
        original_version = @version
        @version = version
        yield
        @version = original_version
end