hash_extensions 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hash_extensions.rb +26 -2
- metadata +5 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 076cb1da870e83259e4714b53a3aad6599173170
|
4
|
+
data.tar.gz: 55b8ddc7b442b0d002b78ce5da6ca82d9001d769
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 10b701d6490e727be55fa81f71699c68bb1c22b890f034f57f1f0ee9a92da7dbac6919a4daaa345d3180af7cfcf0368488f80cef57acf34ca8acb884162d99d2
|
7
|
+
data.tar.gz: f63341b90af97f50da472b55e2ee05aafe4e74fad949f8d9ce45c5866c651d8de29ce190d41dc41ecd26049711cca6760293ce1b584fdc32c73c59577f55d9c3
|
data/lib/hash_extensions.rb
CHANGED
@@ -3,7 +3,7 @@ require 'active_support/all'
|
|
3
3
|
class Hash
|
4
4
|
# Search for key path in hash
|
5
5
|
#
|
6
|
-
# Examples
|
6
|
+
# == Examples
|
7
7
|
#
|
8
8
|
# { a: { b: :c } }.dig(:a, :b) # => :c
|
9
9
|
# { a: { 'b' => :c } }.dig(:a, :b) # => :c
|
@@ -16,7 +16,7 @@ class Hash
|
|
16
16
|
|
17
17
|
# Search the Hash deeply for all values matching the +key+.
|
18
18
|
#
|
19
|
-
# Examples
|
19
|
+
# == Examples
|
20
20
|
#
|
21
21
|
# { a: :b, c: { a: '1', d: :e } }.values_for('a') # => [:b, '1']
|
22
22
|
def values_for(key)
|
@@ -30,4 +30,28 @@ class Hash
|
|
30
30
|
|
31
31
|
found_values.flatten.compact
|
32
32
|
end
|
33
|
+
|
34
|
+
# Flattens the hash keys recursively
|
35
|
+
#
|
36
|
+
# == Examples
|
37
|
+
#
|
38
|
+
# { a: :b, c: { d: :e } }.recursive_flat_map # => [ :a, :c, :d ]
|
39
|
+
def recursive_flat_map
|
40
|
+
a = flat_map do |k, v|
|
41
|
+
v = v.respond_to?(:recursive_flat_map) ? v.recursive_flat_map : nil
|
42
|
+
[k, v].compact
|
43
|
+
end
|
44
|
+
a.flatten
|
45
|
+
end
|
46
|
+
|
47
|
+
# Recursively flattens and sorts a hash into its keys and values.
|
48
|
+
#
|
49
|
+
# == Examples
|
50
|
+
#
|
51
|
+
# { c: { d: :e }, a: :b, f: ['g', 'h'] }.recursive_flatten # => [:a, :b, :c, :d, :e, :f, 'g', 'h']
|
52
|
+
def recursive_flatten
|
53
|
+
sort.flat_map do |k, v|
|
54
|
+
[k, v.respond_to?(:recursive_flatten) ? v.recursive_flatten : v].compact
|
55
|
+
end.flatten
|
56
|
+
end
|
33
57
|
end
|
metadata
CHANGED
@@ -1,43 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Emil Kampp
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '4.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '4.2'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: rspec
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '3.3'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
22
|
version_requirements: !ruby/object:Gem::Requirement
|
37
23
|
requirements:
|
38
24
|
- - "~>"
|
39
25
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
26
|
+
version: '4.2'
|
41
27
|
description: Some common, practical hash hash_extensions
|
42
28
|
email: emil@kampp.me
|
43
29
|
executables: []
|
@@ -65,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
51
|
version: '0'
|
66
52
|
requirements: []
|
67
53
|
rubyforge_project:
|
68
|
-
rubygems_version: 2.4.5
|
54
|
+
rubygems_version: 2.4.5
|
69
55
|
signing_key:
|
70
56
|
specification_version: 4
|
71
57
|
summary: Some common, practical hash hash_extensions
|