hash-pipe 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -2
- data/hash-pipe.gemspec +1 -1
- data/lib/hash-pipe.rb +2 -1
- data/lib/hash_pipe/flattening.rb +32 -0
- data/spec/hash-pipe/flattening_spec.rb +68 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 97c6b82f6891f63b2ebe99e853caad3ed0f9dba3
|
4
|
+
data.tar.gz: 2c3902d25d5e59190851df564ffccf96072ae411
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5b28216f53590f935ddb234de702ab5cb9b59c77ad50ccb0721981fd464122bea77adfa39fc40323116faeade97f052c5a8d9c56ddcb49670cfbd4a1f211648
|
7
|
+
data.tar.gz: eb0caa24de4e4da80bc3674cb56ba8126ffe9a43a3dc7b44f777cad650c441a4ccbe5ffff9dfb73da596e570cd6e6c0146d822fbbc7565d82a72e5f87dff52de
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
hash-pipe (0.
|
4
|
+
hash-pipe (0.3.0)
|
5
5
|
activesupport (~> 4.1)
|
6
6
|
|
7
7
|
GEM
|
@@ -32,7 +32,7 @@ GEM
|
|
32
32
|
diff-lcs (>= 1.1.3, < 2.0)
|
33
33
|
rspec-mocks (2.14.4)
|
34
34
|
slop (3.4.7)
|
35
|
-
thread_safe (0.3.
|
35
|
+
thread_safe (0.3.5)
|
36
36
|
tzinfo (1.2.2)
|
37
37
|
thread_safe (~> 0.1)
|
38
38
|
|
data/hash-pipe.gemspec
CHANGED
data/lib/hash-pipe.rb
CHANGED
@@ -0,0 +1,32 @@
|
|
1
|
+
Hash.class_eval do
|
2
|
+
# Flattens a hash with delimiters and key prefix/postfix.
|
3
|
+
#
|
4
|
+
# hash = {
|
5
|
+
# "errors" => {
|
6
|
+
# "something" => {
|
7
|
+
# "somethingElse" => {
|
8
|
+
# "password" => ["VALIDATION_STATE_PASSWORD"]
|
9
|
+
# }
|
10
|
+
# }
|
11
|
+
# }
|
12
|
+
# }
|
13
|
+
#
|
14
|
+
# Could be turned into a flattened hash using this:
|
15
|
+
#
|
16
|
+
# hash.string_from_hash prefix: "registration[", delimiter: "][", postfix: "]"
|
17
|
+
#
|
18
|
+
# And the end result would look like this:
|
19
|
+
#
|
20
|
+
# { "registration[errors][something][somethingElse][password]" => ["VALIDATION_STATE_PASSWORD"] }
|
21
|
+
def flatten_keys(prefix: "", postfix: "", delimiter: "")
|
22
|
+
each_with_object({}) do |(k, v), ret|
|
23
|
+
key = k.to_s
|
24
|
+
if v.is_a? Hash
|
25
|
+
ret.merge! v.flatten_keys(prefix: prefix + key + delimiter, postfix: postfix, delimiter: delimiter) if v.present?
|
26
|
+
else
|
27
|
+
ret[prefix + key + postfix] = v
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'flattening' do
|
4
|
+
it 'should do nothing on a flat hash' do
|
5
|
+
{ 'hello' => 'world' }.flatten_keys.should eq 'hello' => 'world'
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should do add a prefix' do
|
9
|
+
{ 'hello' => 'world' }.flatten_keys(prefix: "foo_").should eq 'foo_hello' => 'world'
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should allow nil values' do
|
13
|
+
{ 'hello' => 'world', 'bar' => nil }.flatten_keys(prefix: "foo_").should eq 'foo_hello' => 'world', 'foo_bar' => nil
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should work with nested hashes with no parameters' do
|
17
|
+
{ 'hello' => { 'world' => true }, 'foo' => 'bar' }.flatten_keys.should eq 'helloworld' => true, 'foo' => 'bar'
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should work with many levels of nested' do
|
21
|
+
{ 'hello' => { 'world' => { 'you' => { 'are' => 'awesome' } } } }.flatten_keys(delimiter: "_").should eq 'hello_world_you_are' => 'awesome'
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should work with delimters and post and prefixes' do
|
25
|
+
{
|
26
|
+
'hello' => {
|
27
|
+
'world' => {
|
28
|
+
'you' => {
|
29
|
+
'are' => 'awesome'
|
30
|
+
}
|
31
|
+
}
|
32
|
+
}
|
33
|
+
}.flatten_keys(
|
34
|
+
delimiter: "][",
|
35
|
+
prefix: "response[",
|
36
|
+
postfix: "]"
|
37
|
+
).should eq 'response[hello][world][you][are]' => 'awesome'
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should wrap keys in prefix and postfix' do
|
41
|
+
{
|
42
|
+
'hello' => {
|
43
|
+
'world' => {
|
44
|
+
'you' => {
|
45
|
+
'are' => 'awesome'
|
46
|
+
}
|
47
|
+
}
|
48
|
+
},
|
49
|
+
'my' => 'lord',
|
50
|
+
'foo' => 1,
|
51
|
+
'bam' => 'yam'
|
52
|
+
}.flatten_keys(
|
53
|
+
prefix: "|_",
|
54
|
+
postfix: "_|"
|
55
|
+
).should eq '|_helloworldyouare_|' => 'awesome',
|
56
|
+
'|_my_|' => 'lord',
|
57
|
+
'|_foo_|' => 1,
|
58
|
+
'|_bam_|' => 'yam'
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should work with nested arrays' do
|
62
|
+
{ 'hello' => { 'world' => [[1, 2, 3], [1, 2, 3]] } }.flatten_keys(delimiter: "_").should eq 'hello_world' => [[1, 2, 3], [1, 2, 3]]
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should work with symbol keys' do
|
66
|
+
{ hello: "world", foo: { :bar => "baz", "flam" => "boil"} }.flatten_keys(delimiter: ",").should eq "hello" => "world", "foo,bar" => "baz", "foo,flam" => "boil"
|
67
|
+
end
|
68
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash-pipe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Damien Timewell
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-04-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -67,9 +67,11 @@ files:
|
|
67
67
|
- README.md
|
68
68
|
- hash-pipe.gemspec
|
69
69
|
- lib/hash-pipe.rb
|
70
|
+
- lib/hash_pipe/flattening.rb
|
70
71
|
- lib/hash_pipe/key_conversion.rb
|
71
72
|
- lib/hash_pipe/nested_values.rb
|
72
73
|
- lib/hash_pipe/value_checks.rb
|
74
|
+
- spec/hash-pipe/flattening_spec.rb
|
73
75
|
- spec/hash-pipe/key_conversion_spec.rb
|
74
76
|
- spec/hash-pipe/nested_keys_spec.rb
|
75
77
|
- spec/hash-pipe/value_checks_spec.rb
|
@@ -99,6 +101,7 @@ signing_key:
|
|
99
101
|
specification_version: 4
|
100
102
|
summary: A small collection of useful hash extensions
|
101
103
|
test_files:
|
104
|
+
- spec/hash-pipe/flattening_spec.rb
|
102
105
|
- spec/hash-pipe/key_conversion_spec.rb
|
103
106
|
- spec/hash-pipe/nested_keys_spec.rb
|
104
107
|
- spec/hash-pipe/value_checks_spec.rb
|