Skip to content

Commit 05158c7

Browse files
author
Michael Bleigh
committed
Merging in master.
2 parents 5e96cb0 + 8d1a6ca commit 05158c7

File tree

4 files changed

+73
-21
lines changed

4 files changed

+73
-21
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@ pkg
2323
.yardoc/*
2424
dist
2525
Gemfile.lock
26+
*.orig
27+
*.rej
2628

2729
## PROJECT::SPECIFIC

README.markdown

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,37 @@ To circumvent this default behaviour, one could use the `:strict` option. When t
8686

8787
Serialization takes place automatically. For more detailed usage information, please visit the [Grape Wiki](http://github.com/intridea/grape/wiki).
8888

89+
## Helpers
90+
91+
You can define helper methods that your endpoints can use with the `helpers`
92+
macro by either giving a block or a module:
93+
94+
````ruby
95+
module MyHelpers
96+
def say_hello(user)
97+
"hey there #{user.name}"
98+
end
99+
end
100+
101+
class API < Grape::API
102+
# define helpers with a block
103+
helpers do
104+
def current_user
105+
User.find(params[:user_id])
106+
end
107+
end
108+
109+
# or mix in a module
110+
helpers MyHelpers
111+
112+
get '/hello' do
113+
# helpers available in your endpoint and filters
114+
say_hello(current_user)
115+
end
116+
end
117+
````
118+
119+
89120
## Working with Entities
90121

91122
A common problem in designing Ruby APIs is that you probably don't want

lib/grape/api.rb

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ def prefix(prefix = nil)
7979
# @example API with legacy support.
8080
# class MyAPI < Grape::API
8181
# version 'v2'
82-
#
82+
#
8383
# get '/main' do
8484
# {:some => 'data'}
8585
# end
86-
#
86+
#
8787
# version 'v1' do
8888
# get '/main' do
8989
# {:legacy => 'data'}
@@ -103,8 +103,8 @@ def version(*args, &block)
103103
end
104104
end
105105
end
106-
107-
# Specify the default format for the API's
106+
107+
# Specify the default format for the API's
108108
# serializers. Currently only `:json` is
109109
# supported.
110110
def default_format(new_format = nil)
@@ -180,7 +180,10 @@ def represent(model_class, options)
180180
#
181181
# When called without a block, all known helpers within this scope
182182
# are included.
183-
#
183+
#
184+
# @param mod [Module] optional module of methods to include
185+
# @param &block [Block] optional block of methods to include
186+
#
184187
# @example Define some helpers.
185188
# class ExampleAPI < Grape::API
186189
# helpers do
@@ -189,18 +192,18 @@ def represent(model_class, options)
189192
# end
190193
# end
191194
# end
192-
def helpers(&block)
193-
if block_given?
194-
m = settings.peek[:helpers] || Module.new
195-
m.class_eval &block
196-
set(:helpers, m)
195+
def helpers(mod = nil, &block)
196+
if block_given? || mod
197+
mod ||= settings.peek[:helpers] || Module.new
198+
mod.class_eval &block if block_given?
199+
set(:helpers, mod)
197200
else
198-
m = Module.new
201+
mod = Module.new
199202
settings.stack.each do |s|
200-
m.send :include, s[:helpers] if s[:helpers]
203+
mod.send :include, s[:helpers] if s[:helpers]
201204
end
202205
change!
203-
m
206+
mod
204207
end
205208
end
206209

@@ -267,7 +270,7 @@ def route(methods, paths = ['/'], route_options = {}, &block)
267270
def before(&block)
268271
imbue(:befores, [block])
269272
end
270-
273+
271274
def after(&block)
272275
imbue(:afters, [block])
273276
end
@@ -287,14 +290,14 @@ def namespace(space = nil, &block)
287290
Rack::Mount::Utils.normalize_path(settings.stack.map{|s| s[:namespace]}.join('/'))
288291
end
289292
end
290-
293+
291294
alias_method :group, :namespace
292295
alias_method :resource, :namespace
293296
alias_method :resources, :namespace
294297
alias_method :segment, :namespace
295-
298+
296299
# Create a scope without affecting the URL.
297-
#
300+
#
298301
# @param name [Symbol] Purely placebo, just allows to to name the scope to make the code more readable.
299302
def scope(name = nil, &block)
300303
nest(block)
@@ -321,13 +324,13 @@ def middleware
321324
def routes
322325
@routes ||= []
323326
end
324-
327+
325328
def versions
326329
@versions ||= []
327330
end
328-
331+
329332
protected
330-
333+
331334
# Execute first the provided block, then each of the
332335
# block passed in. Allows for simple 'before' setups
333336
# of settings stack pushes.
@@ -343,7 +346,7 @@ def nest(*blocks, &block)
343346
end
344347
end
345348

346-
def inherited(subclass)
349+
def inherited(subclass)
347350
subclass.reset!
348351
subclass.logger = logger.clone
349352
end

spec/grape/api_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,22 @@ def two
502502

503503
lambda{get '/howdy'}.should_not raise_error
504504
end
505+
506+
it 'should allow for modules' do
507+
mod = Module.new do
508+
def hello
509+
"Hello, world."
510+
end
511+
end
512+
subject.helpers mod
513+
514+
subject.get '/howdy' do
515+
hello
516+
end
517+
518+
get '/howdy'
519+
last_response.body.should eql 'Hello, world.'
520+
end
505521
end
506522

507523
describe '.scope' do

0 commit comments

Comments
 (0)