Skip to content
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

Add possibility to define reusable params. #550

Merged
merged 1 commit into from
Jan 7, 2014
Merged

Add possibility to define reusable params. #550

merged 1 commit into from
Jan 7, 2014

Conversation

dm1try
Copy link
Member

@dm1try dm1try commented Jan 4, 2014

Another one concept :)

Examples:

helpers do
  params :pagination do
    optional :page, type: Integer
    optional :per_page, type: Integer   
  end
end

desc "Get collection"
params do
  use :pagination
end

using module:

# shared_params.rb
module SharedParams
  extend Grape::ApiHelpers

  params :period do
    optional :start_date
    optional :end_date
  end

  params :pagination do
    optional :page, type: Integer
    optional :per_page, type: Integer   
  end
end
helpers SharedParams

desc "Get collection"
params do
  use :pagination, :period
end

@dm1try
Copy link
Member Author

dm1try commented Jan 4, 2014

@dblock what do you think ?

@dblock
Copy link
Member

dblock commented Jan 5, 2014

(edited)

I like the part where you can define params within helpers, that's perfect.

I am a bit worried that ApiHelpers is going to be getting more and more functionality that copies Api. So I wonder whether this means we should be doing some serious refactoring to avoid duplicating things? Or a more generic meta-programming idea could be that ApiHelpers takes anything via method_missing, remembers it and invokes it in the context of the API?

@dblock
Copy link
Member

dblock commented Jan 5, 2014

@dm1try Btw, whichever way we go, this is a very elegant approach - you, sir, are a Ruby 師匠

@dm1try
Copy link
Member Author

dm1try commented Jan 5, 2014

@dblock , thanks for feedback!
Ok, lets talk about more generic way, we can try to use ApiHelpers like some proxy to API with allowed methods:

module ApiHelpers
  ALLOWED_METHODS = [:params]
  def method_missing(name, *args, &block)
    ALLOWED_METHODS.include?(name) ? save_lazy_methods : super
  end

  def api_changed(new_api)
    call_lazy_methods
  end
end

The problem is that we should extend API with methods which can be used only in helpers, but now we can do some confusing things:

helpers do
  params do
  end
end

desc 'Get'
params :params do
end

And the main problem is that helpers are used to extend Endpoint on the fly, and maybe it's not worth to mix them with API helpers, and we will have more loosely coupled components 👏

What do the community think? Should we continue?)

@dm1try
Copy link
Member Author

dm1try commented Jan 5, 2014

About "serious refactoring",
I don't know how to do this without breaking compatibility, for now.

@dblock
Copy link
Member

dblock commented Jan 6, 2014

Ok, I think I agree with you, ApiHelpers is fine.

Do you think this should be defined as an inner class of Api? Like Api::Helpers?

Add README and CHANGELOG and I am happy to merge this.

@dm1try
Copy link
Member Author

dm1try commented Jan 6, 2014

@dblock hey, thanks for suggestion. I put API::Helpers at the bottom of API as inner module. Added some README.

Can you take a look?

@@ -14,6 +14,7 @@ Next Release
* [#540](https://github.com/intridea/grape/pull/540): Ruby 2.1.0 is now supported - [@salimane](https://github.com/salimane).
* [#544](https://github.com/intridea/grape/pull/544): The `rescue_from` keyword now handles subclasses of exceptions by default - [@xevix](https://github.com/xevix).
* [#545](https://github.com/intridea/grape/pull/545): Added `type` (Array or Hash) support to `requires`, `optional` and `group` - [@bwalex](https://github.com/bwalex).
* [#550](https://github.com/intridea/grape/pull/550): Added possibility to define reusable params. - [@dm1try](https://github.com/dm1try).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The extra dot after params is driving me crazy :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch :)

@dblock
Copy link
Member

dblock commented Jan 7, 2014

Perfect, merging.

dblock added a commit that referenced this pull request Jan 7, 2014
Add possibility to define reusable params.
@dblock dblock merged commit b760958 into ruby-grape:master Jan 7, 2014
@dm1try
Copy link
Member Author

dm1try commented Jan 7, 2014

Great ❤️

@dm1try dm1try deleted the named_params branch January 7, 2014 05:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants