Skip to content
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

Set 2.6 as minimum ruby version for Sinatra 3 #1699

Merged
merged 5 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Remove not needed checks for methods defined
  • Loading branch information
epergo committed May 21, 2021
commit 91096ac9602f1c530a513d5a071fa3fb860f161f
41 changes: 15 additions & 26 deletions lib/sinatra/indifferent_hash.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
# frozen_string_literal: true
$stderr.puts <<EOF if !Hash.method_defined?(:slice) && !$LOAD_PATH.grep(%r{gems/activesupport}).empty? && ENV['SINATRA_ACTIVESUPPORT_WARNING'] != 'false'
Copy link
Member Author

Choose a reason for hiding this comment

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

Hash#slice was added in Ruby 2.5 so this if is going to be false always.

WARNING: If you plan to load any of ActiveSupport's core extensions to Hash, be
sure to do so *before* loading Sinatra::Application or Sinatra::Base. If not,
you may disregard this warning.

Set SINATRA_ACTIVESUPPORT_WARNING=false in the environment to hide this warning.
EOF

module Sinatra
# A poor man's ActiveSupport::HashWithIndifferentAccess, with all the Rails-y
Expand Down Expand Up @@ -112,19 +105,19 @@ def delete(key)

def dig(key, *other_keys)
super(convert_key(key), *other_keys)
end if method_defined?(:dig) # Added in Ruby 2.3
end

def fetch_values(*keys)
keys.map!(&method(:convert_key))

super(*keys)
end if method_defined?(:fetch_values) # Added in Ruby 2.3
end

def slice(*keys)
keys.map!(&method(:convert_key))

self.class[super(*keys)]
end if method_defined?(:slice) # Added in Ruby 2.5
end

def values_at(*keys)
keys.map!(&method(:convert_key))
Expand Down Expand Up @@ -158,26 +151,22 @@ def replace(other_hash)
super(other_hash.is_a?(self.class) ? other_hash : self.class[other_hash])
end

if method_defined?(:transform_values!) # Added in Ruby 2.4
def transform_values(&block)
dup.transform_values!(&block)
end
def transform_values(&block)
dup.transform_values!(&block)
end

def transform_values!
super
super(&method(:convert_value))
end
def transform_values!
super
super(&method(:convert_value))
end

if method_defined?(:transform_keys!) # Added in Ruby 2.5
def transform_keys(&block)
dup.transform_keys!(&block)
end
def transform_keys(&block)
dup.transform_keys!(&block)
end

def transform_keys!
super
super(&method(:convert_key))
end
def transform_keys!
super
super(&method(:convert_key))
end

private
Expand Down
22 changes: 0 additions & 22 deletions test/indifferent_hash_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
#
require 'minitest/autorun' unless defined?(Minitest)

# Suppress the ActiveSupport warning when this test is executed independently,
# outside of the full suite, on older Rubies.
ENV['SINATRA_ACTIVESUPPORT_WARNING'] = 'false'

require_relative '../lib/sinatra/indifferent_hash'

class TestIndifferentHashBasics < Minitest::Test
Expand Down Expand Up @@ -69,10 +65,6 @@ def test_merge!
end

class TestIndifferentHash < Minitest::Test
def skip_if_lacking(meth)
skip "Hash##{meth} not supported on this Ruby" unless Hash.method_defined?(meth)
end

def setup
@hash = Sinatra::IndifferentHash[:a=>:a, ?b=>:b, 3=>3,
:simple_nested=>{ :a=>:a, ?b=>:b },
Expand Down Expand Up @@ -158,8 +150,6 @@ def test_delete
end

def test_dig
skip_if_lacking :dig

assert_equal :a, @hash.dig(:a)
assert_equal :b, @hash.dig(?b)
assert_nil @hash.dig(:d)
Expand All @@ -174,8 +164,6 @@ def test_dig
end

def test_slice
skip_if_lacking :slice

assert_equal Sinatra::IndifferentHash[a: :a], @hash.slice(:a)
assert_equal Sinatra::IndifferentHash[b: :b], @hash.slice(?b)
assert_equal Sinatra::IndifferentHash[3 => 3], @hash.slice(3)
Expand All @@ -186,8 +174,6 @@ def test_slice
end

def test_fetch_values
skip_if_lacking :fetch_values

assert_raises(KeyError) { @hash.fetch_values(3, :d) }
assert_equal [:a, :b, 3, ?D], @hash.fetch_values(:a, ?b, 3, :d) { |k| k.upcase }
end
Expand Down Expand Up @@ -225,16 +211,12 @@ def test_replace
end

def test_transform_values!
skip_if_lacking :transform_values!

@hash.transform_values! { |v| v.is_a?(Hash) ? Hash[v.to_a] : v }

assert_instance_of Sinatra::IndifferentHash, @hash[:simple_nested]
end

def test_transform_values
skip_if_lacking :transform_values

hash2 = @hash.transform_values { |v| v.respond_to?(:upcase) ? v.upcase : v }

refute_equal @hash, hash2
Expand All @@ -243,17 +225,13 @@ def test_transform_values
end

def test_transform_keys!
skip_if_lacking :transform_keys!

@hash.transform_keys! { |k| k.respond_to?(:to_sym) ? k.to_sym : k }

assert_equal :a, @hash[:a]
assert_equal :a, @hash[?a]
end

def test_transform_keys
skip_if_lacking :transform_keys

hash2 = @hash.transform_keys { |k| k.respond_to?(:upcase) ? k.upcase : k }

refute_equal @hash, hash2
Expand Down