Skip to content

Commit 8c7a8b9

Browse files
committed
fix: support Hash#transform_keys for older Rubies
1 parent e0325e5 commit 8c7a8b9

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
module ActionPolicy
4+
module Ext
5+
# Add transform_keys to Hash for older Rubys
6+
module HashTransformKeys
7+
refine Hash do
8+
def transform_keys
9+
return enum_for(:transform_keys) { size } unless block_given?
10+
result = {}
11+
each_key do |key|
12+
result[yield(key)] = self[key]
13+
end
14+
result
15+
end
16+
end
17+
end
18+
end
19+
end

lib/action_policy/policy/cache.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ module ActionPolicy # :nodoc:
99
require "action_policy/ext/yield_self_then"
1010
require "action_policy/ext/policy_cache_key"
1111

12-
using ActionPolicy::Ext::YieldSelfThen
12+
unless "".respond_to?(:then)
13+
require "action_policy/ext/yield_self_then"
14+
using ActionPolicy::Ext::YieldSelfThen
15+
end
1316
using ActionPolicy::Ext::PolicyCacheKey
1417

1518
module Policy

lib/action_policy/policy/reasons.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
# frozen_string_literal: true
22

3-
require "action_policy/ext/yield_self_then"
4-
using ActionPolicy::Ext::YieldSelfThen
3+
unless "".respond_to?(:then)
4+
require "action_policy/ext/yield_self_then"
5+
using ActionPolicy::Ext::YieldSelfThen
6+
end
7+
8+
unless {}.respond_to?(:transform_keys)
9+
require "action_policy/ext/hash_transform_keys"
10+
using ActionPolicy::Ext::HashTransformKeys
11+
end
512

613
module ActionPolicy
714
module Policy

lib/action_policy/utils/pretty_print.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
end
1010

1111
module ActionPolicy
12-
require "action_policy/ext/yield_self_then"
13-
using ActionPolicy::Ext::YieldSelfThen
12+
unless "".respond_to?(:then)
13+
require "action_policy/ext/yield_self_then"
14+
using ActionPolicy::Ext::YieldSelfThen
15+
end
1416

1517
# Takes the object and a method name,
1618
# and returns the "annotated" source code for the method:

0 commit comments

Comments
 (0)