mobme_support 2.2.4 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,24 +7,43 @@ require_relative '../../version'
|
|
7
7
|
module MobME::Infrastructure::Utilities::CoreExtensions
|
8
8
|
# Hash extension, allowing recursive Hash key symbolization
|
9
9
|
module Keys
|
10
|
-
#
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
# Returns a version of the supplies Hash or Array with all Hash keys symbolized.
|
11
|
+
#
|
12
|
+
# @param [Boolean] modify_nested_arrays set to true to iterate over array contents. Defaults to false.
|
13
|
+
# @return [Hash, Array] recursively symbolized Array or Hash
|
14
|
+
def recursively_symbolize_keys(modify_nested_arrays = false)
|
15
|
+
recursively_symbolized_value = case self
|
16
|
+
when Hash
|
17
|
+
symbolized_hash = symbolize_keys
|
15
18
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
+
symbolized_hash.each do |key, value|
|
20
|
+
if value.is_a?(Hash) || (modify_nested_arrays && value.is_a?(Array))
|
21
|
+
symbolized_hash[key] = value.recursively_symbolize_keys(modify_nested_arrays)
|
22
|
+
end
|
19
23
|
end
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
24
|
+
|
25
|
+
symbolized_hash
|
26
|
+
when Array
|
27
|
+
symbolized_array = self.dup
|
28
|
+
|
29
|
+
symbolized_array.each_with_index do |value, index|
|
30
|
+
symbolized_array[index] = value.recursively_symbolize_keys(true) if (value.is_a?(Hash) || value.is_a?(Array))
|
31
|
+
end
|
32
|
+
|
33
|
+
symbolized_array
|
25
34
|
end
|
26
35
|
|
27
|
-
|
36
|
+
recursively_symbolized_value
|
37
|
+
end
|
38
|
+
|
39
|
+
alias_method :recursive_symbolize_keys, :recursively_symbolize_keys
|
40
|
+
|
41
|
+
# Recursively symbolize all keys in hashes.
|
42
|
+
#
|
43
|
+
# @param [Boolean] modify_nested_arrays set to true to modify array contents. Defaults to false.
|
44
|
+
# @return [Hash, Array] recursively symbolized Array or Hash
|
45
|
+
def recursively_symbolize_keys!(modify_nested_arrays = false)
|
46
|
+
replace recursively_symbolize_keys(modify_nested_arrays)
|
28
47
|
end
|
29
48
|
|
30
49
|
alias_method :recursive_symbolize_keys!, :recursively_symbolize_keys!
|
@@ -6,28 +6,27 @@ module MobMESupport::CoreExtensions
|
|
6
6
|
describe "Hash" do
|
7
7
|
subject { {"foo" => {"bar" => "baz"}, :already_symbol => "far", 123 => "faz"} }
|
8
8
|
|
9
|
-
describe "
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
describe "recursively_symbolize_keys" do
|
10
|
+
it "returns a recursilvely symbolized version of the value" do
|
11
|
+
subject.recursively_symbolize_keys.should == {:foo => {:bar => "baz"}, :already_symbol => "far", 123 => "faz"}
|
12
|
+
end
|
13
13
|
|
14
|
-
it "
|
15
|
-
|
14
|
+
it "does not alter the original object" do
|
15
|
+
subject.recursively_symbolize_keys
|
16
|
+
subject.should == {"foo" => {"bar" => "baz"}, :already_symbol => "far", 123 => "faz"}
|
16
17
|
end
|
17
18
|
|
18
19
|
context "when the modify_nested_arrays flag is set to true" do
|
19
20
|
it "recursively symbolizes nested Hashes if they are nested inside an Array" do
|
20
21
|
with_hash_inside_array = {"foo" => {"bar" => [{"baz" => [{"qux" => "qar"}, "string"]}]}}
|
21
|
-
with_hash_inside_array.recursively_symbolize_keys
|
22
|
-
with_hash_inside_array.should == {:foo => {:bar => [{:baz => [{:qux => "qar"}, "string"]}]}}
|
22
|
+
with_hash_inside_array.recursively_symbolize_keys(true).should == {:foo => {:bar => [{:baz => [{:qux => "qar"}, "string"]}]}}
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
context "when the modify_nested_arrays flag is not set" do
|
27
27
|
it "does not recursively symbolize nested Hashes if they are nested inside an Array" do
|
28
28
|
with_hash_inside_array = {"foo" => {"bar" => [{"baz" => "qux"}]}}
|
29
|
-
with_hash_inside_array.recursively_symbolize_keys
|
30
|
-
with_hash_inside_array.should == {:foo => {:bar => [{"baz" => "qux"}]}}
|
29
|
+
with_hash_inside_array.recursively_symbolize_keys.should == {:foo => {:bar => [{"baz" => "qux"}]}}
|
31
30
|
end
|
32
31
|
end
|
33
32
|
|
@@ -37,7 +36,7 @@ module MobMESupport::CoreExtensions
|
|
37
36
|
sample_hash = {"far" => "bar"}
|
38
37
|
hash_to_symbolize = {"array" => sample_array, "hash" => sample_hash}
|
39
38
|
|
40
|
-
hash_to_symbolize.recursively_symbolize_keys
|
39
|
+
hash_to_symbolize.recursively_symbolize_keys(true)
|
41
40
|
|
42
41
|
sample_array.should == [{"foo" => "bar"}]
|
43
42
|
sample_hash.should == {"far" => "bar"}
|
@@ -45,6 +44,21 @@ module MobMESupport::CoreExtensions
|
|
45
44
|
end
|
46
45
|
end
|
47
46
|
|
47
|
+
describe "#recursively_symbolize_keys!" do
|
48
|
+
it "replaces itself with the recursively symbolized version" do
|
49
|
+
replacement_object = double "Replacement"
|
50
|
+
subject.stub recursively_symbolize_keys: replacement_object
|
51
|
+
subject.should_receive(:replace).with(replacement_object)
|
52
|
+
subject.recursively_symbolize_keys!
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#recursive_symbolize_keys" do
|
57
|
+
it "is an alias for #recursively_symbolize_keys" do
|
58
|
+
subject.method(:recursive_symbolize_keys).should == subject.method(:recursively_symbolize_keys)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
48
62
|
describe "#recursive_symbolize_keys!" do
|
49
63
|
it "is an alias for #recursively_symbolize_keys!" do
|
50
64
|
subject.method(:recursive_symbolize_keys!).should == subject.method(:recursively_symbolize_keys!)
|
@@ -53,11 +67,20 @@ module MobMESupport::CoreExtensions
|
|
53
67
|
end
|
54
68
|
|
55
69
|
describe "Array" do
|
56
|
-
|
70
|
+
subject { [{"foo" => "bar"}, {"baz" => "qux"}] }
|
71
|
+
|
72
|
+
describe "#recursively_symbolize_keys" do
|
57
73
|
it "recursively symbolizes Hash values in the array" do
|
58
|
-
|
59
|
-
|
60
|
-
|
74
|
+
subject.recursively_symbolize_keys.should == [{:foo => "bar"}, {:baz => "qux"}]
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "#recursively_symbolize_keys!" do
|
79
|
+
it "replaces itself with the recursively symbolized version" do
|
80
|
+
replacement_object = double "Replacement"
|
81
|
+
subject.stub recursively_symbolize_keys: replacement_object
|
82
|
+
subject.should_receive(:replace).with(replacement_object)
|
83
|
+
subject.recursively_symbolize_keys!
|
61
84
|
end
|
62
85
|
end
|
63
86
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mobme_support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-08-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: i18n
|