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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8317453dcbfcc0f2e4f639c777b81957a37844c0
4
- data.tar.gz: 5979979f40acc682dd5c29146f338c0223ebf0da
3
+ metadata.gz: 97c6b82f6891f63b2ebe99e853caad3ed0f9dba3
4
+ data.tar.gz: 2c3902d25d5e59190851df564ffccf96072ae411
5
5
  SHA512:
6
- metadata.gz: 42fe12c24ade00fdb0203e93d74294cf92dc2a981c56ddbe8e36f5f08d1113135af8adff85d9ee6a35d9c33c840a329de4fcceeccb5691f3972d678fb561c5a4
7
- data.tar.gz: b6f6d992e1fd9f5a2423b76777ec9f9e3be8b5411f6e2c72e2ba1ee1dd94e7c67fdee3d7087593364f588a4d492d6b006c5dd36fd518c4c45ef8255d79318012
6
+ metadata.gz: b5b28216f53590f935ddb234de702ab5cb9b59c77ad50ccb0721981fd464122bea77adfa39fc40323116faeade97f052c5a8d9c56ddcb49670cfbd4a1f211648
7
+ data.tar.gz: eb0caa24de4e4da80bc3674cb56ba8126ffe9a43a3dc7b44f777cad650c441a4ccbe5ffff9dfb73da596e570cd6e6c0146d822fbbc7565d82a72e5f87dff52de
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hash-pipe (0.2.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.4)
35
+ thread_safe (0.3.5)
36
36
  tzinfo (1.2.2)
37
37
  thread_safe (~> 0.1)
38
38
 
@@ -2,7 +2,7 @@ $:.push File.expand_path("../lib", __FILE__)
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "hash-pipe"
5
- s.version = "0.2.0"
5
+ s.version = "0.3.0"
6
6
  s.authors = ["Damien Timewell"]
7
7
  s.email = ["mail@damientimewell.com"]
8
8
  s.licenses = ['MIT']
@@ -2,4 +2,5 @@ require 'active_support/all'
2
2
 
3
3
  require 'hash_pipe/key_conversion'
4
4
  require 'hash_pipe/value_checks'
5
- require 'hash_pipe/nested_values'
5
+ require 'hash_pipe/nested_values'
6
+ require 'hash_pipe/flattening'
@@ -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.2.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-02-16 00:00:00.000000000 Z
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