-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allow stubbing of helper methods #398
Changes from 4 commits
f28e60d
29c0b5b
eae467d
ec8e987
897313f
a13c729
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,7 @@ def initialize(settings, options = {}, &block) | |
@block = self.class.generate_api_method(method_name, &block) | ||
end | ||
@options = options | ||
@klass = options[:klass] | ||
|
||
raise Grape::Exceptions::MissingOption.new(:path) unless options.key?(:path) | ||
options[:path] = Array(options[:path]) | ||
|
@@ -358,12 +359,25 @@ def endpoints | |
end | ||
end | ||
|
||
def helper | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be |
||
@klass.helper if @klass | ||
end | ||
|
||
def method_missing(method_name, *args, &block) | ||
if helper.respond_to?(method_name) | ||
helper.set_context(params, request, env) | ||
helper.send(method_name, *args, &block) | ||
else | ||
super | ||
end | ||
end | ||
|
||
def run(env) | ||
@env = env | ||
@header = {} | ||
@request = Grape::Request.new(@env) | ||
|
||
self.extend helpers | ||
#self.extend helpers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't keep comments around, delete. |
||
cookies.read(@request) | ||
|
||
run_filters befores | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Grape | ||
class HelperObject | ||
|
||
def set_context(params, request, env) | ||
reset_context | ||
@params = params | ||
@request = request | ||
@env = env | ||
end | ||
|
||
def reset_context | ||
instance_variables.each do |name| | ||
next if name.to_s == "@mock_proxy" | ||
instance_variable_set(name, nil) | ||
end | ||
end | ||
|
||
attr_reader :params, :request, :env | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: I am not in love with the name. Maybe |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Grape | ||
VERSION = '0.4.2' | ||
VERSION = '0.4.3' | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor:
describe
parts and therequire
, just to get exactly to the point of how to stub something.