20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/permisi/permission_util.rb', line 20
def transform_namespace(namespace, current_path: nil)
HashWithIndifferentAccess.new.tap do |transformed|
namespace.each_pair do |key, value|
if !value.is_a? Array
raise InvalidNamespace,
"`#{[current_path, key].compact.join(".")}` should be an array"
end
if key.to_s.include?(".")
raise InvalidNamespace, "namespace or action should not contain period: `#{key}`"
end
value.each.with_index do |arr_v, arr_i|
case arr_v
when Symbol
if arr_v.to_s.include?(".")
raise InvalidNamespace, "namespace or action should not contain period: `#{arr_v}`"
end
transformed[key] ||= ::HashWithIndifferentAccess.new
if transformed[key].key? arr_v
raise InvalidNamespace, "duplicate entry: `#{[current_path, key, arr_v].compact.join(".")}`"
end
transformed[key][arr_v] = false
when Hash
transform_namespace(arr_v,
current_path: [current_path, key].compact.join(".")).each_pair do |ts_k, ts_v|
transformed[key] ||= ::HashWithIndifferentAccess.new
if transformed[key].key? ts_k
raise InvalidNamespace, "duplicate entry: `#{[current_path, key, ts_k].compact.join(".")}`"
end
transformed[key][ts_k] = ts_v
end
else
raise InvalidNamespace,
"`#{[current_path, key].compact.join(".")}[#{arr_i}]` should be a symbol or a hash"
end
end
end
end
end
|