Skip to content
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
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Style/Send:
Enabled: true

Metrics/AbcSize:
Max: 50
Max: 80 # TODO: revert to 50 once the refactor of public api is done.
Copy link
Member

Choose a reason for hiding this comment

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

I just run rubocop -a ; rubocop --auto-gen-config.


Metrics/BlockLength:
Max: 30
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* [#2603](https://github.com/ruby-grape/grape/pull/2603): Remove `namespace_stackable_with_hash` from public interface and move to internal InheritableSetting - [@ericproulx](https://github.com/ericproulx).
* [#2604](https://github.com/ruby-grape/grape/pull/2604): Enable branch coverage - [@ericproulx](https://github.com/ericproulx).
* [#2605](https://github.com/ruby-grape/grape/pull/2605): Add Rack 3.2 support with new gemfile and CI integration - [@ericproulx](https://github.com/ericproulx).
* [#2607](https://github.com/ruby-grape/grape/pull/2607): Remove namespace_stackable and namespace_inheritable from public API - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

#### Fixes
Expand Down
49 changes: 10 additions & 39 deletions lib/grape/api/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,9 @@ def call!(env)

# (see #cascade?)
def cascade(value = nil)
if value.nil?
inheritable_setting.namespace_inheritable.key?(:cascade) ? !namespace_inheritable(:cascade).nil? : true
else
namespace_inheritable(:cascade, value)
end
return inheritable_setting.namespace_inheritable.key?(:cascade) ? !inheritable_setting.namespace_inheritable(:cascade).nil? : true if value.nil?

inheritable_setting.namespace_inheritable[:cascade] = value
end

def compile!
Expand All @@ -103,35 +101,6 @@ def recognize_path(path)

protected

# Execute first the provided block, then each of the
# block passed in. Allows for simple 'before' setups
# of settings stack pushes.
def nest(*blocks, &block)
blocks.compact!
if blocks.any?
evaluate_as_instance_with_configuration(block) if block
blocks.each { |b| evaluate_as_instance_with_configuration(b) }
reset_validations!
else
instance_eval(&block)
end
end

def evaluate_as_instance_with_configuration(block, lazy: false)
lazy_block = Grape::Util::Lazy::Block.new do |configuration|
value_for_configuration = configuration
self.configuration = value_for_configuration.evaluate if value_for_configuration.try(:lazy?)
response = instance_eval(&block)
self.configuration = value_for_configuration
response
end
if base && base_instance? && lazy
lazy_block
else
lazy_block.evaluate_from(configuration)
end
end

def inherited(subclass)
super
subclass.reset!
Expand Down Expand Up @@ -187,8 +156,9 @@ def call(env)
# errors from reaching upstream. This is effectivelly done by unsetting
# X-Cascade. Default :cascade is true.
def cascade?
return self.class.namespace_inheritable(:cascade) if self.class.inheritable_setting.namespace_inheritable.key?(:cascade)
return self.class.namespace_inheritable(:version_options)[:cascade] if self.class.namespace_inheritable(:version_options)&.key?(:cascade)
namespace_inheritable = self.class.inheritable_setting.namespace_inheritable
return namespace_inheritable[:cascade] if namespace_inheritable.key?(:cascade)
return namespace_inheritable[:version_options][:cascade] if namespace_inheritable[:version_options]&.key?(:cascade)

true
end
Expand Down Expand Up @@ -219,11 +189,12 @@ def collect_route_config_per_pattern(all_routes)
last_route = routes.last # Most of the configuration is taken from the last endpoint
next if routes.any? { |route| route.request_method == '*' }

namespace_inheritable = self.class.inheritable_setting.namespace_inheritable
allowed_methods = routes.map(&:request_method)
allowed_methods |= [Rack::HEAD] if !self.class.namespace_inheritable(:do_not_route_head) && allowed_methods.include?(Rack::GET)
allowed_methods |= [Rack::HEAD] if !namespace_inheritable[:do_not_route_head] && allowed_methods.include?(Rack::GET)

allow_header = self.class.namespace_inheritable(:do_not_route_options) ? allowed_methods : [Rack::OPTIONS] | allowed_methods
last_route.app.options[:options_route_enabled] = true unless self.class.namespace_inheritable(:do_not_route_options) || allowed_methods.include?(Rack::OPTIONS)
allow_header = namespace_inheritable[:do_not_route_options] ? allowed_methods : [Rack::OPTIONS] | allowed_methods
last_route.app.options[:options_route_enabled] = true unless namespace_inheritable[:do_not_route_options] || allowed_methods.include?(Rack::OPTIONS)

@router.associate_routes(last_route.pattern, {
endpoint: last_route.app,
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/dsl/callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Callbacks

%w[before before_validation after_validation after finally].each do |callback_method|
define_method callback_method.to_sym do |&block|
namespace_stackable(callback_method.pluralize.to_sym, block)
inheritable_setting.namespace_stackable[callback_method.pluralize.to_sym] = block
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/dsl/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def include_block(block)
def make_inclusion(mod, &block)
define_boolean_in_mod(mod)
inject_api_helpers_to_mod(mod, &block)
namespace_stackable(:helpers, mod)
inheritable_setting.namespace_stackable[:helpers] = mod
end

def include_all_in_scope
Expand Down Expand Up @@ -95,7 +95,7 @@ def api_changed(new_api)
def process_named_params
return if @named_params.blank?

api.namespace_stackable(:named_params, @named_params)
api.inheritable_setting.namespace_stackable[:named_params] = @named_params
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def declared(passed_params, options = {}, declared_params = nil, params_nested_p
declared_hash(passed_params, options, declared_params, params_nested_path)
end

if (key_maps = namespace_stackable(:contract_key_map))
if (key_maps = inheritable_setting.namespace_stackable[:contract_key_map])
key_maps.each { |key_map| key_map.write(passed_params, res) }
end

Expand Down Expand Up @@ -122,7 +122,7 @@ def optioned_declared_params(include_parent_namespaces)
inheritable_setting.route[:declared_params]
else
# Declared params at current namespace
namespace_stackable(:declared_params).last || []
inheritable_setting.namespace_stackable[:declared_params].last || []
end

raise ArgumentError, 'Tried to filter for declared parameters but none exist.' unless declared_params
Expand Down Expand Up @@ -164,7 +164,7 @@ def configuration
# @param backtrace [Array<String>] The backtrace of the exception that caused the error.
# @param original_exception [Exception] The original exception that caused the error.
def error!(message, status = nil, additional_headers = nil, backtrace = nil, original_exception = nil)
status = self.status(status || namespace_inheritable(:default_error_status))
status = self.status(status || inheritable_setting.namespace_inheritable[:default_error_status])
headers = additional_headers.present? ? header.merge(additional_headers) : header
throw :error,
message: message,
Expand Down
6 changes: 3 additions & 3 deletions lib/grape/dsl/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ def use(middleware_class, *args, &block)
arr = [:use, middleware_class, *args]
arr << block if block

namespace_stackable(:middleware, arr)
inheritable_setting.namespace_stackable[:middleware] = arr
end

%i[insert insert_before insert_after].each do |method_name|
define_method method_name do |*args, &block|
arr = [method_name, *args]
arr << block if block

namespace_stackable(:middleware, arr)
inheritable_setting.namespace_stackable[:middleware] = arr
end
end

# Retrieve an array of the middleware classes
# and arguments that are currently applied to the
# application.
def middleware
namespace_stackable(:middleware) || []
inheritable_setting.namespace_stackable[:middleware] || []
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/dsl/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module Parameters
# end
# end
def build_with(build_with)
@api.namespace_inheritable(:build_params_with, build_with)
@api.inheritable_setting.namespace_inheritable[:build_params_with] = build_with
end

# Include reusable params rules among current.
Expand Down
42 changes: 23 additions & 19 deletions lib/grape/dsl/request_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,42 @@ module RequestResponse
# Specify the default format for the API's serializers.
# May be `:json` or `:txt` (default).
def default_format(new_format = nil)
namespace_inheritable(:default_format, new_format&.to_sym)
return inheritable_setting.namespace_inheritable[:default_format] if new_format.nil?

inheritable_setting.namespace_inheritable[:default_format] = new_format.to_sym
end

# Specify the format for the API's serializers.
# May be `:json`, `:xml`, `:txt`, etc.
def format(new_format = nil)
return namespace_inheritable(:format) unless new_format
return inheritable_setting.namespace_inheritable[:format] if new_format.nil?

symbolic_new_format = new_format.to_sym
namespace_inheritable(:format, symbolic_new_format)
namespace_inheritable(:default_error_formatter, Grape::ErrorFormatter.formatter_for(symbolic_new_format))
inheritable_setting.namespace_inheritable[:format] = symbolic_new_format
inheritable_setting.namespace_inheritable[:default_error_formatter] = Grape::ErrorFormatter.formatter_for(symbolic_new_format)

content_type = content_types[symbolic_new_format]
raise Grape::Exceptions::MissingMimeType.new(new_format) unless content_type

namespace_stackable(:content_types, symbolic_new_format => content_type)
inheritable_setting.namespace_stackable[:content_types] = { symbolic_new_format => content_type }
end

# Specify a custom formatter for a content-type.
def formatter(content_type, new_formatter)
namespace_stackable(:formatters, content_type.to_sym => new_formatter)
inheritable_setting.namespace_stackable[:formatters] = { content_type.to_sym => new_formatter }
end

# Specify a custom parser for a content-type.
def parser(content_type, new_parser)
namespace_stackable(:parsers, content_type.to_sym => new_parser)
inheritable_setting.namespace_stackable[:parsers] = { content_type.to_sym => new_parser }
end

# Specify a default error formatter.
def default_error_formatter(new_formatter_name = nil)
return namespace_inheritable(:default_error_formatter) unless new_formatter_name
return inheritable_setting.namespace_inheritable[:default_error_formatter] if new_formatter_name.nil?

new_formatter = Grape::ErrorFormatter.formatter_for(new_formatter_name)
namespace_inheritable(:default_error_formatter, new_formatter)
inheritable_setting.namespace_inheritable[:default_error_formatter] = new_formatter
end

def error_formatter(format, options)
Expand All @@ -49,13 +51,13 @@ def error_formatter(format, options)
options
end

namespace_stackable(:error_formatters, format.to_sym => formatter)
inheritable_setting.namespace_stackable[:error_formatters] = { format.to_sym => formatter }
end

# Specify additional content-types, e.g.:
# content_type :xls, 'application/vnd.ms-excel'
def content_type(key, val)
namespace_stackable(:content_types, key.to_sym => val)
inheritable_setting.namespace_stackable[:content_types] = { key.to_sym => val }
end

# All available content types.
Expand All @@ -66,7 +68,9 @@ def content_types

# Specify the default status code for errors.
def default_error_status(new_status = nil)
namespace_inheritable(:default_error_status, new_status)
return inheritable_setting.namespace_inheritable[:default_error_status] if new_status.nil?

inheritable_setting.namespace_inheritable[:default_error_status] = new_status
end

# Allows you to rescue certain exceptions that occur to return
Expand Down Expand Up @@ -102,12 +106,12 @@ def rescue_from(*args, &block)
handler ||= extract_with(options)

if args.include?(:all)
namespace_inheritable(:rescue_all, true)
namespace_inheritable(:all_rescue_handler, handler)
inheritable_setting.namespace_inheritable[:rescue_all] = true
inheritable_setting.namespace_inheritable[:all_rescue_handler] = handler
elsif args.include?(:grape_exceptions)
namespace_inheritable(:rescue_all, true)
namespace_inheritable(:rescue_grape_exceptions, true)
namespace_inheritable(:grape_exceptions_rescue_handler, handler)
inheritable_setting.namespace_inheritable[:rescue_all] = true
inheritable_setting.namespace_inheritable[:rescue_grape_exceptions] = true
inheritable_setting.namespace_inheritable[:grape_exceptions_rescue_handler] = handler
else
handler_type =
case options[:rescue_subclasses]
Expand All @@ -120,7 +124,7 @@ def rescue_from(*args, &block)
inheritable_setting.namespace_reverse_stackable[handler_type] = args.to_h { |arg| [arg, handler] }
end

namespace_stackable(:rescue_options, options)
inheritable_setting.namespace_stackable[:rescue_options] = options
end

# Allows you to specify a default representation entity for a
Expand All @@ -146,7 +150,7 @@ def rescue_from(*args, &block)
def represent(model_class, options)
raise Grape::Exceptions::InvalidWithOptionForRepresent.new unless options[:with].is_a?(Class)

namespace_stackable(:representations, model_class => options[:with])
inheritable_setting.namespace_stackable[:representations] = { model_class => options[:with] }
end

private
Expand Down
Loading