Skip to content

Commit

Permalink
Refactor registry
Browse files Browse the repository at this point in the history
  • Loading branch information
ericproulx committed Dec 23, 2024
1 parent 4478463 commit 7d0f366
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 28 deletions.
4 changes: 1 addition & 3 deletions lib/grape/error_formatter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ def format_structured_message(_structured_message)

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

ErrorFormatter.register(klass.name.demodulize.underscore, klass)
ErrorFormatter.register(klass)
end
end
end
Expand Down
4 changes: 1 addition & 3 deletions lib/grape/formatter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ def self.call(_object, _env)

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

Formatter.register(klass.name.demodulize.underscore, klass)
Formatter.register(klass)
end
end
end
Expand Down
5 changes: 1 addition & 4 deletions lib/grape/middleware/versioner/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ class Base < Grape::Middleware::Base

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

short_name = klass.name.demodulize.underscore
Versioner.register(short_name, klass)
Versioner.register(klass)
end

def default_options
Expand Down
5 changes: 1 addition & 4 deletions lib/grape/parser/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ def self.call(_object, _env)

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

short_name = klass.name.demodulize.underscore
Parser.register(short_name, klass)
Parser.register(klass)
end
end
end
Expand Down
11 changes: 10 additions & 1 deletion lib/grape/util/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
module Grape
module Util
module Registry
def register(short_name, klass)
def register(klass)
short_name = build_short_name(klass)
return if short_name.nil?

warn "#{short_name} is already registered with class #{klass}" if registry.key?(short_name)
registry[short_name] = klass
end

private

def build_short_name(klass)
return if klass.name.blank?

klass.name.demodulize.underscore
end

def registry
@registry ||= {}.with_indifferent_access
end
Expand Down
6 changes: 6 additions & 0 deletions lib/grape/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,11 @@ def require_validator(short_name)

registry[short_name]
end

def build_short_name(klass)
return if klass.name.blank?

klass.name.demodulize.underscore.delete_suffix('_validator')
end
end
end
2 changes: 1 addition & 1 deletion lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ def derive_validator_options(validations)
def validates_presence(validations, attrs, doc, opts)
return unless validations.key?(:presence) && validations[:presence]

validate(:presence, validations.delete(:presence), attrs, doc, opts)
validate('presence', validations.delete(:presence), attrs, doc, opts)
validations.delete(:message) if validations.key?(:message)
end
end
Expand Down
5 changes: 1 addition & 4 deletions lib/grape/validations/validators/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ def validate!(params)

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

short_validator_name = klass.name.demodulize.underscore.delete_suffix('_validator')
Validations.register(short_validator_name, klass)
Validations.register(klass)
end

def message(default_key = nil)
Expand Down
10 changes: 5 additions & 5 deletions spec/grape/api/custom_validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def validate_param!(attr_name, params)

before do
stub_const('DefaultLengthValidator', default_length_validator)
described_class.register(:default_length, DefaultLengthValidator)
described_class.register(DefaultLengthValidator)
end

after do
Expand Down Expand Up @@ -86,7 +86,7 @@ def validate(request)

before do
stub_const('InBodyValidator', in_body_validator)
described_class.register(:in_body, InBodyValidator)
described_class.register(InBodyValidator)
end

after do
Expand Down Expand Up @@ -129,7 +129,7 @@ def validate_param!(attr_name, _params)

before do
stub_const('WithMessageKeyValidator', message_key_validator)
described_class.register(:with_message_key, WithMessageKeyValidator)
described_class.register(WithMessageKeyValidator)
end

after do
Expand Down Expand Up @@ -182,7 +182,7 @@ def access_header

before do
stub_const('AdminValidator', admin_validator)
described_class.register(:admin, AdminValidator)
described_class.register(AdminValidator)
end

after do
Expand Down Expand Up @@ -248,7 +248,7 @@ def validate_param!(_attr_name, _params)

before do
stub_const('InstanceValidatorValidator', validator_type)
described_class.register(:instance_validator, InstanceValidatorValidator)
described_class.register(InstanceValidatorValidator)
end

after do
Expand Down
6 changes: 3 additions & 3 deletions spec/grape/validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ def validate_param!(attr_name, params)

before do
stub_const('DateRangeValidator', date_range_validator)
described_class.register(:date_range, DateRangeValidator)
described_class.register(DateRangeValidator)
subject.params do
optional :date_range, date_range: true, type: Hash do
requires :from, type: Integer
Expand Down Expand Up @@ -1193,7 +1193,7 @@ def validate_param!(attr_name, params)

before do
stub_const('CustomvalidatorValidator', custom_validator)
described_class.register(:customvalidator, CustomvalidatorValidator)
described_class.register(CustomvalidatorValidator)
end

after do
Expand Down Expand Up @@ -1351,7 +1351,7 @@ def validate_param!(attr_name, params)

before do
stub_const('CustomvalidatorWithOptionsValidator', custom_validator_with_options)
described_class.register(:customvalidator_with_options, CustomvalidatorWithOptionsValidator)
described_class.register(CustomvalidatorWithOptionsValidator)
subject.params do
optional :custom, customvalidator_with_options: { text: 'im custom with options', message: 'is not custom with options!' }
end
Expand Down

0 comments on commit 7d0f366

Please sign in to comment.