the_hash_whisperer 0.0.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.
- checksums.yaml +7 -0
- data/lib/the_hash_whisperer.rb +70 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 43d77e1352362539179673141aad2e31018eb454fc2913842c7d7fff33ae4f84
|
4
|
+
data.tar.gz: 1adb4aa3f60ee7a2977f18c4370b2c3990b582efa416cba653442d27f7d807aa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0ebe11c6b004979069da6e6a9d01503f576edb01585bab3794ced36ab78e815efb551fc62780d6ddab2b2f0c1e480d27f176e90615747a98f8ac908639e31155
|
7
|
+
data.tar.gz: de9ef18ede3a0bb1412c741a830cfbcbf5b45c8bd42c17f5253822f0c7008601e59762b2528b2c2e31249abd921ff86db520fd7c73fd4118bc1de2d11f34d2f6
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# dependencies
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
class TheHashWhisperer
|
5
|
+
|
6
|
+
def initialize(hash, json = false)
|
7
|
+
@hash_input = JSON.parse(hash) if json == true
|
8
|
+
@hash_input = JSON.parse(hash.to_json) if hash.is_a?(Hash)
|
9
|
+
@hash_input = JSON.parse(hash.to_json) if hash.is_a?(Array) && hash.flatten.first.is_a?(Hash)
|
10
|
+
return unless @hash_input && @hash_input.is_a?(Hash)
|
11
|
+
end
|
12
|
+
|
13
|
+
def drill_into_and_find(key)
|
14
|
+
keys = key.split('.')
|
15
|
+
target = keys.last
|
16
|
+
dig_values_for(keys, @hash_input, target)
|
17
|
+
end
|
18
|
+
|
19
|
+
# This method is pulled from:
|
20
|
+
# https://www.cookieshq.co.uk/posts/find-values-key-nested-hash-ruby
|
21
|
+
# Thank you for inspiring this gem, Zac Moody!
|
22
|
+
def find_all_values_for(key)
|
23
|
+
hash = @hash_input
|
24
|
+
result = []
|
25
|
+
result << hash[key] unless hash.is_a? Array
|
26
|
+
hash.each do |hash_value|
|
27
|
+
result << hash_value[key] unless hash_value.is_a? Array
|
28
|
+
if hash_value.is_a? Array
|
29
|
+
hash_value.each do |value|
|
30
|
+
result << find_all_values_for(key, value) if value.is_a? Hash
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
result.compact
|
35
|
+
end
|
36
|
+
|
37
|
+
def dig_values_for(keys, hash, target, iterating = false)
|
38
|
+
result = []
|
39
|
+
if (hash.is_a?(Array))
|
40
|
+
result << dig_array(keys, hash, target)
|
41
|
+
else
|
42
|
+
result << hash[target] if (keys.find_index(keys.first) == keys.find_index(keys.last))
|
43
|
+
end
|
44
|
+
|
45
|
+
return result.compact.flatten unless keys&.first && hash&.is_a?(Hash) && hash[keys.first]
|
46
|
+
if keys[1] && hash[keys.first].include?(keys[1])
|
47
|
+
hash = hash[keys.first] if hash.is_a?(Hash) && [keys.first]
|
48
|
+
keys.shift
|
49
|
+
result << dig_values_for(keys, hash, target)
|
50
|
+
else
|
51
|
+
if hash.is_a?(Hash) || hash.is_a?(Array)
|
52
|
+
hash = hash[keys.first]
|
53
|
+
keys.shift unless !iterating
|
54
|
+
result << dig_values_for(keys, hash, target)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
result.compact.flatten
|
58
|
+
end
|
59
|
+
|
60
|
+
def dig_array(keys, hash, target)
|
61
|
+
return unless hash.is_a?(Array)
|
62
|
+
result = []
|
63
|
+
keys.shift
|
64
|
+
hash.each do |value|
|
65
|
+
new_keys = keys.clone
|
66
|
+
result << dig_values_for(new_keys, value, target, true)
|
67
|
+
end
|
68
|
+
result.compact.flatten
|
69
|
+
end
|
70
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: the_hash_whisperer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ian Rand McKenzie
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-09-21 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: "Tame that wild hash to get the values you're looking for — \n without
|
14
|
+
making your eyes bleed staring at a jumbled console!"
|
15
|
+
email: admin@ianrandmckenzie.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/the_hash_whisperer.rb
|
21
|
+
homepage: https://rubygems.org/gems/the_hash_whisperer
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubygems_version: 3.1.2
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: The Hash Whisperer
|
44
|
+
test_files: []
|