forked from railslove/rack-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextensions.rb
More file actions
64 lines (57 loc) · 1.61 KB
/
extensions.rb
File metadata and controls
64 lines (57 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
63
64
require 'ostruct'
# Backport of 2.0.0 stdlib ostruct#to_h
class OpenStruct
def to_h
@table.dup
end unless method_defined? :to_h
end
class Hash
def stringify_values
inject({}) do |options, (key, value)|
options[key] = value.to_s
options
end
end
def compact
select { |_, value| !value.nil? }
end
def deep_merge!(other_hash, &block)
other_hash.each_pair do |k,v|
tv = self[k]
if tv.is_a?(Hash) && v.is_a?(Hash)
self[k] = tv.deep_merge(v, &block)
else
self[k] = block && tv ? block.call(k, tv, v) : v
end
end
self
end
# NOTE Back ported from Rails 4 to 3
# Destructively convert all keys by using the block operation.
# This includes the keys from the root hash and from all
# nested hashes.
def deep_transform_keys!(&block)
_deep_transform_keys_in_object!(self, &block)
end unless method_defined? :deep_transform_keys!
def _deep_transform_keys_in_object!(object, &block)
case object
when Hash
object.keys.each do |key|
value = object.delete(key)
object[yield(key)] = _deep_transform_keys_in_object!(value, &block)
end
object
when Array
object.map! {|e| _deep_transform_keys_in_object!(e, &block)}
else
object
end
end unless method_defined? :_deep_transform_keys_in_object!
# NOTE Back ported from Rails 4 to 3
# Destructively convert all keys to strings.
# This includes the keys from the root hash and from all
# nested hashes.
def deep_stringify_keys!
deep_transform_keys!{ |key| key.to_s }
end unless method_defined? :deep_stringify_keys!
end