Module: Sinatra::Ace::Dsl

Defined in:
lib/sinatra/ace.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



56
57
58
# File 'lib/sinatra/ace.rb', line 56

def actions
  @actions
end

Instance Method Details

#action(action, options = {}, &block) ⇒ Object



58
59
60
61
62
63
64
65
# 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!Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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) ⇒ Object



74
75
76
77
78
79
# File 'lib/sinatra/ace.rb', line 74

def path(path)
	original_path = @path
	@path = path
	yield
	@path = original_path
end

#version(version) ⇒ Object



67
68
69
70
71
72
# File 'lib/sinatra/ace.rb', line 67

def version(version)
	original_version = @version
	@version = version
	yield
	@version = original_version
end