-
Notifications
You must be signed in to change notification settings - Fork 152
Fix the method Grape::Entity#exec_with_object to work with Ruby 3 #376
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
base: master
Are you sure you want to change the base?
Conversation
We already have Ruby 3.x in the CI. Are we missing tests? (Add part of this PR please)? Don't worry about versions, revert that. |
@@ -523,6 +523,7 @@ def exec_with_object(options, &block) | |||
# it handles: https://github.com/ruby/ruby/blob/v3_0_0_preview1/NEWS.md#language-changes point 3, Proc | |||
# accounting for expose :foo, &:bar | |||
if e.is_a?(ArgumentError) && block.parameters == [[:req], [:rest]] | |||
Rails.logger.error("***** ERROR in exec_with_object: #{e.message}\n#{e.backtrace.join("\n")}") |
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.
Not everyone uses Rails.
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.
This has a fix but also still has a Rails
mention, needs work.
After working through the #389 and seeing how our Right now: class FooObject
def method_without_args
'result'
end
end
class FooObjectEntity < Grape::Entity
# These two lines do exactly the same thing:
expose :boo, &:method_without_args
expose :method_without_args, as: :boo_again
end
foo = FooObject.new
FooObjectEntity.represent(foo).serializable_hash
# => { boo: "result", boo_again: "result" } In other words: expose :alias, &:method_name
# is identical to:
expose :method_name, as: :alias Because there’s no extra behavior or transformation happening, the shorthand doesn't buy us anything over the already-concise as: form. If our goal is in "expose a zero-argument method under a different key," we can do that directly. The usage of
Because of these points, the "nice little shorthand" is actually increasing our maintenance burden and confusing people more than helping them (in my subjective opinion). Another idea is to make class FooObject
def name = "name"
def surname = "surname"
end
class FooObjectEntity < Grape::Entity
# Treat `&:capitalize` as “call my own `capitalize` helper”
expose :name, &:capitalize
expose :surname, &:capitalize
# If `value` is the raw attribute value (e.g. "name"),
# `object` and `options` can be used, too.
def capitalize(value, object = nil, options = nil)
value.upcase
end
end
foo = FooObject.new
FooObjectEntity.represent(foo).serializable_hash
# => { name: "NAME", surname: "SURNAME" } In this proposal, wdyt? |
Fix the method Grape::Entity#exec_with_object to work with Ruby 3.