hash_magick 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/hash_magick.rb +107 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 65190ab03f5ae6811390b4d77ea32073a7d12a3e
|
4
|
+
data.tar.gz: 113eab34380af3d6a93105d577c5deec339a76b0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4c3d35b6325bbc40e086f5179aa9c8aafcdb6e7fbb6cd456359895fef9957b0fdb82c0cdce3d8346f183077c03e9d7f1ae06454b0c20e71621b9d8b5179433c7
|
7
|
+
data.tar.gz: d2d3325e007f4346b9cb2b2ad3d4f61ef5dd4c2c5082e21ef4b863dd353ad607f1a863d82b3b9aa297c29590021bd3cb62f53988b98a8d6aeea5d9d8ef5cdbf1
|
data/lib/hash_magick.rb
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
class HashMagick
|
2
|
+
def self.welcome
|
3
|
+
puts "Welcome to HashMagick."
|
4
|
+
puts "Warning: Test. Do not use. Probably flammable."
|
5
|
+
end
|
6
|
+
|
7
|
+
def nestle_array(array,nested)
|
8
|
+
array.each do |line|
|
9
|
+
merge_to_category_hash(line,nested)
|
10
|
+
end
|
11
|
+
nested
|
12
|
+
end
|
13
|
+
|
14
|
+
def merge_to_category_hash(line,nested)
|
15
|
+
categories = line.split(/\s?:\s?/)
|
16
|
+
to_merge = {}
|
17
|
+
#puts "#{line}"
|
18
|
+
categories.each_with_index do |category, i|
|
19
|
+
target_key = categories[i]
|
20
|
+
new_value = categories[i+1] ? categories[i+1] : 0
|
21
|
+
|
22
|
+
deep_merge_add(nested, target_key, new_value)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def deep_merge_add(nested,target_k,new_v,top=true) #this is used build a nested hash from scratch, using an array
|
27
|
+
if target_k == "PomParsley"
|
28
|
+
#puts "#{target_k} => #{new_v} MERGES INTO #{nested}"
|
29
|
+
end
|
30
|
+
|
31
|
+
if target_k == "PomParsley" and nested == {"Blender Class"=>0, "Corgi"=>0}
|
32
|
+
#puts "YOU FOUND ME! I'm about to do a very bad THING!"
|
33
|
+
# only merge if it's at the top level!!!
|
34
|
+
end
|
35
|
+
new_v = {new_v => 0} if new_v.class == String
|
36
|
+
if hash_has_key?(nested,target_k)
|
37
|
+
#puts "I have the key!"
|
38
|
+
if nested.keys.include?(target_k)
|
39
|
+
if nested[target_k].class == Hash
|
40
|
+
nested[target_k].merge!(new_v)
|
41
|
+
else
|
42
|
+
nested[target_k] = new_v
|
43
|
+
end
|
44
|
+
else
|
45
|
+
nested.each do |k,v|
|
46
|
+
deep_merge_add(v,target_k,new_v,false) if v.class == Hash
|
47
|
+
end
|
48
|
+
end
|
49
|
+
else
|
50
|
+
|
51
|
+
if top
|
52
|
+
#puts "I don't have the key and IMA MERGE ANYWAY!"
|
53
|
+
nested.merge!({target_k => new_v})
|
54
|
+
end
|
55
|
+
end
|
56
|
+
# if target_k == "PomParsley"
|
57
|
+
# puts "!! #{target_k} => #{new_v} NOW IN #{nested}"
|
58
|
+
# end
|
59
|
+
end
|
60
|
+
|
61
|
+
def hash_has_key?(hash,key)
|
62
|
+
if hash.keys.include?(key)
|
63
|
+
return true
|
64
|
+
else
|
65
|
+
hash.each do |k,v|
|
66
|
+
return hash_has_key?(v,key) if v.class == Hash
|
67
|
+
end
|
68
|
+
end
|
69
|
+
false
|
70
|
+
end
|
71
|
+
|
72
|
+
def nestle_flat_hash(flat,nested)
|
73
|
+
flat.each { |k,v| merge_at_key(nested,k,v) }
|
74
|
+
return nested
|
75
|
+
end
|
76
|
+
|
77
|
+
def merge_at_key(nested,target_k,new_v) #this is for restructuring a flat hash to nested
|
78
|
+
if hash_has_key?(nested,target_k)
|
79
|
+
if nested.keys.include?(target_k)
|
80
|
+
if nested[target_k].class != Hash
|
81
|
+
nested.merge!({target_k => new_v}) { |k,v1,v2| v1+v2 }
|
82
|
+
else #if v is hash, it means the key has children, so add a "Misc" category for top-level category activity
|
83
|
+
|
84
|
+
# NOTE: commented code erroneously subtracts value
|
85
|
+
|
86
|
+
# extant_values = nested[target_k].values.inject(:+)
|
87
|
+
# nested[target_k].merge!({"Misc" => new_v-extant_values}) { |k,v1,v2| v1+v2 }
|
88
|
+
|
89
|
+
nested[target_k].merge!({"Misc" => new_v}) { |k,v1,v2| v1+v2 }
|
90
|
+
|
91
|
+
|
92
|
+
end
|
93
|
+
else
|
94
|
+
nested.each do |k,v|
|
95
|
+
merge_at_key(v,target_k,new_v) if v.class == Hash
|
96
|
+
end
|
97
|
+
end
|
98
|
+
else
|
99
|
+
nested.merge!({target_k => new_v})
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def add_to_key(target_hash,target_key,value) #could refactor with *target_hashes
|
104
|
+
target_hash.merge!({ target_key => value }) { |k, old_v, new_v| old_v + new_v }
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_magick
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- George A. Michel
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-13 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: For nesting flat hashes, etcetera.
|
14
|
+
email: sarumonakuba@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/hash_magick.rb
|
20
|
+
homepage: http://rubygems.org/gems/hash_magick
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.0.3
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Hash Magick!
|
44
|
+
test_files: []
|
45
|
+
has_rdoc:
|