Class: Sinatra::Controllers::Mapping
- Inherits:
-
Object
- Object
- Sinatra::Controllers::Mapping
- Defined in:
- lib/sinatra-controllers.rb
Constant Summary collapse
- @@routes =
[]
Instance Attribute Summary collapse
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#opts ⇒ Object
Returns the value of attribute opts.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(klass, opts = {}) ⇒ Mapping
constructor
A new instance of Mapping.
- #method_missing(method, *a) ⇒ Object
- #parse ⇒ Object
- #route(verb, path, action) ⇒ Object
Constructor Details
#initialize(klass, opts = {}) ⇒ Mapping
Returns a new instance of Mapping.
23 24 25 26 |
# File 'lib/sinatra-controllers.rb', line 23 def initialize(klass,opts={}) @klass = klass @opts = opts end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *a) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/sinatra-controllers.rb', line 55 def method_missing(method,*a) case method.to_s when /(get|post|delete|put)/ route(method, *a) else super end end |
Instance Attribute Details
#klass ⇒ Object
Returns the value of attribute klass.
22 23 24 |
# File 'lib/sinatra-controllers.rb', line 22 def klass @klass end |
#opts ⇒ Object
Returns the value of attribute opts.
22 23 24 |
# File 'lib/sinatra-controllers.rb', line 22 def opts @opts end |
Class Method Details
.routes ⇒ Object
28 29 30 |
# File 'lib/sinatra-controllers.rb', line 28 def self.routes @@routes end |
Instance Method Details
#parse ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/sinatra-controllers.rb', line 43 def parse klass.instance_methods.each do |meth| verb,name = meth.to_s.scan(/^(get|post|delete|put)_(.*)$/).flatten next unless verb scope = '/' + (opts[:scope] || klass.to_s.downcase) scope.gsub!(%r{^/+},'/') route verb,"#{scope}/#{name.downcase}", meth end end |
#route(verb, path, action) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/sinatra-controllers.rb', line 32 def route(verb, path, action) aklass = klass # need this so it will stay in scope for closure block = proc { aklass.new(params, request).send action } if path[0] != '/' path = '/' + File.join(opts[:scope] || '', path) end @@routes.push [verb.to_sym, path, "#{aklass}\##{action}"] path.gsub!(%r{^/+},'/') Sinatra::Base.send verb, path, &block end |