Class: Sinatra::Trails::Scope
- Inherits:
-
Object
- Object
- Sinatra::Trails::Scope
show all
- Defined in:
- lib/sinatra/trails.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#after(*args, &block) ⇒ Object
-
#before(*args, &block) ⇒ Object
-
#generate_routes!(&block) ⇒ Object
-
#initialize(app, path, ancestors = []) ⇒ Scope
constructor
-
#map(name, opts = {}, &block) ⇒ Object
-
#namespace(path, &block) ⇒ Object
-
#resource(name, opts = {}, &block) ⇒ Object
-
#resources(name, opts = {}, &block) ⇒ Object
-
#route_for(name) ⇒ Object
(also: #[])
-
#routes_hash(&block) ⇒ Object
Constructor Details
#initialize(app, path, ancestors = []) ⇒ Scope
Returns a new instance of Scope.
100
101
102
103
104
105
|
# File 'lib/sinatra/trails.rb', line 100
def initialize app, path, ancestors = []
@ancestors, @routes = ancestors, []
@path = path.to_s.sub(/^\//, '') if path
@name = path if Symbol === path
@sinatra_app = app
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
153
154
155
156
157
158
159
|
# File 'lib/sinatra/trails.rb', line 153
def method_missing name, *args, &block
if @sinatra_app.respond_to? name
@sinatra_app.send name, *args, &block
else
route_for(name) or super
end
end
|
Instance Attribute Details
#ancestors ⇒ Object
Returns the value of attribute ancestors.
98
99
100
|
# File 'lib/sinatra/trails.rb', line 98
def ancestors
@ancestors
end
|
#name ⇒ Object
Returns the value of attribute name.
98
99
100
|
# File 'lib/sinatra/trails.rb', line 98
def name
@name
end
|
#path ⇒ Object
Returns the value of attribute path.
98
99
100
|
# File 'lib/sinatra/trails.rb', line 98
def path
@path
end
|
#routes ⇒ Object
Returns the value of attribute routes.
98
99
100
|
# File 'lib/sinatra/trails.rb', line 98
def routes
@routes
end
|
Instance Method Details
#after(*args, &block) ⇒ Object
131
132
133
134
135
|
# File 'lib/sinatra/trails.rb', line 131
def after *args, &block
opts = Hash === args.last ? args.pop : {}
@sinatra_app.after ScopeMatcher.new(self, args), opts, &block
end
|
#before(*args, &block) ⇒ Object
126
127
128
129
|
# File 'lib/sinatra/trails.rb', line 126
def before *args, &block
opts = Hash === args.last ? args.pop : {}
@sinatra_app.before ScopeMatcher.new(self, args), opts, &block
end
|
#generate_routes!(&block) ⇒ Object
137
138
139
140
|
# File 'lib/sinatra/trails.rb', line 137
def generate_routes! &block
instance_eval &block if block_given?
@routes
end
|
#map(name, opts = {}, &block) ⇒ Object
107
108
109
110
111
112
|
# File 'lib/sinatra/trails.rb', line 107
def map name, opts = {}, &block
path = opts.delete(:to) || name
route = Route.new(path, name.to_sym, [*ancestors, self], self)
instance_eval &block if block_given?
route
end
|
#namespace(path, &block) ⇒ Object
114
115
116
|
# File 'lib/sinatra/trails.rb', line 114
def namespace path, &block
@routes += Scope.new(@sinatra_app, path, [*ancestors, self]).generate_routes!(&block)
end
|
#resource(name, opts = {}, &block) ⇒ Object
118
119
120
|
# File 'lib/sinatra/trails.rb', line 118
def resource name, opts = {}, &block
restful_routes Resource, name, opts, &block
end
|
#resources(name, opts = {}, &block) ⇒ Object
122
123
124
|
# File 'lib/sinatra/trails.rb', line 122
def resources name, opts = {}, &block
restful_routes Resources, name, opts, &block
end
|
#route_for(name) ⇒ Object
Also known as:
[]
146
147
148
149
|
# File 'lib/sinatra/trails.rb', line 146
def route_for name
name = name.to_s
@routes.find{ |route| route.full_name == name || route.scope == self && route.name == name }
end
|
#routes_hash(&block) ⇒ Object
142
143
144
|
# File 'lib/sinatra/trails.rb', line 142
def routes_hash &block
Hash[*generate_routes!(&block).map{ |route| [route.full_name, route]}.flatten]
end
|