hash_multi_tool 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.md +22 -3
- data/hash_multi_tool-0.1.1.gem +0 -0
- data/lib/hash_multi_tool/version.rb +1 -1
- data/lib/hash_multi_tool.rb +17 -3
- metadata +3 -2
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -27,7 +27,7 @@ Or install it yourself as:
|
|
27
27
|
{ foo: 'c', bar: 1 },
|
28
28
|
{ foo: 'b', bar: 4 }]
|
29
29
|
|
30
|
-
1) HashMultiTool.sort_by_order ARR_HASH, [:bar, :foo]
|
30
|
+
1) HashMultiTool.sort_by_order ARR_HASH, [:bar, :foo] :-
|
31
31
|
|
32
32
|
Note: By default it will sort in ASC order
|
33
33
|
|
@@ -37,13 +37,32 @@ Or install it yourself as:
|
|
37
37
|
|
38
38
|
2) Find array of hashes of matched key values :-
|
39
39
|
|
40
|
-
HashMultiTool.select_by_key_value
|
40
|
+
HashMultiTool.select_by_key_value ARR_HASH, :bar, 4
|
41
41
|
|
42
42
|
=> [{ foo: 'a', bar: 4 }, { foo: 'b', bar: 4 }]
|
43
43
|
|
44
|
-
3) Return all the available keys from a hash including subset hash
|
44
|
+
3) Return all the available keys from a hash including subset hash :-
|
45
|
+
|
45
46
|
hash = {"World" => "Region", "Country" => [{"Name" => "India", "Capital" => "Delhi"}, {"Name" => "England", "Capital" => "London"}]}
|
47
|
+
hash_keys = HashMultiTool.collect_keys hash
|
46
48
|
=> ["World","Country","Name","Capital","Name","Capital"]
|
49
|
+
|
50
|
+
hash_keys.uniq
|
51
|
+
=> ["World","Country","Name","Capital"]
|
52
|
+
|
53
|
+
4) Merge the keys and with collected values belongs to respective keys and return a single hash
|
54
|
+
arr = [{a:1, b:2, c:3},{a:1, b:2, c:3},{a:1, b:2, c:3},{a:1, b:2}]
|
55
|
+
|
56
|
+
hash_keys = HashMultiTool.transpose_to_hash arr
|
57
|
+
|
58
|
+
=> {:a=>[1, 1, 1, 1], :b=>[2, 2, 2, 2], :c=>[3, 3, 3]}
|
59
|
+
|
60
|
+
5) Return a collection of keys differentiated with values
|
61
|
+
hash = {:a=>[1, 1, 1, 1], :b=>[2, 2, 2, 2], :c=>[3, 3, 3]}
|
62
|
+
|
63
|
+
hash_keys = HashMultiTool.transpose_to_array hash
|
64
|
+
|
65
|
+
=> [{a:1, b:2, c:3},{a:1, b:2, c:3},{a:1, b:2, c:3},{a:1, b:2}]
|
47
66
|
|
48
67
|
## Development
|
49
68
|
|
Binary file
|
data/lib/hash_multi_tool.rb
CHANGED
@@ -2,7 +2,7 @@ require "hash_multi_tool/version"
|
|
2
2
|
|
3
3
|
module HashMultiTool
|
4
4
|
class << self
|
5
|
-
def sort_by_order hash =
|
5
|
+
def sort_by_order hash = [], order = [], direction ='ASC'
|
6
6
|
hash.sort do |a,b|
|
7
7
|
a_arr = []
|
8
8
|
b_arr = []
|
@@ -11,11 +11,11 @@ module HashMultiTool
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
def select_by_key_value hash =
|
14
|
+
def select_by_key_value hash = [], key = [], value = nil
|
15
15
|
hash.select { |h| h[key] == value }
|
16
16
|
end
|
17
17
|
|
18
|
-
def collect_keys
|
18
|
+
def collect_keys h_object
|
19
19
|
if h_object.is_a? Hash
|
20
20
|
(h_object.keys + collect_keys(h_object.values)).flatten.uniq
|
21
21
|
elsif h_object.is_a? Array
|
@@ -24,5 +24,19 @@ module HashMultiTool
|
|
24
24
|
[]
|
25
25
|
end
|
26
26
|
end
|
27
|
+
|
28
|
+
def transpose_to_hash arr = []
|
29
|
+
arr.inject({}){|a, h|
|
30
|
+
h.each_pair{|k,v| (a[k] ||= []) << v}
|
31
|
+
a
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
def transpose_to_array hash = {}
|
36
|
+
hash.inject([]){|a, (k,vs)|
|
37
|
+
vs.each_with_index{|v,i| (a[i] ||= {})[k] = v}
|
38
|
+
a
|
39
|
+
}
|
40
|
+
end
|
27
41
|
end
|
28
42
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_multi_tool
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-06-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -75,6 +75,7 @@ files:
|
|
75
75
|
- bin/console
|
76
76
|
- bin/setup
|
77
77
|
- hash_multi_tool-0.1.0.gem
|
78
|
+
- hash_multi_tool-0.1.1.gem
|
78
79
|
- hash_multi_tool.gemspec
|
79
80
|
- lib/hash_multi_tool.rb
|
80
81
|
- lib/hash_multi_tool/version.rb
|