Skip to content

Commit 673a8fe

Browse files
committed
Allow for helper modules.
1 parent 12e4478 commit 673a8fe

File tree

2 files changed

+118
-102
lines changed

2 files changed

+118
-102
lines changed

lib/grape/api.rb

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def set(key, value)
4949
def imbue(key, value)
5050
settings.imbue(key, value)
5151
end
52-
52+
5353
# Define a root URL prefix for your entire
5454
# API.
5555
def prefix(prefix = nil)
@@ -61,11 +61,11 @@ def prefix(prefix = nil)
6161
# @example API with legacy support.
6262
# class MyAPI < Grape::API
6363
# version 'v2'
64-
#
64+
#
6565
# get '/main' do
6666
# {:some => 'data'}
6767
# end
68-
#
68+
#
6969
# version 'v1' do
7070
# get '/main' do
7171
# {:legacy => 'data'}
@@ -85,8 +85,8 @@ def version(*args, &block)
8585
end
8686
end
8787
end
88-
89-
# Specify the default format for the API's
88+
89+
# Specify the default format for the API's
9090
# serializers. Currently only `:json` is
9191
# supported.
9292
def default_format(new_format = nil)
@@ -162,7 +162,7 @@ def represent(model_class, options)
162162
#
163163
# When called without a block, all known helpers within this scope
164164
# are included.
165-
#
165+
#
166166
# @example Define some helpers.
167167
# class ExampleAPI < Grape::API
168168
# helpers do
@@ -171,17 +171,17 @@ def represent(model_class, options)
171171
# end
172172
# end
173173
# end
174-
def helpers(&block)
175-
if block_given?
176-
m = settings.peek[:helpers] || Module.new
177-
m.class_eval &block
178-
set(:helpers, m)
174+
def helpers(mod = nil, &block)
175+
if block_given? || mod
176+
mod ||= settings.peek[:helpers] || Module.new
177+
mod.class_eval &block if block_given?
178+
set(:helpers, mod)
179179
else
180-
m = Module.new
180+
mod = Module.new
181181
settings.stack.each do |s|
182-
m.send :include, s[:helpers] if s[:helpers]
182+
mod.send :include, s[:helpers] if s[:helpers]
183183
end
184-
m
184+
mod
185185
end
186186
end
187187

@@ -215,7 +215,7 @@ def mount(mounts)
215215

216216
mounts.each_pair do |app, path|
217217
next unless app.respond_to?(:call)
218-
route_set.add_route(app,
218+
route_set.add_route(app,
219219
:path_info => compile_path(path, false)
220220
)
221221
end
@@ -271,8 +271,8 @@ def route(methods, paths = ['/'], route_options = {}, &block)
271271
def before(&block)
272272
settings.imbue(:befores, [block])
273273
end
274-
275-
def after(&block)
274+
275+
def after(&block)
276276
settings.imbue(:afters, [block])
277277
end
278278

@@ -291,14 +291,14 @@ def namespace(space = nil, &block)
291291
Rack::Mount::Utils.normalize_path(settings.stack.map{|s| s[:namespace]}.join('/'))
292292
end
293293
end
294-
294+
295295
alias_method :group, :namespace
296296
alias_method :resource, :namespace
297297
alias_method :resources, :namespace
298298
alias_method :segment, :namespace
299-
299+
300300
# Create a scope without affecting the URL.
301-
#
301+
#
302302
# @param name [Symbol] Purely placebo, just allows to to name the scope to make the code more readable.
303303
def scope(name = nil, &block)
304304
nest(block)
@@ -325,13 +325,13 @@ def middleware
325325
def routes
326326
@routes ||= []
327327
end
328-
328+
329329
def versions
330330
@versions ||= []
331331
end
332-
332+
333333
protected
334-
334+
335335
# Execute first the provided block, then each of the
336336
# block passed in. Allows for simple 'before' setups
337337
# of settings stack pushes.
@@ -355,11 +355,11 @@ def aggregate_setting(key)
355355

356356
def build_endpoint(&block)
357357
b = Rack::Builder.new
358-
b.use Grape::Middleware::Error,
359-
:default_status => settings[:default_error_status] || 403,
360-
:rescue_all => settings[:rescue_all],
361-
:rescued_errors => settings[:rescued_errors],
362-
:format => settings[:error_format] || :txt,
358+
b.use Grape::Middleware::Error,
359+
:default_status => settings[:default_error_status] || 403,
360+
:rescue_all => settings[:rescue_all],
361+
:rescued_errors => settings[:rescued_errors],
362+
:format => settings[:error_format] || :txt,
363363
:rescue_options => settings[:rescue_options],
364364
:rescue_handlers => settings[:rescue_handlers] || {}
365365

@@ -382,15 +382,15 @@ def build_endpoint(&block)
382382
representations = settings[:representations] || {}
383383

384384
endpoint = Grape::Endpoint.generate({
385-
:befores => befores,
385+
:befores => befores,
386386
:afters => afters,
387387
:representations => representations
388388
}, &block)
389389
endpoint.send :include, helpers
390390
b.run endpoint
391391
b.to_app
392392
end
393-
393+
394394
def inherited(subclass)
395395
subclass.reset!
396396
subclass.logger = logger.clone

0 commit comments

Comments
 (0)