Skip to content

Validations Autoload #2207

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

Merged
merged 4 commits into from
Dec 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* [#2200](https://github.com/ruby-grape/grape/pull/2200): Add validators module to all validators - [@ericproulx](https://github.com/ericproulx).
* [#2202](https://github.com/ruby-grape/grape/pull/2202): Fix random mock spec error - [@ericproulx](https://github.com/ericproulx).
* [#2203](https://github.com/ruby-grape/grape/pull/2203): Add rubocop-rspec - [@ericproulx](https://github.com/ericproulx).
* [#2207](https://github.com/ruby-grape/grape/pull/2207): Autoload Validations/Validators - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

### 1.6.0 (2021/10/04)
Expand Down
62 changes: 38 additions & 24 deletions lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
require 'active_support/version'
require 'active_support/core_ext/hash/indifferent_access'
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/array/conversions'
require 'active_support/core_ext/array/extract_options'
require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/array/conversions'
require 'active_support/core_ext/hash/conversions'
require 'active_support/core_ext/hash/deep_merge'
require 'active_support/core_ext/hash/reverse_merge'
require 'active_support/core_ext/hash/except'
require 'active_support/core_ext/hash/reverse_merge'
require 'active_support/core_ext/hash/slice'
require 'active_support/core_ext/hash/conversions'
require 'active_support/dependencies/autoload'
require 'active_support/notifications'
require 'i18n'
Expand Down Expand Up @@ -216,6 +216,41 @@ module ServeStream
autoload :StreamResponse
end
end

module Validations
extend ::ActiveSupport::Autoload

eager_autoload do
autoload :AttributesIterator
autoload :MultipleAttributesIterator
autoload :SingleAttributeIterator
autoload :ParamsScope
autoload :Types
autoload :ValidatorFactory
end

module Validators
extend ::ActiveSupport::Autoload

eager_autoload do
autoload :Base
autoload :MultipleParamsBase
autoload :AllOrNoneOfValidator
autoload :AllowBlankValidator
autoload :AsValidator
autoload :AtLeastOneOfValidator
autoload :CoerceValidator
autoload :DefaultValidator
autoload :ExactlyOneOfValidator
autoload :ExceptValuesValidator
autoload :MutualExclusionValidator
autoload :PresenceValidator
autoload :RegexpValidator
autoload :SameAsValidator
autoload :ValuesValidator
end
end
end
end

require 'grape/config'
Expand All @@ -225,25 +260,4 @@ module ServeStream
require 'grape/util/lazy_block'
require 'grape/util/endpoint_configuration'

require 'grape/validations/validators/base'
require 'grape/validations/attributes_iterator'
require 'grape/validations/single_attribute_iterator'
require 'grape/validations/multiple_attributes_iterator'
require 'grape/validations/validators/allow_blank'
require 'grape/validations/validators/as'
require 'grape/validations/validators/at_least_one_of'
require 'grape/validations/validators/coerce'
require 'grape/validations/validators/default'
require 'grape/validations/validators/exactly_one_of'
require 'grape/validations/validators/mutual_exclusion'
require 'grape/validations/validators/presence'
require 'grape/validations/validators/regexp'
require 'grape/validations/validators/same_as'
require 'grape/validations/validators/values'
require 'grape/validations/validators/except_values'
require 'grape/validations/params_scope'
require 'grape/validations/validators/all_or_none'
require 'grape/validations/types'
require 'grape/validations/validator_factory'

require 'grape/version'
2 changes: 1 addition & 1 deletion lib/grape/dsl/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def include_all_in_scope
def define_boolean_in_mod(mod)
return if defined? mod::Boolean

mod.const_set('Boolean', Grape::API::Boolean)
mod.const_set(:Boolean, Grape::API::Boolean)
end

def inject_api_helpers_to_mod(mod, &block)
Expand Down
2 changes: 2 additions & 0 deletions lib/grape/util/json.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'json'

module Grape
if Object.const_defined? :MultiJson
Json = ::MultiJson
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/util/strict_hash_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def self.nested_settings_methods(setting_name, new_config_class)
end
end

define_method 'to_hash' do
define_method :to_hash do
merge_hash = {}
setting_name.each_key { |k| merge_hash[k] = send("#{k}_context").to_hash }

Expand Down
6 changes: 0 additions & 6 deletions lib/grape/validations.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
# frozen_string_literal: true

require 'grape/validations/attributes_iterator'
require 'grape/validations/single_attribute_iterator'
require 'grape/validations/multiple_attributes_iterator'
require 'grape/validations/params_scope'
require 'grape/validations/types'

module Grape
# Registry to store and locate known Validators.
module Validations
Expand Down
2 changes: 0 additions & 2 deletions lib/grape/validations/types/json.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'json'

module Grape
module Validations
module Types
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'grape/validations/validators/multiple_params_base'

module Grape
module Validations
module Validators
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'grape/validations/validators/multiple_params_base'

module Grape
module Validations
module Validators
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'grape/validations/validators/multiple_params_base'

module Grape
module Validations
module Validators
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'grape/validations/validators/multiple_params_base'

module Grape
module Validations
module Validators
Expand Down