Skip to content

Commit

Permalink
ActiveSupport inflector methods (#2330)
Browse files Browse the repository at this point in the history
* Replace convert_to_short_name by ActiveSupport inflector methods

* Use delete_suffix!

* Changelog entry
  • Loading branch information
ericproulx committed May 19, 2023
1 parent 96e2faf commit 82f3497
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* [#2326](https://github.com/ruby-grape/grape/pull/2326): Use ActiveSupport extensions - [@ericproulx](https://github.com/ericproulx).
* [#2327](https://github.com/ruby-grape/grape/pull/2327): Use ActiveSupport deprecation - [@ericproulx](https://github.com/ericproulx).
* [#2330](https://github.com/ruby-grape/grape/pull/2330): Use ActiveSupport inflector - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

#### Fixes
Expand Down
1 change: 1 addition & 0 deletions lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
require 'active_support/core_ext/object/duplicable'
require 'active_support/dependencies/autoload'
require 'active_support/deprecation'
require 'active_support/inflector'
require 'active_support/notifications'
require 'i18n'

Expand Down
12 changes: 2 additions & 10 deletions lib/grape/validations/validators/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,11 @@ def validate!(params)
raise Grape::Exceptions::ValidationArrayErrors.new(array_errors) if array_errors.any?
end

def self.convert_to_short_name(klass)
ret = klass.name.gsub(/::/, '/')
ret.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
ret.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
ret.tr!('-', '_')
ret.downcase!
File.basename(ret, '_validator')
end

def self.inherited(klass)
return if klass.name.blank?

Validations.register_validator(convert_to_short_name(klass), klass)
short_validator_name = klass.name.demodulize.underscore.delete_suffix!('_validator')
Validations.register_validator(short_validator_name, klass)
end

def message(default_key = nil)
Expand Down

0 comments on commit 82f3497

Please sign in to comment.