Skip to content

Commit 9feefdf

Browse files
authored
Merge pull request #1961 from ruby-grape/revert-1953-chore/micto-optimization-3
Revert "delete a reversible stackable values class"
2 parents 3e9c32d + 54e823b commit 9feefdf

16 files changed

+238
-39
lines changed

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#### Features
44

55
* Your contribution here.
6-
* [#1953](https://github.com/ruby-grape/grape/pull/1953): Delete a reversible stackable values class - [@dnesteryuk](https://github.com/dnesteryuk).
76
* [#1949](https://github.com/ruby-grape/grape/pull/1949): Add support for Ruby 2.7 - [@nbulaj](https://github.com/nbulaj).
87
* [#1948](https://github.com/ruby-grape/grape/pull/1948): Relax `dry-types` dependency version - [@nbulaj](https://github.com/nbulaj).
98
* [#1944](https://github.com/ruby-grape/grape/pull/1944): Reduces `attribute_translator` string allocations - [@ericproulx](https://github.com/ericproulx).

lib/grape.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ module Util
140140
eager_autoload do
141141
autoload :InheritableValues
142142
autoload :StackableValues
143+
autoload :ReverseStackableValues
143144
autoload :InheritableSetting
144145
autoload :StrictHashConfiguration
145146
autoload :Registrable

lib/grape/dsl/request_response.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def rescue_from(*args, &block)
127127
:base_only_rescue_handlers
128128
end
129129

130-
namespace_stackable handler_type, Hash[args.map { |arg| [arg, handler] }]
130+
namespace_reverse_stackable handler_type, Hash[args.map { |arg| [arg, handler] }]
131131
end
132132

133133
namespace_stackable(:rescue_options, options)

lib/grape/dsl/settings.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,21 @@ def namespace_stackable(key, value = nil)
9696
get_or_set :namespace_stackable, key, value
9797
end
9898

99+
def namespace_reverse_stackable(key, value = nil)
100+
get_or_set :namespace_reverse_stackable, key, value
101+
end
102+
99103
def namespace_stackable_with_hash(key)
100104
settings = get_or_set :namespace_stackable, key, nil
101105
return if settings.blank?
102106
settings.each_with_object({}) { |value, result| result.deep_merge!(value) }
103107
end
104108

105109
def namespace_reverse_stackable_with_hash(key)
106-
settings = get_or_set :namespace_stackable, key, nil
110+
settings = get_or_set :namespace_reverse_stackable, key, nil
107111
return if settings.blank?
108-
109112
result = {}
110-
settings.reverse_each do |setting|
113+
settings.each do |setting|
111114
setting.each do |field, value|
112115
result[field] ||= value
113116
end
@@ -168,11 +171,7 @@ def within_namespace(&_block)
168171
# the superclass's :inheritable_setting.
169172
def build_top_level_setting
170173
Grape::Util::InheritableSetting.new.tap do |setting|
171-
# Doesn't try to inherit settings from +Grape::API::Instance+ which also responds to
172-
# +inheritable_setting+, however, it doesn't contain any user-defined settings.
173-
# Otherwise, it would lead to an extra instance of +Grape::Util::InheritableSetting+
174-
# in the chain for every endpoint.
175-
if defined?(superclass) && superclass.respond_to?(:inheritable_setting) && superclass != Grape::API::Instance
174+
if defined?(superclass) && superclass.respond_to?(:inheritable_setting) && superclass != Grape::API
176175
setting.inherit_from superclass.inheritable_setting
177176
end
178177
end

lib/grape/endpoint.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,24 @@ def run_filters(filters, type = :other)
386386
extend post_extension if post_extension
387387
end
388388

389-
%i[befores before_validations after_validations afters finallies].each do |meth_id|
390-
define_method(meth_id) { namespace_stackable(meth_id) }
389+
def befores
390+
namespace_stackable(:befores) || []
391+
end
392+
393+
def before_validations
394+
namespace_stackable(:before_validations) || []
395+
end
396+
397+
def after_validations
398+
namespace_stackable(:after_validations) || []
399+
end
400+
401+
def afters
402+
namespace_stackable(:afters) || []
403+
end
404+
405+
def finallies
406+
namespace_stackable(:finallies) || []
391407
end
392408

393409
def validations

lib/grape/util/inheritable_setting.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Util
66
# and inheritable values (see InheritableValues and StackableValues).
77
class InheritableSetting
88
attr_accessor :route, :api_class, :namespace
9-
attr_accessor :namespace_inheritable, :namespace_stackable
9+
attr_accessor :namespace_inheritable, :namespace_stackable, :namespace_reverse_stackable
1010
attr_accessor :parent, :point_in_time_copies
1111

1212
# Retrieve global settings.
@@ -31,6 +31,7 @@ def initialize
3131
# used with a mount, or should every API::Class be a separate namespace by default?
3232
self.namespace_inheritable = InheritableValues.new
3333
self.namespace_stackable = StackableValues.new
34+
self.namespace_reverse_stackable = ReverseStackableValues.new
3435

3536
self.point_in_time_copies = []
3637

@@ -53,6 +54,7 @@ def inherit_from(parent)
5354

5455
namespace_inheritable.inherited_values = parent.namespace_inheritable
5556
namespace_stackable.inherited_values = parent.namespace_stackable
57+
namespace_reverse_stackable.inherited_values = parent.namespace_reverse_stackable
5658
self.route = parent.route.merge(route)
5759

5860
point_in_time_copies.map { |cloned_one| cloned_one.inherit_from parent }
@@ -70,6 +72,7 @@ def point_in_time_copy
7072
new_setting.namespace = namespace.clone
7173
new_setting.namespace_inheritable = namespace_inheritable.clone
7274
new_setting.namespace_stackable = namespace_stackable.clone
75+
new_setting.namespace_reverse_stackable = namespace_reverse_stackable.clone
7376
new_setting.route = route.clone
7477
new_setting.api_class = api_class
7578

@@ -90,7 +93,8 @@ def to_hash
9093
route: route.clone,
9194
namespace: namespace.to_hash,
9295
namespace_inheritable: namespace_inheritable.to_hash,
93-
namespace_stackable: namespace_stackable.to_hash
96+
namespace_stackable: namespace_stackable.to_hash,
97+
namespace_reverse_stackable: namespace_reverse_stackable.to_hash
9498
}
9599
end
96100
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
require_relative 'stackable_values'
4+
5+
module Grape
6+
module Util
7+
class ReverseStackableValues < StackableValues
8+
protected
9+
10+
def concat_values(inherited_value, new_value)
11+
[].tap do |value|
12+
value.concat(new_value)
13+
value.concat(inherited_value)
14+
end
15+
end
16+
end
17+
end
18+
end

lib/grape/util/stackable_values.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ def initialize(*_args)
1313
@frozen_values = {}
1414
end
1515

16-
# Even if there is no value, an empty array will be returned.
1716
def [](name)
1817
return @frozen_values[name] if @frozen_values.key? name
1918

lib/grape/validations/attributes_iterator.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ def initialize(validator, scope, params)
1111
@scope = scope
1212
@attrs = validator.attrs
1313
@original_params = scope.params(params)
14-
@array_given = @original_params.is_a?(Array)
1514
@params = Array.wrap(@original_params)
1615
end
1716

@@ -31,7 +30,7 @@ def do_each(params_to_process, parent_indicies = [], &block)
3130
end
3231

3332
if @scope.type == Array
34-
next unless @array_given # do not validate content of array if it isn't array
33+
next unless @original_params.is_a?(Array) # do not validate content of array if it isn't array
3534

3635
# fill current and parent scopes with correct array indicies
3736
parent_scope = @scope.parent

lib/grape/validations/params_scope.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,8 @@ def configuration
4545
# @return [Boolean] whether or not this entire scope needs to be
4646
# validated
4747
def should_validate?(parameters)
48-
scoped_params = params(parameters)
49-
50-
return false if @optional && (scoped_params.blank? || all_element_blank?(scoped_params))
51-
return false unless meets_dependency?(scoped_params, parameters)
48+
return false if @optional && (params(parameters).blank? || all_element_blank?(parameters))
49+
return false unless meets_dependency?(params(parameters), parameters)
5250
return true if parent.nil?
5351
parent.should_validate?(parameters)
5452
end
@@ -448,8 +446,8 @@ def options_key?(type, key, validations)
448446
validations[type].respond_to?(:key?) && validations[type].key?(key) && !validations[type][key].nil?
449447
end
450448

451-
def all_element_blank?(scoped_params)
452-
scoped_params.respond_to?(:all?) && scoped_params.all?(&:blank?)
449+
def all_element_blank?(parameters)
450+
params(parameters).respond_to?(:all?) && params(parameters).all?(&:blank?)
453451
end
454452

455453
# Validators don't have access to each other and they don't need, however,

0 commit comments

Comments
 (0)