deeper_hash 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5d1ef6b02dd166070be15cb75cad60aa6a613d310f1e64638b9bace1c540f7f1
4
+ data.tar.gz: 4cad788494e23a76d79dce3d0e5e437cb58bfad322acfdd066d0a4eee792e7bb
5
+ SHA512:
6
+ metadata.gz: 58f67f87bfa15f38274caed83a85af1a8f688f6b2ef24d0d2e37cc27bd6598bbf6b6977fd978950178c4a8e6f5e2d3ddea046226e85ab74d521c35316e7e81a5
7
+ data.tar.gz: ae2664777bfcb82adbc83f5053792810d2448f83e861aae5126d96af2ab8cd4a0d1a0a98d7af77e41a9550c7bf34200a66fb313ca0b9157ef7cc9e301c0b8d47
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-10-04
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at dominik.sito@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in deephash.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ deeper_hash (0.1.0)
5
+ ttyhue (~> 0.1.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ansi (1.5.0)
11
+ builder (3.2.4)
12
+ minitest (5.16.3)
13
+ minitest-reporters (1.5.0)
14
+ ansi
15
+ builder
16
+ minitest (>= 5.0)
17
+ ruby-progressbar
18
+ rake (13.0.6)
19
+ ruby-progressbar (1.11.0)
20
+ shoulda-context (2.0.0)
21
+ ttyhue (0.1.2)
22
+
23
+ PLATFORMS
24
+ x86_64-linux
25
+
26
+ DEPENDENCIES
27
+ deeper_hash!
28
+ minitest (= 5.16.3)
29
+ minitest-reporters (= 1.5.0)
30
+ rake (~> 13.0)
31
+ shoulda-context (= 2.0.0)
32
+
33
+ BUNDLED WITH
34
+ 2.3.22
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Dominik Sito
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,336 @@
1
+ # DeepHash
2
+
3
+ Set of utility methods for vanilia ruby `Hash` providing a way of handling, transforming, diffing it in a Tree-like manner.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add deeper_hash
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install deeper_hash
14
+
15
+ ## Usage
16
+
17
+ ```ruby
18
+ require 'deeper_hash'
19
+ ```
20
+
21
+ ## Methods
22
+
23
+ ### #deep_include?
24
+
25
+ Same as `Hash#include?` but fully recursive:
26
+
27
+ ```ruby
28
+ hash = {
29
+ a: 1,
30
+ b: {
31
+ c: 1,
32
+ d: 2
33
+ }
34
+ }
35
+
36
+ hash.deep_include?(a: 1, b: { c: 1 }) #=> true
37
+ hash.deep_include?(a: 1, b: { c: 2 }) #=> false
38
+
39
+ ```
40
+
41
+ ### #deep_merge
42
+
43
+ Adding or replacing both root and nested values:
44
+
45
+ ```ruby
46
+ hash = {
47
+ a: 1,
48
+ b: 2,
49
+ c: {
50
+ d: 1,
51
+ e: 2
52
+ }
53
+ }
54
+
55
+ hash.deep_merge(
56
+ b: 3,
57
+ c: {
58
+ e: 4,
59
+ f: 5
60
+ }
61
+ )
62
+ # #=> {
63
+ # a: 1,
64
+ # b: 3,
65
+ # c: {
66
+ # d: 1,
67
+ # e: 4,
68
+ # f: 5
69
+ # }
70
+ # }
71
+ ```
72
+
73
+ ### #deep_select
74
+
75
+ Filtering `Hash` based on key/value pairs with non-hash values:
76
+
77
+ ```ruby
78
+ hash = {
79
+ a: 1,
80
+ b: 10,
81
+ c: {
82
+ d: 11,
83
+ e: 2,
84
+ f: {
85
+ g: 12,
86
+ h: 4
87
+ },
88
+ i: {
89
+ j: 15
90
+ }
91
+ }
92
+ }
93
+
94
+ hash.deep_select { |k,v| [:a, :e, :h].include?(k) }
95
+ # #=> {
96
+ # a: 1,
97
+ # c: {
98
+ # e: 2,
99
+ # f: {
100
+ # h: 4
101
+ # }
102
+ # }
103
+ # }
104
+
105
+ hash.deep_select { |k,v| v > 9 }
106
+ # #=> {
107
+ # b: 10,
108
+ # c: {
109
+ # d: 11,
110
+ # f: {
111
+ # g: 12
112
+ # },
113
+ # i: {
114
+ # j: 15
115
+ # }
116
+ # }
117
+ # }
118
+ ```
119
+
120
+ ### #deep_reject
121
+
122
+ Inverse of `#deep_select`
123
+
124
+ ### #deep_transform_keys
125
+
126
+ Same as `Hash#transform_keys` but fully recursive:
127
+
128
+ ```ruby
129
+ hash = {
130
+ a: 1,
131
+ b: {
132
+ c: 2,
133
+ d: 3
134
+ }
135
+ }
136
+
137
+ hash.deep_transform_keys { |k| "prefix_#{k}".to_sym }
138
+ # #=> {
139
+ # prefix_a: 1,
140
+ # prefix_b: {
141
+ # prefix_c: 2,
142
+ # prefix_d: 3
143
+ # }
144
+ # }
145
+ ```
146
+
147
+ ### #deep_transform_values
148
+
149
+ Same as `Hash#transform_values` but fully recursive:
150
+
151
+ ```ruby
152
+ hash = {
153
+ a: 1,
154
+ b: {
155
+ c: 2,
156
+ d: 3
157
+ }
158
+ }
159
+
160
+ hash.deep_transform_values { |k,v| v + 10 }
161
+ # #=> {
162
+ # a: 10,
163
+ # b: {
164
+ # c: 12,
165
+ # d: 13
166
+ # }
167
+ # }
168
+ ```
169
+
170
+ ## Diffing
171
+
172
+ ### #diff
173
+
174
+ Presents difference between two hashes at any level returning `Hash` object with all changes.
175
+
176
+ ### #draw_diff
177
+
178
+ Returns the diff as git-like colored string.
179
+
180
+ ```ruby
181
+ hash1 = {
182
+ foo: "bar",
183
+ fiz: "biz",
184
+ fez: "cez",
185
+ a: {
186
+ b: 10,
187
+ c: 2
188
+ }
189
+ }
190
+
191
+ hash2 = {
192
+ foo: "bar",
193
+ fiz: "biz",
194
+ faz: "caz",
195
+ a: {
196
+ b: 1,
197
+ c: 2,
198
+ d: 12
199
+ }
200
+ }
201
+
202
+ hash1.diff(hash2)
203
+ # #=> {
204
+ # :removed_key=>[[:fez, "cez"]],
205
+ # :added_key=>[[:faz, "caz"]],
206
+ # :a=>{
207
+ # :b=>{ :updated_val=>{:from=>10, :to=>1} },
208
+ # :added_key=>[[:d, 12]]
209
+ # }
210
+ # }
211
+
212
+ puts hash1.draw_diff(hash2)
213
+ ```
214
+ ![screenshot](https://raw.githubusercontent.com/railis/deeper_hash/master/examples/example1.png)
215
+
216
+ Also works with `Array` values:
217
+
218
+ ```ruby
219
+ hash1 = {
220
+ foo: "bar",
221
+ fiz: [1, 2, 3]
222
+ }
223
+
224
+ hash2 = {
225
+ foo: "bar",
226
+ fiz: [2, 2, 3, 4]
227
+ }
228
+
229
+ hash1.diff(hash2)
230
+ # #=> {
231
+ # :fiz=>{
232
+ # :updated_arr=>{
233
+ # :arr=>[1, 2, 3],
234
+ # :appended=>[4],
235
+ # :changed_el=>[{:from=>1, :to=>2, :index=>0}]
236
+ # }
237
+ # }
238
+ # }
239
+
240
+ puts hash1.draw_diff(hash2)
241
+ ```
242
+ ![screenshot](https://raw.githubusercontent.com/railis/deeper_hash/master/examples/example2.png)
243
+
244
+ The diff can traverse across entire `Hash` until reaching deepest level. Eg: `Array` values with `Hash` elements, containing other `Arrays` and `Hashes`:
245
+
246
+ ```ruby
247
+ hash1 = {
248
+ foo: "bar",
249
+ fiz: [
250
+ 1,
251
+ {
252
+ a: 1,
253
+ b: [
254
+ "alpha",
255
+ "beta"
256
+ ]
257
+ },
258
+ 3
259
+ ]
260
+ }
261
+
262
+ hash2 = {
263
+ foo: "bar",
264
+ fiz: [
265
+ 2,
266
+ {
267
+ a: 2,
268
+ b: [
269
+ "alpha",
270
+ "betav2",
271
+ "gamma"
272
+ ]
273
+ },
274
+ 3
275
+ ]
276
+ }
277
+
278
+ hash1.diff(hash2)
279
+ # #=> {
280
+ # :fiz=>{
281
+ # :updated_arr=>{
282
+ # :arr=>[1,{:a=>1,:b=>["alpha","beta"]}, 3],
283
+ # :changed_el=>[
284
+ # {
285
+ # :from=>1,
286
+ # :to=>2,
287
+ # :index=>0
288
+ # },
289
+ # {
290
+ # :updated_hash=>{
291
+ # :a=>{:updated_val=>{:from=>1, :to=>2}},
292
+ # :b=>{
293
+ # :updated_arr=>{
294
+ # :arr=>["alpha", "beta"],
295
+ # :appended=>["gamma"],
296
+ # :changed_el=>[
297
+ # {:from=>"beta", :to=>"betav2", :index=>1}
298
+ # ]
299
+ # }
300
+ # }
301
+ # },
302
+ # :index=>1
303
+ # }
304
+ # ]
305
+ # }
306
+ # }
307
+ # }
308
+
309
+ puts hash1.draw_diff(hash2)
310
+ ```
311
+ ![screenshot](https://raw.githubusercontent.com/railis/deeper_hash/master/examples/example3.png)
312
+
313
+ Diff output color can be formatted by overwriting styles: `content` `added` `removed` `changed`, eg:
314
+ ```ruby
315
+ puts hash1.draw_diff(hash2, content: { fg: :white }, added: { fg: :lblue })
316
+ ```
317
+
318
+ For more information about using styles and available color options, see [TTYHue](https://github.com/railis/ttyhue) ruby gem.
319
+
320
+ ## Development
321
+
322
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
323
+
324
+ To install this gem onto your local machine, run `bundle exec rake install`.
325
+
326
+ ## Contributing
327
+
328
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/railis/deeper_hash). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/deephash/blob/master/CODE_OF_CONDUCT.md).
329
+
330
+ ## License
331
+
332
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
333
+
334
+ ## Code of Conduct
335
+
336
+ Everyone interacting in the Deephash project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/deeper_hash/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rake/testtask"
4
+ require "bundler/gem_tasks"
5
+
6
+ task default: %i[]
7
+
8
+ Rake::TestTask.new do |task|
9
+ task.pattern = "test/*_test.rb"
10
+ end
data/deephash.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/deeper_hash/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "deeper_hash"
7
+ spec.version = DeeperHash::VERSION
8
+ spec.authors = ["Dominik Sito"]
9
+ spec.email = ["dominik.sito@gmail.com"]
10
+
11
+ spec.summary = "Set of utility methods for vanilia ruby Hash providing a way of handling, transforming, diffing it in a Tree-like manner."
12
+ spec.description = "Set of utility methods for vanilia ruby Hash providing a way of handling, transforming, diffing it in a Tree-like manner."
13
+ spec.homepage = "https://github.com/railis/deephash"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = "https://raw.githubusercontent.com/railis/deephash/master/CHANGELOG.md"
20
+
21
+ spec.files = Dir.chdir(__dir__) do
22
+ `git ls-files -z`.split("\x0").reject do |f|
23
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
24
+ end
25
+ end
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_dependency "ttyhue", "~> 0.1.2"
29
+ spec.add_development_dependency "minitest", "5.16.3"
30
+ spec.add_development_dependency "minitest-reporters", "1.5.0"
31
+ spec.add_development_dependency "shoulda-context", "2.0.0"
32
+ end
Binary file
Binary file
Binary file
@@ -0,0 +1,134 @@
1
+ require "ttyhue"
2
+
3
+ module DeeperHash
4
+ class ColorDiff
5
+
6
+ INDENT_SIZE = 2
7
+
8
+ def initialize(h1, h2)
9
+ @diff = Diff.new(h1, h2).diff
10
+ end
11
+
12
+ def draw(color_opts = {})
13
+ @color_opts = {
14
+ added: { fg: :gui114 },
15
+ removed: { fg: :gui161 },
16
+ changed: { fg: :gui105 },
17
+ content: { fg: :gui241 }
18
+ }.merge(color_opts)
19
+ @use_indent = true
20
+ @indent = 0
21
+ @result_str = ""
22
+ draw_hash_diff(@diff)
23
+ @indent = 0
24
+ @result_str
25
+ end
26
+
27
+ private
28
+
29
+ def draw_added_key(k, v)
30
+ @result_str << color_f("{added}#{k}: #{v.inspect}\n", :add)
31
+ end
32
+
33
+ def draw_removed_key(k, v)
34
+ @result_str << color_f("{removed}#{k}: #{v.inspect}\n", :delete)
35
+ end
36
+
37
+ def draw_updated_value(from, to)
38
+ @result_str << color_f("{removed}#{from.inspect}{/removed} {changed}->{/changed} {added}#{to.inspect}{/added}\n", :change)
39
+ end
40
+
41
+ def draw_hash_diff(diff)
42
+ dup_diff = diff.dup
43
+ deleted = dup_diff.delete(:removed_key)
44
+ added = dup_diff.delete(:added_key)
45
+ if changed = dup_diff.delete(:updated_val)
46
+ draw_updated_value(changed[:from], changed[:to])
47
+ @use_indent = true
48
+ end
49
+ if updated_arr = dup_diff.delete(:updated_arr)
50
+ draw_array_diff(updated_arr)
51
+ end
52
+ unless @use_indent
53
+ @result_str << "\n"
54
+ @use_indent = true
55
+ end
56
+ dup_diff.each do |k, v|
57
+ if v[:updated_val] || v[:updated_arr]
58
+ @result_str << color_f("{content}#{k}: ", (:change if v[:updated_val]))
59
+ @use_indent = false
60
+ draw_hash_diff(v)
61
+ else
62
+ @result_str << color_f("{content}#{k}:\n")
63
+ @indent += 1
64
+ draw_hash_diff(v)
65
+ @indent -= 1
66
+ end
67
+ end
68
+ deleted.to_a.each do |k,v|
69
+ draw_removed_key(k, v)
70
+ end
71
+ added.to_a.each do |k,v|
72
+ draw_added_key(k, v)
73
+ end
74
+ end
75
+
76
+ def draw_array_diff(updated_arr)
77
+ base_arr =
78
+ if updated_arr[:detached]
79
+ updated_arr[:arr][0..-(updated_arr[:detached].size + 1)]
80
+ else
81
+ updated_arr[:arr]
82
+ end
83
+ @result_str << color_f("{content}[\n")
84
+ @use_indent = true
85
+ @indent += 1
86
+ base_arr.each_with_index do |e, idx|
87
+ change_on_index = updated_arr[:changed_el].to_a.select {|c| c[:index] == idx}.first
88
+ if change_on_index
89
+ if change_on_index[:updated_arr]
90
+ draw_array_diff(change_on_index[:updated_arr])
91
+ elsif change_on_index[:updated_hash]
92
+ @result_str << color_f("{content}{\n")
93
+ @indent += 1
94
+ draw_hash_diff(change_on_index[:updated_hash])
95
+ @indent -= 1
96
+ @result_str << color_f("{content}}\n")
97
+ else
98
+ draw_updated_value(change_on_index[:from], change_on_index[:to])
99
+ end
100
+ else
101
+ @result_str << color_f("{content}#{e.inspect}\n")
102
+ end
103
+ end
104
+ updated_arr[:detached].to_a.each { |e| @result_str << color_f("{removed}#{e.inspect}\n", :delete) }
105
+ updated_arr[:appended].to_a.each { |e| @result_str << color_f("{added}#{e.inspect}\n", :add) }
106
+ @indent -= 1
107
+ @result_str << color_f("{content}]\n")
108
+ end
109
+
110
+ def color_f(str, icon = nil)
111
+ icon_str =
112
+ case icon
113
+ when :add
114
+ colorize("{added}+ ")
115
+ when :delete
116
+ colorize("{removed}- ")
117
+ when :change
118
+ colorize("{changed}~ ")
119
+ else
120
+ colorize("{content}. ")
121
+ end
122
+ if @use_indent
123
+ icon_str + (" " * (@indent.to_i * INDENT_SIZE)) + colorize(str)
124
+ else
125
+ colorize(str)
126
+ end
127
+ end
128
+
129
+ def colorize(str)
130
+ TTYHue.c(str, @color_opts)
131
+ end
132
+
133
+ end
134
+ end
@@ -0,0 +1,120 @@
1
+ require "ttyhue"
2
+
3
+ module DeeperHash
4
+ class Diff
5
+ class Stack
6
+ def initialize
7
+ @stack = []
8
+ end
9
+
10
+ def push(value)
11
+ @stack << value
12
+ end
13
+
14
+ def pop
15
+ return if @stack.empty?
16
+
17
+ @stack = @stack[0..-2]
18
+ end
19
+
20
+ def to_a
21
+ @stack
22
+ end
23
+ end
24
+
25
+ def initialize(h1, h2)
26
+ @h1 = h1
27
+ @h2 = h2
28
+ end
29
+
30
+ def diff
31
+ @diff_stack = Stack.new
32
+ @result = {}
33
+ @attr_change_log = []
34
+ process_diff(@h1, @h2)
35
+ @result
36
+ end
37
+
38
+ def process_diff(h1, h2)
39
+ process_diff_pair(h1, h2, :removed_key)
40
+ process_diff_pair(h2, h1, :added_key)
41
+ end
42
+
43
+ def process_diff_pair(h1, h2, action)
44
+ h1.each do |k,v|
45
+ next if attribute_change_logged?(k)
46
+
47
+ if h2[k]
48
+ if v != h2[k]
49
+ if v.is_a?(Hash) && h2[k].is_a?(Hash)
50
+ @diff_stack.push(k)
51
+ process_diff(v, h2[k])
52
+ @diff_stack.pop
53
+ else
54
+ if v.is_a?(Array) && h2[k].is_a?(Array)
55
+ @result.deep_set(arr_diff(v, h2[k]), *(@diff_stack.to_a + [k, :updated_arr]))
56
+ else
57
+ @result.deep_set({from: v, to: h2[k]}, *(@diff_stack.to_a + [k, :updated_val]))
58
+ end
59
+ log_attribute_change(k)
60
+ end
61
+ end
62
+ else
63
+ dir = @diff_stack.to_a + [action]
64
+ val = (@result.dig(*dir).is_a?(Array) ? @result.dig(*dir) : [])
65
+ val << [k, v]
66
+ @result.deep_set(val, *dir)
67
+ log_attribute_change(k)
68
+ end
69
+ end
70
+ end
71
+
72
+ def arr_diff(a1, a2)
73
+ if a1.size == a2.size
74
+ common_1 = a1; common_2 = a2; appended = []; detached = []
75
+ elsif a1.size > a2.size
76
+ if a2.size == 0
77
+ common_1 = []; common_2 = []; appended = []; detached = a1
78
+ else
79
+ common_1 = a1[0..(a2.size-1)]; common_2 = a2; appended = []; detached = a1[a2.size..-1]
80
+ end
81
+ else
82
+ if a1.size == 0
83
+ common_1 = []; common_2 = []; appended = a2; detached = []
84
+ else
85
+ common_1 = a1; common_2 = a2[0..(a1.size-1)]; appended = a2[a1.size..-1]; detached = []
86
+ end
87
+ end
88
+ {}.tap do |res|
89
+ res[:arr] = a1
90
+ res[:appended] = appended unless appended.empty?
91
+ res[:detached] = detached unless detached.empty?
92
+ unless common_1.empty?
93
+ common_1.each_with_index do |v, i|
94
+ val_1 = v
95
+ val_2 = common_2[i]
96
+ if val_1 != val_2
97
+ res[:changed_el] ||= []
98
+ if val_1.is_a?(Array) && val_2.is_a?(Array)
99
+ res[:changed_el] << {updated_arr: arr_diff(val_1, val_2), index: i}
100
+ elsif val_1.is_a?(Hash) && val_2.is_a?(Hash)
101
+ res[:changed_el] << {updated_hash: Diff.new(val_1, val_2).diff, index: i}
102
+ else
103
+ res[:changed_el] << {from: val_1, to: val_2, index: i}
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
110
+
111
+ def log_attribute_change(attr)
112
+ @attr_change_log << @diff_stack.to_a + [attr]
113
+ end
114
+
115
+ def attribute_change_logged?(attr)
116
+ @attr_change_log.include?(@diff_stack.to_a + [attr])
117
+ end
118
+
119
+ end
120
+ end
@@ -0,0 +1,84 @@
1
+ module DeeperHash
2
+ class Meta
3
+
4
+ class << self
5
+
6
+ def transform_keys(hash, &block)
7
+ transform(hash, include_hash_values: true) do |k, v, h|
8
+ h[block.call(k)] = v
9
+ end
10
+ end
11
+
12
+ def transform_values(hash, &block)
13
+ transform(hash) do |k, v, h|
14
+ h[k] = block.call(v) unless v.is_a?(Hash)
15
+ end
16
+ end
17
+
18
+ def select(hash, &block)
19
+ transform(hash) do |k, v, h|
20
+ h[k] = v if block.call(k,v)
21
+ end
22
+ end
23
+
24
+ def reject(hash, &block)
25
+ transform(hash) do |k, v, h|
26
+ h[k] = v unless block.call(k,v)
27
+ end
28
+ end
29
+
30
+ def merge(hash, other_hash)
31
+ hash.dup.tap do |h|
32
+ other_hash.each do |k, v|
33
+ if h[k].is_a?(Hash) && v.is_a?(Hash)
34
+ h[k] = merge(h[k], v)
35
+ else
36
+ h[k] = v
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ def include?(hash, other_hash)
43
+ return true if hash == other_hash
44
+
45
+ other_hash.each do |k, v|
46
+ if hash[k].is_a?(Hash) && v.is_a?(Hash)
47
+ return false unless include?(hash[k], v)
48
+ else
49
+ return false unless hash[k] == v
50
+ end
51
+ end
52
+ true
53
+ end
54
+
55
+ def set(hash, value, keys)
56
+ keys[0..-2].inject(hash) do |acc, e|
57
+ acc[e] ||= {}
58
+ end[keys.last] = value
59
+ end
60
+
61
+ private
62
+
63
+ def transform(hash, opts = {}, &block)
64
+ {}.tap do |new_hash|
65
+ hash.each do |k,v|
66
+ case v
67
+ when Hash
68
+ nested_hash = transform(v, opts, &block)
69
+ if opts[:include_hash_values]
70
+ block.call(k, nested_hash, new_hash)
71
+ else
72
+ new_hash[k] = nested_hash unless nested_hash.empty?
73
+ end
74
+ else
75
+ block.call(k, v, new_hash)
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeeperHash
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "deeper_hash/version"
4
+ require_relative "deeper_hash/meta"
5
+ require_relative "deeper_hash/diff"
6
+ require_relative "deeper_hash/color_diff"
7
+
8
+ module DeeperHash
9
+
10
+ %w[
11
+ transform_keys
12
+ transform_values
13
+ select
14
+ reject
15
+ ].each do |method|
16
+ define_method "deep_#{method}" do |&block|
17
+ Meta.send(method, self, &block)
18
+ end
19
+ end
20
+
21
+ %w[
22
+ merge
23
+ include?
24
+ ].each do |method|
25
+ define_method "deep_#{method}" do |value|
26
+ Meta.send(method, self, value)
27
+ end
28
+ end
29
+
30
+ def deep_set(value, *keys)
31
+ Meta.set(self, value, keys)
32
+ end
33
+
34
+ def diff(other_hash)
35
+ Diff.new(self, other_hash).diff
36
+ end
37
+
38
+ def draw_diff(other_hash, styles = {})
39
+ ColorDiff.new(self, other_hash).draw(styles)
40
+ end
41
+
42
+ end
43
+
44
+ Hash.send :include, DeeperHash
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deeper_hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dominik Sito
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-10-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ttyhue
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 5.16.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 5.16.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest-reporters
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.5.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.5.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: shoulda-context
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 2.0.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 2.0.0
69
+ description: Set of utility methods for vanilia ruby Hash providing a way of handling,
70
+ transforming, diffing it in a Tree-like manner.
71
+ email:
72
+ - dominik.sito@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - CHANGELOG.md
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - deephash.gemspec
85
+ - examples/example1.png
86
+ - examples/example2.png
87
+ - examples/example3.png
88
+ - lib/deeper_hash.rb
89
+ - lib/deeper_hash/color_diff.rb
90
+ - lib/deeper_hash/diff.rb
91
+ - lib/deeper_hash/meta.rb
92
+ - lib/deeper_hash/version.rb
93
+ homepage: https://github.com/railis/deephash
94
+ licenses:
95
+ - MIT
96
+ metadata:
97
+ homepage_uri: https://github.com/railis/deephash
98
+ source_code_uri: https://github.com/railis/deephash
99
+ changelog_uri: https://raw.githubusercontent.com/railis/deephash/master/CHANGELOG.md
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: 2.6.0
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubygems_version: 3.3.22
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Set of utility methods for vanilia ruby Hash providing a way of handling,
119
+ transforming, diffing it in a Tree-like manner.
120
+ test_files: []