deep_compactor 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: 0144c33f7ae717ade589a35a30f2daeb86b43ed23832cea62cb44d8e85ea8e6f
4
+ data.tar.gz: 52c31d9f9abf6833f659d700ec9ef1df3e75d106226669f47e0324efa9d643d8
5
+ SHA512:
6
+ metadata.gz: c5b24adc8e32138f9020042296747942b5f4ded2e726890f307a18d6540195d948f535cf0fcee7d544e73c8dc0776cb67045a8556a8ab12b56b78f40fe8df5a2
7
+ data.tar.gz: a805929542938bb17fa43cafa90af1961bc2d44a5dc1a340a6549521d4ffa72d582d8f4d78eb974f531fc43bf2582fa374eb33ed982f384a86658670144c2d63
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,16 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
14
+
15
+ Metrics/BlockLength:
16
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [0.1.0] - 2024-01-09
2
+
3
+ - 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 38120991+yamashush@users.noreply.github.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/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 yamashush
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,79 @@
1
+ [![Gem Version](https://badge.fury.io/rb/deep_compactor.svg)](https://badge.fury.io/rb/deep_compactor)
2
+ [![Tests](https://github.com/yamashush/deep_compactor/actions/workflows/ci.yml/badge.svg)](https://github.com/yamashush/deep_compactor/actions/workflows/ci.yml)
3
+
4
+ # DeepCompactor
5
+
6
+ Add recursive compact methods for nested Array and Hash object.
7
+
8
+ - `#deep_compact`
9
+ - `#deep_compact!`
10
+ - `#deep_compact_blank`
11
+ - `#deep_compact_blank!`
12
+
13
+ ## Installation
14
+
15
+ Install the gem and add to the application's Gemfile by executing:
16
+
17
+ $ bundle add deep_compactor
18
+
19
+ If bundler is not being used to manage dependencies, install the gem by executing:
20
+
21
+ $ gem install deep_compactor
22
+
23
+ ## Usage
24
+
25
+ ### `#deep_compact` `#deep_compact!`
26
+
27
+ ```ruby
28
+ using DeepCompactor
29
+
30
+ array = ["a", nil, ["aa", nil], { aa: 11, bb: nil }]
31
+
32
+ array.compact
33
+ #=> ["a", ["aa", nil], { aa: 11, bb: nil }]
34
+
35
+ array.deep_compact
36
+ #=> ["a", ["aa"], { aa: 11 }]
37
+
38
+ array.deep_compact!
39
+ #=> ["a", ["aa"], { aa: 11 }]
40
+ array
41
+ #=> ["a", ["aa"], { aa: 11 }]
42
+ ```
43
+
44
+ ### `#deep_compact_blank` `#deep_compact_blank!`
45
+
46
+ ```ruby
47
+ using DeepCompactor
48
+
49
+ hash = { a: 1, b: nil, c: [], d: {}, e: ["aa", nil, [], {}], f: { aa: 11, bb: nil, cc: [], dd: {} }}
50
+
51
+ hash.compact_blank # required activesupport
52
+ #=> { a: 1, e: ["aa", nil, [], {}], f: { aa: 11, bb: nil, cc: [], dd: {}}}
53
+
54
+ hash.deep_compact_blank
55
+ #=> { a: 1, e: ["aa"], f: { aa: 11 }}
56
+
57
+ hash.deep_compact_blank!
58
+ #=> { a: 1, e: ["aa"], f: { aa: 11 }}
59
+ hash
60
+ #=> { a: 1, e: ["aa"], f: { aa: 11 }}
61
+ ```
62
+
63
+ ## Development
64
+
65
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
66
+
67
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
68
+
69
+ ## Contributing
70
+
71
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/deep_compactor. 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]/deep_compactor/blob/main/CODE_OF_CONDUCT.md).
72
+
73
+ ## License
74
+
75
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
76
+
77
+ ## Code of Conduct
78
+
79
+ Everyone interacting in the DeepCompactor project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/deep_compactor/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeepCompactor
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "deep_compactor/version"
4
+
5
+ # For deep compact nested Array and Hash.
6
+ module DeepCompactor
7
+ refine Array do
8
+ # @return [Array]
9
+ # @see https://docs.ruby-lang.org/en/master/Array.html#method-i-compact
10
+ def deep_compact
11
+ map do |value|
12
+ if value.is_a?(Array) || value.is_a?(Hash)
13
+ value.deep_compact
14
+ else
15
+ value
16
+ end
17
+ end.compact
18
+ end
19
+
20
+ # @return [self, nil]
21
+ # @see https://docs.ruby-lang.org/en/master/Array.html#method-i-compact-21
22
+ def deep_compact!
23
+ compacted = deep_compact
24
+
25
+ if self == compacted
26
+ nil
27
+ else
28
+ replace(compacted)
29
+ end
30
+ end
31
+
32
+ # @return [Array]
33
+ def deep_compact_blank
34
+ map do |value|
35
+ if value.is_a?(Array) || value.is_a?(Hash)
36
+ value.empty? ? nil : value.deep_compact_blank
37
+ else
38
+ value
39
+ end
40
+ end.compact
41
+ end
42
+
43
+ # @return [self, nil]
44
+ def deep_compact_blank!
45
+ compacted = deep_compact_blank
46
+
47
+ if self == compacted
48
+ nil
49
+ else
50
+ replace(compacted)
51
+ end
52
+ end
53
+ end
54
+
55
+ refine Hash do
56
+ # @return [Hash]
57
+ # @see https://docs.ruby-lang.org/en/master/Hash.html#method-i-compact
58
+ def deep_compact
59
+ transform_values do |value|
60
+ if value.is_a?(Array) || value.is_a?(Hash)
61
+ value.deep_compact
62
+ else
63
+ value
64
+ end
65
+ end.compact
66
+ end
67
+
68
+ # @return [self, nil]
69
+ # @see https://docs.ruby-lang.org/en/master/Hash.html#method-i-compact-21
70
+ def deep_compact!
71
+ compacted = deep_compact
72
+
73
+ if self == compacted
74
+ nil
75
+ else
76
+ replace(compacted)
77
+ end
78
+ end
79
+
80
+ # @return [Hash]
81
+ def deep_compact_blank
82
+ transform_values do |value|
83
+ if value.is_a?(Array) || value.is_a?(Hash)
84
+ value.empty? ? nil : value.deep_compact_blank
85
+ else
86
+ value
87
+ end
88
+ end.compact
89
+ end
90
+
91
+ # @return [self, nil]
92
+ def deep_compact_blank!
93
+ compacted = deep_compact_blank
94
+
95
+ if self == compacted
96
+ nil
97
+ else
98
+ replace(compacted)
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,4 @@
1
+ module DeepCompactor
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deep_compactor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - yamashush
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-01-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Deep Compactor is a gem to deep compact nested Array and Hash.
14
+ email:
15
+ - 38120991+yamashush@users.noreply.github.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - CODE_OF_CONDUCT.md
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/deep_compactor.rb
28
+ - lib/deep_compactor/version.rb
29
+ - sig/deep_compactor.rbs
30
+ homepage: https://github.com/yamashush/deep_compactor
31
+ licenses:
32
+ - MIT
33
+ metadata:
34
+ allowed_push_host: https://rubygems.org
35
+ homepage_uri: https://github.com/yamashush/deep_compactor
36
+ source_code_uri: https://github.com/yamashush/deep_compactor
37
+ changelog_uri: https://github.com/yamashush/deep_compactor/blob/main/CHANGELOG.md
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 3.0.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.5.3
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Deep Compactor is a gem to deep compact nested Array and Hash.
57
+ test_files: []