ruby-development-toolbox 1.0.3 → 1.1.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 +4 -4
- data/README.md +1 -1
- data/VERSION +1 -1
- data/lib/toolbox/hash_diff.rb +153 -0
- data/lib/toolbox/uuid.rb +38 -0
- data/ruby-development-toolbox.gemspec +4 -2
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 664d3ef4c0c0e9946f9d2d2ea0cdc16a20174beb
|
4
|
+
data.tar.gz: 04c4ae771e114310d5ec10c23111033ef91497d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce2305afc283e4d452f37af764a6db03fd2ac9db3b39401d329a79c0b5c33e15f2a4ab9d355b13ef72b72a78503e80c47cd7dfd6f3cba6d9bab2b60a636a5574
|
7
|
+
data.tar.gz: a0012109a92a998c685b046318cf66d15c514b96cfc7ec50031e845ae4e500c8d2a563a6d34333805f42836013dd5b97e279701e33acb1143f41866ab6691c4c
|
data/README.md
CHANGED
@@ -40,7 +40,7 @@ The projects homepage can be found [here](https://github.com/gradeawarrior/ruby-
|
|
40
40
|
|
41
41
|
# Development
|
42
42
|
|
43
|
-
The project is built by [jeweler](https://github.com/technicalpickles/jeweler). See the project's page for more details about how to manage this gem. However, I will list out quick
|
43
|
+
The project is built by [jeweler](https://github.com/technicalpickles/jeweler). See the project's page for more details about how to manage this gem. However, I will list out quick guidance on a typical release.
|
44
44
|
|
45
45
|
## Active Gem Development
|
46
46
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0
|
1
|
+
1.1.0
|
@@ -0,0 +1,153 @@
|
|
1
|
+
##
|
2
|
+
# Extends the functionality of a Hash to be able to perform (i) diff and (ii) similarity operations
|
3
|
+
#
|
4
|
+
class Hash
|
5
|
+
|
6
|
+
##
|
7
|
+
# Provides diff capabilities on a Hash. This implementation is slightly customized for
|
8
|
+
# Proofpoint Prism service-records, but otherwise should work on any Hash
|
9
|
+
#
|
10
|
+
def diff(other, svc_record_map = nil)
|
11
|
+
svc_record_map = {} unless svc_record_map.kind_of?(Hash)
|
12
|
+
(self.keys + other.keys).uniq.inject({}) do |memo, key|
|
13
|
+
rkeys = svc_record_map[key] || [key]
|
14
|
+
rkeys.each do |rkey|
|
15
|
+
diff_key = (rkeys.size > 1 || (key != rkey)) ? "#{key} --> #{rkey}" : rkey
|
16
|
+
if self[key].kind_of?(Hash) && other[rkey].kind_of?(Hash)
|
17
|
+
memo[diff_key] = self[key].diff(other[rkey])
|
18
|
+
elsif self[rkey] != other[rkey]
|
19
|
+
memo[diff_key] = [self[key], other[rkey]]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
memo
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
##
|
27
|
+
# Provides similarity capabilities on a Hash. This implementation is slightly optimized for
|
28
|
+
# Proofpoint Prism service-records, but otherwise should work on any Hash
|
29
|
+
#
|
30
|
+
# == Parameters
|
31
|
+
#
|
32
|
+
# other
|
33
|
+
#
|
34
|
+
def similarity(other, svc_record_map = nil)
|
35
|
+
svc_record_map = {} unless svc_record_map.kind_of?(Hash)
|
36
|
+
(self.keys + other.keys).uniq.inject({}) do |memo, key|
|
37
|
+
rkeys = svc_record_map.has_key?(key) ? svc_record_map[key] : [key]
|
38
|
+
rkeys.each do |rkey|
|
39
|
+
diff_key = (rkeys.size > 1 || (key != rkey)) ? "#{key} --> #{rkey}" : rkey
|
40
|
+
if self[key].kind_of?(Hash) && other[rkey].kind_of?(Hash)
|
41
|
+
memo[diff_key] = self[key].similarity(other[rkey])
|
42
|
+
elsif self[key] == other[rkey]
|
43
|
+
memo[diff_key] = self[key]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
memo
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
##
|
51
|
+
# Utility for deleting any Prism service-record keys that I don't want to do a diff/similarity on
|
52
|
+
#
|
53
|
+
def clean
|
54
|
+
['svc_id', 'name', 'environment_name', 'type', 'subtype', 'note'].each { |k| self.delete k }
|
55
|
+
self.keys.each do |svc_record_name|
|
56
|
+
self[svc_record_name].clean() if self[svc_record_name].kind_of?(Hash)
|
57
|
+
end
|
58
|
+
self.keys.each do |key|
|
59
|
+
if self[key].kind_of?(Hash) && self[key].empty?
|
60
|
+
self.delete(key)
|
61
|
+
elsif self[key].kind_of?(Hash)
|
62
|
+
self[key].clean
|
63
|
+
self.delete(key) if self[key].empty?
|
64
|
+
end
|
65
|
+
end
|
66
|
+
self
|
67
|
+
end
|
68
|
+
|
69
|
+
##
|
70
|
+
# Utility for counting the number of leaf nodes in a Hash-Array structure. A leaf node is determined
|
71
|
+
# to be anything that is a non-Hash
|
72
|
+
#
|
73
|
+
def count_leafs
|
74
|
+
self.keys.inject(0) do |result, element|
|
75
|
+
if self[element].kind_of?(Hash)
|
76
|
+
result+=self[element].count_leafs
|
77
|
+
else
|
78
|
+
result+=1
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
##
|
84
|
+
# Return only the List of Prism service-records; if the List is empty, then return the original Hash with no filters
|
85
|
+
# applied.
|
86
|
+
#
|
87
|
+
# == Parameters
|
88
|
+
#
|
89
|
+
# filter_keys : an array of keys to exclude
|
90
|
+
# filter_values : an array of values to exclude
|
91
|
+
#
|
92
|
+
def filter(filter_keys = nil, filter_values = nil)
|
93
|
+
filter_keys = Array.new unless filter_keys.kind_of?(Array)
|
94
|
+
filter_values = Array.new unless filter_values.kind_of?(Array)
|
95
|
+
result = filter_keys.empty? ? self : Hash.new
|
96
|
+
filter_keys.each do |key|
|
97
|
+
self.keys.each do |svc_record|
|
98
|
+
if svc_record.match(/^#{key}$/)
|
99
|
+
result[svc_record] = self[svc_record]
|
100
|
+
elsif svc_record.match(/^#{key} -->/)
|
101
|
+
result[svc_record] = self[svc_record]
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
filter_values.each do |filter_value|
|
106
|
+
result.keys.each do |key|
|
107
|
+
if result[key].kind_of?(Hash)
|
108
|
+
result[key] = result[key].filter(nil, filter_values)
|
109
|
+
result.delete(key) if result[key].keys.empty?
|
110
|
+
elsif result[key].kind_of?(Array) && key.match(/version/).nil?
|
111
|
+
filter_triggered = false
|
112
|
+
result[key].each do |diff_value|
|
113
|
+
filter_triggered = diff_value =~ /#{filter_value}/
|
114
|
+
break if filter_triggered
|
115
|
+
end
|
116
|
+
result.delete(key) if filter_triggered
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
result
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
##
|
125
|
+
# Extends the functionality of a Hash to be able to perform (i) diff and (ii) similarity operations
|
126
|
+
# For implementation details, see the Hash class for the extended functions
|
127
|
+
#
|
128
|
+
# == Usage
|
129
|
+
#
|
130
|
+
# Working with HashDiff module can be very simple, for example:
|
131
|
+
#
|
132
|
+
# require 'toolbox/hash_diff'
|
133
|
+
# require 'yaml'
|
134
|
+
#
|
135
|
+
# hash1 = {
|
136
|
+
# :foo => 'bar',
|
137
|
+
# :bar => 'hello',
|
138
|
+
# :hello => 'world',
|
139
|
+
# :this => { :exists => 'yay!' }
|
140
|
+
# }
|
141
|
+
#
|
142
|
+
# hash2 = {
|
143
|
+
# :foo => 'bar',
|
144
|
+
# :hello => 'Hi',
|
145
|
+
# :this => { :exists => 'yay!' },
|
146
|
+
# :hey => { :this => "Doesn't exist", :but => "oh well" }
|
147
|
+
# }
|
148
|
+
#
|
149
|
+
# puts hash1.diff(hash2).to_yaml
|
150
|
+
# puts hash1.similarity(hash2).to_yaml
|
151
|
+
#
|
152
|
+
module HashDiff
|
153
|
+
end
|
data/lib/toolbox/uuid.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
##
|
2
|
+
# A generic UUID class
|
3
|
+
#
|
4
|
+
# == Usage
|
5
|
+
#
|
6
|
+
# $ irb
|
7
|
+
# require 'toolbox/uuid'
|
8
|
+
# => true
|
9
|
+
# UUID.generate
|
10
|
+
# => "0a391631-22ba-40ea-af2e-65a64de4a42b"
|
11
|
+
# UUID.generate
|
12
|
+
# => "092516ba-a2f4-45cf-9e1d-c5e63342aaa4"
|
13
|
+
#
|
14
|
+
class UUID
|
15
|
+
|
16
|
+
##
|
17
|
+
# Generate a random UUID.
|
18
|
+
#
|
19
|
+
# Attempts a few different ways, hopefully one of them work. If all fails, die.
|
20
|
+
#
|
21
|
+
def self.generate
|
22
|
+
begin
|
23
|
+
require 'securerandom'
|
24
|
+
return SecureRandom.uuid()
|
25
|
+
|
26
|
+
rescue Exception => e
|
27
|
+
if (File.exist?("/usr/bin/uuidgen")) # Centos e2fsprogs package
|
28
|
+
uuid = `/usr/bin/uuidgen`
|
29
|
+
return uuid.chomp
|
30
|
+
elsif (File.exist?("/usr/bin/uuid")) # Debian uuid package
|
31
|
+
uuid = `/usr/bin/uuid`
|
32
|
+
return uuid.chomp
|
33
|
+
end
|
34
|
+
end
|
35
|
+
abort("Unable to generate UUIDs")
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "ruby-development-toolbox"
|
8
|
-
s.version = "1.0
|
8
|
+
s.version = "1.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Peter Salas"]
|
12
|
-
s.date = "2014-02-
|
12
|
+
s.date = "2014-02-21"
|
13
13
|
s.description = "A collection of useful utilities and libraries for Ruby development (not Rails)"
|
14
14
|
s.email = "psalas+github@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -28,6 +28,8 @@ Gem::Specification.new do |s|
|
|
28
28
|
"VERSION",
|
29
29
|
"lib/ruby-development-toolbox.rb",
|
30
30
|
"lib/toolbox/boolean.rb",
|
31
|
+
"lib/toolbox/hash_diff.rb",
|
32
|
+
"lib/toolbox/uuid.rb",
|
31
33
|
"ruby-development-toolbox.gemspec",
|
32
34
|
"test/helper.rb",
|
33
35
|
"test/test_ruby-development-toolbox.rb"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-development-toolbox
|
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
|
- Peter Salas
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: shoulda
|
@@ -114,6 +114,8 @@ files:
|
|
114
114
|
- VERSION
|
115
115
|
- lib/ruby-development-toolbox.rb
|
116
116
|
- lib/toolbox/boolean.rb
|
117
|
+
- lib/toolbox/hash_diff.rb
|
118
|
+
- lib/toolbox/uuid.rb
|
117
119
|
- ruby-development-toolbox.gemspec
|
118
120
|
- test/helper.rb
|
119
121
|
- test/test_ruby-development-toolbox.rb
|