Skip to content

Commit 280e5b3

Browse files
authored
Use Active support functions (#2326)
* Replace deep_mergeable_hash.rb and deep_symbolize_hash.rb by ActiveSupport functions Use each_with_object instead of each/inject etc ... * Replace if env[...] by env.key? Use deep_dup instead of dup for rack_params * Use presence instead of ternary Replace unless present? by if blank? * Replace !include? by exclude? * Use ActiveSupport duplicable? * Fix rubocop and update CHANGELOG * Add minimal version for ActiveSupport >=5 * Remove extra * in CHANGELOG * Next version 1.8.0
1 parent ccb13ce commit 280e5b3

28 files changed

+63
-183
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
### 1.7.2 (Next)
1+
### 1.8.0 (Next)
22

33
#### Features
44

5+
* [#2326](https://github.com/ruby-grape/grape/pull/2326): Use ActiveSupport extensions - [@ericproulx](https://github.com/ericproulx).
56
* Your contribution here.
67

78
#### Fixes

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ content negotiation, versioning and much more.
159159

160160
## Stable Release
161161

162-
You're reading the documentation for the next release of Grape, which should be **1.7.2**.
162+
You're reading the documentation for the next release of Grape, which should be **1.8.0**.
163163
Please read [UPGRADING](UPGRADING.md) when upgrading from a previous version.
164164
The current stable release is [1.7.1](https://github.com/ruby-grape/grape/blob/v1.7.1/README.md).
165165

grape.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Gem::Specification.new do |s|
2020
'source_code_uri' => "https://github.com/ruby-grape/grape/tree/v#{s.version}"
2121
}
2222

23-
s.add_runtime_dependency 'activesupport'
23+
s.add_runtime_dependency 'activesupport', '>= 5'
2424
s.add_runtime_dependency 'builder'
2525
s.add_runtime_dependency 'dry-types', '>= 1.1'
2626
s.add_runtime_dependency 'mustermann-grape', '~> 1.0.0'

lib/grape.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
require 'active_support/core_ext/hash/deep_merge'
2121
require 'active_support/core_ext/hash/except'
2222
require 'active_support/core_ext/hash/indifferent_access'
23+
require 'active_support/core_ext/hash/keys'
2324
require 'active_support/core_ext/hash/reverse_merge'
2425
require 'active_support/core_ext/hash/slice'
2526
require 'active_support/core_ext/object/blank'
27+
require 'active_support/core_ext/object/duplicable'
2628
require 'active_support/dependencies/autoload'
2729
require 'active_support/notifications'
2830
require 'i18n'
@@ -92,8 +94,6 @@ module Exceptions
9294
module Extensions
9395
extend ::ActiveSupport::Autoload
9496
eager_autoload do
95-
autoload :DeepMergeableHash
96-
autoload :DeepSymbolizeHash
9797
autoload :Hash
9898
end
9999
module ActiveSupport

lib/grape/content_types.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,11 @@ module ContentTypes
1717

1818
class << self
1919
def content_types_for_settings(settings)
20-
return if settings.blank?
21-
22-
settings.each_with_object({}) { |value, result| result.merge!(value) }
20+
settings&.inject(:merge!)
2321
end
2422

2523
def content_types_for(from_settings)
26-
if from_settings.present?
27-
from_settings
28-
else
29-
Grape::ContentTypes::CONTENT_TYPES.merge(default_elements)
30-
end
24+
from_settings.presence || Grape::ContentTypes::CONTENT_TYPES.merge(default_elements)
3125
end
3226
end
3327
end

lib/grape/dsl/inside_route.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def handle_passed_param(params_nested_path, has_passed_children = false, &_block
103103

104104
if type == 'Hash' && !has_children
105105
{}
106-
elsif type == 'Array' || (type&.start_with?('[') && !type&.include?(','))
106+
elsif type == 'Array' || (type&.start_with?('[') && type&.exclude?(','))
107107
[]
108108
elsif type == 'Set' || type&.start_with?('#<Set')
109109
Set.new
@@ -433,7 +433,7 @@ def entity_class_for_obj(object, options)
433433
# the given entity_class.
434434
def entity_representation_for(entity_class, object, options)
435435
embeds = { env: env }
436-
embeds[:version] = env[Grape::Env::API_VERSION] if env[Grape::Env::API_VERSION]
436+
embeds[:version] = env[Grape::Env::API_VERSION] if env.key?(Grape::Env::API_VERSION)
437437
entity_class.represent(object, **embeds.merge(options))
438438
end
439439
end

lib/grape/dsl/settings.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,9 @@ def namespace_reverse_stackable_with_hash(key)
109109
settings = get_or_set :namespace_reverse_stackable, key, nil
110110
return if settings.blank?
111111

112-
result = {}
113-
settings.each do |setting|
114-
setting.each do |field, value|
115-
result[field] ||= value
116-
end
112+
settings.each_with_object({}) do |setting, result|
113+
result.merge!(setting) { |_k, s1, _s2| s1 }
117114
end
118-
result
119115
end
120116

121117
# (see #unset_global_setting)

lib/grape/endpoint.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,9 @@ def to_routes
171171
end
172172

173173
def prepare_routes_requirements
174-
endpoint_requirements = options[:route_options][:requirements] || {}
175-
all_requirements = (namespace_stackable(:namespace).map(&:requirements) << endpoint_requirements)
176-
all_requirements.reduce({}) do |base_requirements, single_requirements|
177-
base_requirements.merge!(single_requirements)
174+
{}.merge!(*namespace_stackable(:namespace).map(&:requirements)).tap do |requirements|
175+
endpoint_requirements = options.dig(:route_options, :requirements)
176+
requirements.merge!(endpoint_requirements) if endpoint_requirements
178177
end
179178
end
180179

lib/grape/error_formatter/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def present(message, env)
2727

2828
if presenter
2929
embeds = { env: env }
30-
embeds[:version] = env[Grape::Env::API_VERSION] if env[Grape::Env::API_VERSION]
30+
embeds[:version] = env[Grape::Env::API_VERSION] if env.key?(Grape::Env::API_VERSION)
3131
presented_message = presenter.represent(presented_message, embeds).serializable_hash
3232
end
3333

lib/grape/exceptions/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ def translate(key, **options)
7373
options = options.dup
7474
options[:default] &&= options[:default].to_s
7575
message = ::I18n.translate(key, **options)
76-
message.present? ? message : fallback_message(key, **options)
76+
message.presence || fallback_message(key, **options)
7777
end
7878

7979
def fallback_message(key, **options)
80-
if ::I18n.enforce_available_locales && !::I18n.available_locales.include?(FALLBACK_LOCALE)
80+
if ::I18n.enforce_available_locales && ::I18n.available_locales.exclude?(FALLBACK_LOCALE)
8181
key
8282
else
8383
::I18n.translate(key, locale: FALLBACK_LOCALE, **options)

0 commit comments

Comments
 (0)