hash_tools 1.2.2 → 1.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.rspec +2 -0
- data/.rubocop.yml +45 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +5 -6
- data/Gemfile.lock +56 -0
- data/LICENSE.txt +33 -20
- data/Rakefile +7 -48
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/hash_tools/indifferent.rb +139 -127
- data/lib/hash_tools/version.rb +5 -0
- data/lib/hash_tools.rb +29 -25
- metadata +45 -36
- data/.document +0 -5
- data/.travis.yml +0 -6
- data/.yardopts +0 -1
- data/hash_tools.gemspec +0 -60
- data/spec/hash_tools/indifferent_spec.rb +0 -90
- data/spec/hash_tools_spec.rb +0 -171
- data/spec/spec_helper.rb +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1a4f7b806a49427383728ff56570b2e6cd56d8844e226db876bca92660f58d6c
|
4
|
+
data.tar.gz: 74a2f8dfa29bf836a8937e8c4f7c6f5f80c4f8e3c8f342d4b590325cdae35106
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 50e218df3684fe58283f1dce82987de5944aeef43b79f8722f60994c5af296c16c0a755debe731d085fdac887bc17987192db82c0a8295e549ade05ee94b10aa
|
7
|
+
data.tar.gz: 684d57fe8aec73f49a828115dcbfe154661c2648cc9b2a4b5dc393bc0dd0a15dbb3042617b4c1c0d0e374f385df78e3725d06a81993c2fafb14d298371a0418e
|
data/.rspec
CHANGED
data/.rubocop.yml
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
AllCops:
|
2
|
+
NewCops: disable
|
3
|
+
TargetRubyVersion: 2.6
|
4
|
+
|
5
|
+
Layout/ArgumentAlignment:
|
6
|
+
EnforcedStyle: with_fixed_indentation
|
7
|
+
|
8
|
+
Layout/MultilineMethodCallIndentation:
|
9
|
+
EnforcedStyle: indented
|
10
|
+
|
11
|
+
Layout/LineLength:
|
12
|
+
Max: 120
|
13
|
+
Severity: convention
|
14
|
+
|
15
|
+
Lint/AmbiguousBlockAssociation:
|
16
|
+
Exclude:
|
17
|
+
- "spec/**/*"
|
18
|
+
|
19
|
+
Metrics/BlockLength:
|
20
|
+
Exclude:
|
21
|
+
- "spec/**/*"
|
22
|
+
|
23
|
+
Metrics/MethodLength:
|
24
|
+
Max: 50
|
25
|
+
|
26
|
+
Style/Documentation:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
Style/FrozenStringLiteralComment:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
Style/IfUnlessModifier:
|
33
|
+
Enabled: false
|
34
|
+
|
35
|
+
Style/StringLiterals:
|
36
|
+
EnforcedStyle: double_quotes
|
37
|
+
|
38
|
+
Style/GuardClause:
|
39
|
+
Enabled: false
|
40
|
+
|
41
|
+
Style/ModuleFunction:
|
42
|
+
Enabled: false
|
43
|
+
|
44
|
+
Lint/MissingSuper:
|
45
|
+
Enabled: false
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -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 gerard@wetransfer.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
CHANGED
@@ -1,7 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
end
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in hash_tools.gemspec
|
6
|
+
gemspec
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
hash_tools (1.2.4)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
diff-lcs (1.5.0)
|
11
|
+
parallel (1.21.0)
|
12
|
+
parser (3.1.0.0)
|
13
|
+
ast (~> 2.4.1)
|
14
|
+
rainbow (3.1.1)
|
15
|
+
rake (13.0.6)
|
16
|
+
regexp_parser (2.2.0)
|
17
|
+
rexml (3.2.5)
|
18
|
+
rspec (3.10.0)
|
19
|
+
rspec-core (~> 3.10.0)
|
20
|
+
rspec-expectations (~> 3.10.0)
|
21
|
+
rspec-mocks (~> 3.10.0)
|
22
|
+
rspec-core (3.10.1)
|
23
|
+
rspec-support (~> 3.10.0)
|
24
|
+
rspec-expectations (3.10.2)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.10.0)
|
27
|
+
rspec-mocks (3.10.2)
|
28
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
+
rspec-support (~> 3.10.0)
|
30
|
+
rspec-support (3.10.3)
|
31
|
+
rubocop (1.25.0)
|
32
|
+
parallel (~> 1.10)
|
33
|
+
parser (>= 3.1.0.0)
|
34
|
+
rainbow (>= 2.2.2, < 4.0)
|
35
|
+
regexp_parser (>= 1.8, < 3.0)
|
36
|
+
rexml
|
37
|
+
rubocop-ast (>= 1.15.1, < 2.0)
|
38
|
+
ruby-progressbar (~> 1.7)
|
39
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
40
|
+
rubocop-ast (1.15.1)
|
41
|
+
parser (>= 3.0.1.1)
|
42
|
+
ruby-progressbar (1.11.0)
|
43
|
+
unicode-display_width (2.1.0)
|
44
|
+
|
45
|
+
PLATFORMS
|
46
|
+
ruby
|
47
|
+
|
48
|
+
DEPENDENCIES
|
49
|
+
bundler (~> 2)
|
50
|
+
hash_tools!
|
51
|
+
rake (~> 13.0)
|
52
|
+
rspec (~> 3.0)
|
53
|
+
rubocop (~> 1.21)
|
54
|
+
|
55
|
+
BUNDLED WITH
|
56
|
+
2.3.4
|
data/LICENSE.txt
CHANGED
@@ -1,20 +1,33 @@
|
|
1
|
-
Copyright
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
the
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
1
|
+
Copyright 2022 WeTransfer
|
2
|
+
|
3
|
+
Hippocratic License Version Number: 2.1.
|
4
|
+
|
5
|
+
Purpose. The purpose of this License is for the Licensor named above to permit the Licensee (as defined below) broad permission, if consistent with Human Rights Laws and Human Rights Principles (as each is defined below), to use and work with the Software (as defined below) within the full scope of Licensor’s copyright and patent rights, if any, in the Software, while ensuring attribution and protecting the Licensor from liability.
|
6
|
+
|
7
|
+
Permission and Conditions. The Licensor grants permission by this license (“License”), free of charge, to the extent of Licensor’s rights under applicable copyright and patent law, to any person or entity (the “Licensee”) obtaining a copy of this software and associated documentation files (the “Software”), to do everything with the Software that would otherwise infringe (i) the Licensor’s copyright in the Software or (ii) any patent claims to the Software that the Licensor can license or becomes able to license, subject to all of the following terms and conditions:
|
8
|
+
|
9
|
+
- Acceptance. This License is automatically offered to every person and entity subject to its terms and conditions. Licensee accepts this License and agrees to its terms and conditions by taking any action with the Software that, absent this License, would infringe any intellectual property right held by Licensor.
|
10
|
+
|
11
|
+
- Notice. Licensee must ensure that everyone who gets a copy of any part of this Software from Licensee, with or without changes, also receives the License and the above copyright notice (and if included by the Licensor, patent, trademark and attribution notice). Licensee must cause any modified versions of the Software to carry prominent notices stating that Licensee changed the Software. For clarity, although Licensee is free to create modifications of the Software and distribute only the modified portion created by Licensee with additional or different terms, the portion of the Software not modified must be distributed pursuant to this License. If anyone notifies Licensee in writing that Licensee has not complied with this Notice section, Licensee can keep this License by taking all practical steps to comply within 30 days after the notice. If Licensee does not do so, Licensee’s License (and all rights licensed hereunder) shall end immediately.
|
12
|
+
|
13
|
+
- Compliance with Human Rights Principles and Human Rights Laws.
|
14
|
+
|
15
|
+
1. Human Rights Principles.
|
16
|
+
|
17
|
+
(a) Licensee is advised to consult the articles of the United Nations Universal Declaration of Human Rights and the United Nations Global Compact that define recognized principles of international human rights (the “Human Rights Principles”). Licensee shall use the Software in a manner consistent with Human Rights Principles.
|
18
|
+
|
19
|
+
(b) Unless the Licensor and Licensee agree otherwise, any dispute, controversy, or claim arising out of or relating to (i) Section 1(a) regarding Human Rights Principles, including the breach of Section 1(a), termination of this License for breach of the Human Rights Principles, or invalidity of Section 1(a) or (ii) a determination of whether any Law is consistent or in conflict with Human Rights Principles pursuant to Section 2, below, shall be settled by arbitration in accordance with the Hague Rules on Business and Human Rights Arbitration (the “Rules”); provided, however, that Licensee may elect not to participate in such arbitration, in which event this License (and all rights licensed hereunder) shall end immediately. The number of arbitrators shall be one unless the Rules require otherwise.
|
20
|
+
|
21
|
+
Unless both the Licensor and Licensee agree to the contrary: (1) All documents and information concerning the arbitration shall be public and may be disclosed by any party; (2) The repository referred to under Article 43 of the Rules shall make available to the public in a timely manner all documents concerning the arbitration which are communicated to it, including all submissions of the parties, all evidence admitted into the record of the proceedings, all transcripts or other recordings of hearings and all orders, decisions and awards of the arbitral tribunal, subject only to the arbitral tribunal's powers to take such measures as may be necessary to safeguard the integrity of the arbitral process pursuant to Articles 18, 33, 41 and 42 of the Rules; and (3) Article 26(6) of the Rules shall not apply.
|
22
|
+
|
23
|
+
2. Human Rights Laws. The Software shall not be used by any person or entity for any systems, activities, or other uses that violate any Human Rights Laws. “Human Rights Laws” means any applicable laws, regulations, or rules (collectively, “Laws”) that protect human, civil, labor, privacy, political, environmental, security, economic, due process, or similar rights; provided, however, that such Laws are consistent and not in conflict with Human Rights Principles (a dispute over the consistency or a conflict between Laws and Human Rights Principles shall be determined by arbitration as stated above). Where the Human Rights Laws of more than one jurisdiction are applicable or in conflict with respect to the use of the Software, the Human Rights Laws that are most protective of the individuals or groups harmed shall apply.
|
24
|
+
|
25
|
+
3. Indemnity. Licensee shall hold harmless and indemnify Licensor (and any other contributor) against all losses, damages, liabilities, deficiencies, claims, actions, judgments, settlements, interest, awards, penalties, fines, costs, or expenses of whatever kind, including Licensor’s reasonable attorneys’ fees, arising out of or relating to Licensee’s use of the Software in violation of Human Rights Laws or Human Rights Principles.
|
26
|
+
|
27
|
+
- Failure to Comply. Any failure of Licensee to act according to the terms and conditions of this License is both a breach of the License and an infringement of the intellectual property rights of the Licensor (subject to exceptions under Laws, e.g., fair use). In the event of a breach or infringement, the terms and conditions of this License may be enforced by Licensor under the Laws of any jurisdiction to which Licensee is subject. Licensee also agrees that the Licensor may enforce the terms and conditions of this License against Licensee through specific performance (or similar remedy under Laws) to the extent permitted by Laws. For clarity, except in the event of a breach of this License, infringement, or as otherwise stated in this License, Licensor may not terminate this License with Licensee.
|
28
|
+
|
29
|
+
- Enforceability and Interpretation. If any term or provision of this License is determined to be invalid, illegal, or unenforceable by a court of competent jurisdiction, then such invalidity, illegality, or unenforceability shall not affect any other term or provision of this License or invalidate or render unenforceable such term or provision in any other jurisdiction; provided, however, subject to a court modification pursuant to the immediately following sentence, if any term or provision of this License pertaining to Human Rights Laws or Human Rights Principles is deemed invalid, illegal, or unenforceable against Licensee by a court of competent jurisdiction, all rights in the Software granted to Licensee shall be deemed null and void as between Licensor and Licensee. Upon a determination that any term or provision is invalid, illegal, or unenforceable, to the extent permitted by Laws, the court may modify this License to affect the original purpose that the Software be used in compliance with Human Rights Principles and Human Rights Laws as closely as possible. The language in this License shall be interpreted as to its fair meaning and not strictly for or against any party.
|
30
|
+
|
31
|
+
- Disclaimer. TO THE FULL EXTENT ALLOWED BY LAW, THIS SOFTWARE COMES “AS IS,” WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED, AND LICENSOR AND ANY OTHER CONTRIBUTOR SHALL NOT BE LIABLE TO ANYONE FOR ANY DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THIS LICENSE, UNDER ANY KIND OF LEGAL CLAIM.
|
32
|
+
|
33
|
+
This Hippocratic License is an Ethical Source license (https://ethicalsource.dev) and is offered for use by licensors and licensees at their own risk, on an “AS IS” basis, and with no warranties express or implied, to the maximum extent permitted by Laws.
|
data/Rakefile
CHANGED
@@ -1,53 +1,12 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rspec/core/rake_task"
|
4
5
|
|
5
|
-
|
6
|
-
require 'bundler'
|
7
|
-
begin
|
8
|
-
Bundler.setup(:default, :development)
|
9
|
-
rescue Bundler::BundlerError => e
|
10
|
-
$stderr.puts e.message
|
11
|
-
$stderr.puts "Run `bundle install` to install missing gems"
|
12
|
-
exit e.status_code
|
13
|
-
end
|
14
|
-
require 'rake'
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
15
7
|
|
16
|
-
require
|
17
|
-
Jeweler::Tasks.new do |gem|
|
18
|
-
gem.version = HashTools::VERSION
|
19
|
-
# gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
|
20
|
-
gem.name = "hash_tools"
|
21
|
-
gem.homepage = "http://github.com/julik/hash_tools"
|
22
|
-
gem.license = "MIT"
|
23
|
-
gem.description = %Q{Do useful things to Ruby Hashes}
|
24
|
-
gem.summary = %Q{Do useful things to Ruby Hashes}
|
25
|
-
gem.email = "me@julik.nl"
|
26
|
-
gem.authors = ["Julik Tarkhanov"]
|
27
|
-
# dependencies defined in Gemfile
|
28
|
-
end
|
29
|
-
Jeweler::RubygemsDotOrgTasks.new
|
8
|
+
require "rubocop/rake_task"
|
30
9
|
|
31
|
-
|
32
|
-
require 'rspec/core/rake_task'
|
33
|
-
RSpec::Core::RakeTask.new(:spec) do |spec|
|
34
|
-
spec.pattern = FileList['spec/**/*_spec.rb']
|
35
|
-
end
|
10
|
+
RuboCop::RakeTask.new
|
36
11
|
|
37
|
-
|
38
|
-
task :simplecov do
|
39
|
-
ENV['COVERAGE'] = "true"
|
40
|
-
Rake::Task['spec'].execute
|
41
|
-
end
|
42
|
-
|
43
|
-
task :default => :spec
|
44
|
-
|
45
|
-
require 'rdoc/task'
|
46
|
-
Rake::RDocTask.new do |rdoc|
|
47
|
-
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
-
|
49
|
-
rdoc.rdoc_dir = 'rdoc'
|
50
|
-
rdoc.title = "hash_tools #{version}"
|
51
|
-
rdoc.rdoc_files.include('README*')
|
52
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
-
end
|
12
|
+
task default: %i[spec rubocop]
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "hash_tools"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -1,135 +1,147 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "delegate"
|
2
4
|
|
3
5
|
# A tiny version of HashWithIndifferentAccess. Works like a wrapper proxy around a Ruby Hash.
|
4
6
|
# Does not support all of the methods for a Ruby Hash object, but nevertheless can be useful
|
5
7
|
# for checking params and for working with parsed JSON.
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
# Get a value from the Hash, bu supplying a Symbol or a String
|
16
|
-
# Key presence is verified by first trying a Symbol, and then a String.
|
17
|
-
#
|
18
|
-
# @param k the key to fetch
|
19
|
-
# @return the value, wrapped in {Indifferent} if it is a Hash
|
20
|
-
def [](k)
|
21
|
-
v = __getobj__[__transform_key__(k)]
|
22
|
-
__rewrap__(v)
|
23
|
-
end
|
24
|
-
|
25
|
-
# Set a value in the Hash, bu supplying a Symbol or a String as a key.
|
26
|
-
# Key presence is verified by first trying a Symbol, and then a String.
|
27
|
-
#
|
28
|
-
# @param k the key to set
|
29
|
-
# @param v the value to set
|
30
|
-
# @return v
|
31
|
-
def []=(k, v)
|
32
|
-
__getobj__[ __transform_key__(k) ] = v
|
33
|
-
end
|
34
|
-
|
35
|
-
# Fetch a value, by supplying a Symbol or a String as a key.
|
36
|
-
# Key presence is verified by first trying a Symbol, and then a String.
|
37
|
-
#
|
38
|
-
# @param k the key to set
|
39
|
-
# @param blk the block for no value
|
40
|
-
# @return v
|
41
|
-
def fetch(k, &blk)
|
42
|
-
v = __getobj__.fetch( __transform_key__(k) , &blk)
|
43
|
-
__rewrap__(v)
|
44
|
-
end
|
45
|
-
|
46
|
-
# Get the keys of the Hash. The keys are returned as-is (both Symbols and Strings).
|
47
|
-
#
|
48
|
-
# @return [Array] an array of keys
|
49
|
-
def keys
|
50
|
-
__getobj__.keys.map{|k| __transform_key__(k) }
|
51
|
-
end
|
52
|
-
|
53
|
-
# Checks for key presence whether the key is a String or a Symbol
|
54
|
-
#
|
55
|
-
# @param k[String,Symbol] the key to check
|
56
|
-
def key?(k)
|
57
|
-
__getobj__.has_key?( __transform_key__(k))
|
58
|
-
end
|
59
|
-
|
60
|
-
# Checks if the value at the given key is non-empty
|
61
|
-
#
|
62
|
-
# @param k[String,Symbol] the key to check
|
63
|
-
def value_present?(k)
|
64
|
-
return false unless key?(k)
|
65
|
-
v = self[k]
|
66
|
-
return false unless v
|
67
|
-
return !v.to_s.empty?
|
68
|
-
end
|
69
|
-
|
70
|
-
# Yields each key - value pair of the indifferent.
|
71
|
-
# If the value is a Hash as well, that hash will be wrapped in an Indifferent before returning
|
72
|
-
def each(&blk)
|
73
|
-
__getobj__.each do |k, v|
|
74
|
-
blk.call([__transform_key__(k), __rewrap__(v)])
|
8
|
+
module HashTools
|
9
|
+
class Indifferent < SimpleDelegator
|
10
|
+
# Create a new Indifferent by supplying a Ruby Hash object to wrap. The Hash being
|
11
|
+
# wrapped is not going to be altered or copied.
|
12
|
+
#
|
13
|
+
# @param naked_hash [Hash] the Hash object to wrap with an Indifferent
|
14
|
+
def initialize(naked_hash)
|
15
|
+
__setobj__(naked_hash)
|
75
16
|
end
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
value =
|
84
|
-
|
17
|
+
|
18
|
+
# Get a value from the Hash, bu supplying a Symbol or a String
|
19
|
+
# Key presence is verified by first trying a Symbol, and then a String.
|
20
|
+
#
|
21
|
+
# @param k the key to fetch
|
22
|
+
# @return the value, wrapped in {Indifferent} if it is a Hash
|
23
|
+
def [](key)
|
24
|
+
value = __getobj__[__transform_key__(key)]
|
25
|
+
__rewrap__(value)
|
85
26
|
end
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
27
|
+
|
28
|
+
# Set a value in the Hash, bu supplying a Symbol or a String as a key.
|
29
|
+
# Key presence is verified by first trying a Symbol, and then a String.
|
30
|
+
#
|
31
|
+
# @param k the key to set
|
32
|
+
# @param v the value to set
|
33
|
+
# @return v
|
34
|
+
def []=(key, value)
|
35
|
+
__getobj__[__transform_key__(key)] = value
|
36
|
+
end
|
37
|
+
|
38
|
+
# Fetch a value, by supplying a Symbol or a String as a key.
|
39
|
+
# Key presence is verified by first trying a Symbol, and then a String.
|
40
|
+
#
|
41
|
+
# @param k the key to set
|
42
|
+
# @param blk the block for no value
|
43
|
+
# @return v
|
44
|
+
def fetch(key, &blk)
|
45
|
+
value = __getobj__.fetch(__transform_key__(key), &blk)
|
46
|
+
__rewrap__(value)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Get the keys of the Hash. The keys are returned as-is (both Symbols and Strings).
|
50
|
+
#
|
51
|
+
# @return [Array] an array of keys
|
52
|
+
def keys
|
53
|
+
__getobj__.keys.map { |key| __transform_key__(key) }
|
54
|
+
end
|
55
|
+
|
56
|
+
# Checks for key presence whether the key is a String or a Symbol
|
57
|
+
#
|
58
|
+
# @param k[String,Symbol] the key to check
|
59
|
+
def key?(key)
|
60
|
+
__getobj__.key?(__transform_key__(key))
|
61
|
+
end
|
62
|
+
|
63
|
+
# Checks if the value at the given key is non-empty
|
64
|
+
#
|
65
|
+
# @param k[String,Symbol] the key to check
|
66
|
+
def value_present?(key)
|
67
|
+
return false unless key?(key)
|
68
|
+
|
69
|
+
value = self[key]
|
70
|
+
return false unless value
|
71
|
+
|
72
|
+
!value.to_s.empty?
|
73
|
+
end
|
74
|
+
|
75
|
+
# Yields each key - value pair of the indifferent.
|
76
|
+
# If the value is a Hash as well, that hash will be wrapped in an Indifferent before returning
|
77
|
+
def each(&blk)
|
78
|
+
__getobj__.each do |key, value|
|
79
|
+
blk.call([__transform_key__(key), __rewrap__(value)])
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# Yields each key - value pair of the indifferent.
|
84
|
+
# If the value is a Hash as well, that hash will be wrapped in an Indifferent before returning
|
85
|
+
def each_pair
|
86
|
+
object = __getobj__
|
87
|
+
keys.each do |key|
|
88
|
+
value = object[__transform_key__(key)]
|
89
|
+
yield(key, __rewrap__(value))
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# Maps over keys and values of the Hash. The key class will be preserved (i.e. within
|
94
|
+
# the block the keys will be either Strings or Symbols depending on what is used in the
|
95
|
+
# underlying Hash).
|
96
|
+
def map
|
97
|
+
keys.map do |key|
|
98
|
+
transform_key = __transform_key__(key)
|
99
|
+
yield [transform_key, __rewrap__(__getobj__[transform_key])]
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
# There is a quirk whereby the delegate library will not pass `to_json` to the
|
104
|
+
# contained Hash, and the Indifferent would the JSON-serialize as a String.
|
105
|
+
# We have to forward this method explicitly.
|
106
|
+
#
|
107
|
+
# In general, the method will never be called by the user directly but will instead
|
108
|
+
# be excercised by `JSON.dump()` and friends.
|
109
|
+
def to_json(*serializer_state)
|
110
|
+
to_h.to_json(*serializer_state)
|
111
|
+
end
|
112
|
+
|
113
|
+
def method_missing(method_name, *args)
|
114
|
+
return self[method_name] if key?(method_name) && args.empty?
|
115
|
+
|
116
|
+
super
|
117
|
+
end
|
118
|
+
|
119
|
+
def respond_to_missing?(method_name, _include_private = false)
|
120
|
+
key?(method_name)
|
121
|
+
end
|
122
|
+
|
123
|
+
def to_hash
|
124
|
+
__getobj__.to_hash
|
125
|
+
end
|
126
|
+
|
127
|
+
alias has_key? key?
|
128
|
+
|
129
|
+
private
|
130
|
+
|
131
|
+
def __transform_key__(key)
|
132
|
+
if __getobj__.key?(key.to_sym)
|
133
|
+
key.to_sym
|
134
|
+
else
|
135
|
+
key.to_s
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def __rewrap__(value)
|
140
|
+
return value if value.is_a?(self.class)
|
141
|
+
return self.class.new(value) if value.is_a?(Hash)
|
142
|
+
return value.map { |e| __rewrap__(e) } if value.is_a?(Array)
|
143
|
+
|
144
|
+
value
|
95
145
|
end
|
96
146
|
end
|
97
|
-
|
98
|
-
# There is a quirk whereby the delegate library will not pass `to_json` to the
|
99
|
-
# contained Hash, and the Indifferent would the JSON-serialize as a String.
|
100
|
-
# We have to forward this method explicitly.
|
101
|
-
#
|
102
|
-
# In general, the method will never be called by the user directly but will instead
|
103
|
-
# be excercised by `JSON.dump()` and friends.
|
104
|
-
def to_json(*serializer_state)
|
105
|
-
to_h.to_json(*serializer_state)
|
106
|
-
end
|
107
|
-
|
108
|
-
def method_missing(method_name, *args)
|
109
|
-
return self[method_name] if key?(method_name) && args.empty?
|
110
|
-
super
|
111
|
-
end
|
112
|
-
|
113
|
-
def respond_to_missing?(method_name, include_private=false)
|
114
|
-
key?(method_name)
|
115
|
-
end
|
116
|
-
|
117
|
-
alias_method :has_key?, :key?
|
118
|
-
|
119
|
-
private
|
120
|
-
|
121
|
-
def __transform_key__(k)
|
122
|
-
if __getobj__.has_key?(k.to_sym)
|
123
|
-
k.to_sym
|
124
|
-
else
|
125
|
-
k.to_s
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
def __rewrap__(v)
|
130
|
-
return v if v.is_a?(self.class)
|
131
|
-
return self.class.new(v) if v.is_a?(Hash)
|
132
|
-
return v.map{|e| __rewrap__(e)} if v.is_a?(Array)
|
133
|
-
v
|
134
|
-
end
|
135
|
-
end
|
147
|
+
end
|
data/lib/hash_tools.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module HashTools
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
INT_KEY_RE = /^\-?\d+$/ # Regular expression to detect array indices in the path ("phones/0/code")
|
8
|
-
|
4
|
+
require_relative "hash_tools/indifferent"
|
5
|
+
|
6
|
+
FWD_SLASH = "/" # Used as the default separator for deep_fetch
|
7
|
+
INT_KEY_RE = /^-?\d+$/.freeze # Regular expression to detect array indices in the path ("phones/0/code")
|
8
|
+
|
9
9
|
# Fetch a deeply-nested hash key from a hash, using a String representing a path
|
10
10
|
#
|
11
11
|
# deep_fetch({
|
@@ -15,7 +15,8 @@ module HashTools
|
|
15
15
|
# }, 'a/b/c') #=> value
|
16
16
|
#
|
17
17
|
# @param hash [Hash] the (potentially deep) string-keyed Hash to fetch the value from
|
18
|
-
# @param path [String] the path to the item in `hash`. The path may contain
|
18
|
+
# @param path [String] the path to the item in `hash`. The path may contain
|
19
|
+
# numbers for deeply nested arrays ('foo/0/bar')
|
19
20
|
# @param separator [String] the path separator, defaults to '/'
|
20
21
|
# @param default_blk The default value block for when there is no value.
|
21
22
|
# @return the fetched value or the value of the default_block
|
@@ -31,7 +32,7 @@ module HashTools
|
|
31
32
|
end
|
32
33
|
end
|
33
34
|
end
|
34
|
-
|
35
|
+
|
35
36
|
# Fetches multiple keys from a deep hash, using a Strings representing paths
|
36
37
|
#
|
37
38
|
# deep_fetch({
|
@@ -42,13 +43,14 @@ module HashTools
|
|
42
43
|
# }, 'a/b', 'z') #=> [value, 1]
|
43
44
|
#
|
44
45
|
# @param hash [Hash] the (potentially deep) string-keyed Hash to fetch the value from
|
45
|
-
# @param key_paths [String] the paths to the items in `hash`. The paths may
|
46
|
+
# @param key_paths [String] the paths to the items in `hash`. The paths may
|
47
|
+
# contain numbers for deeply nested arrays ('foo/0/bar')
|
46
48
|
# @param separator [String] the path separator, defaults to '/'
|
47
49
|
# @return [Array] the fetched values
|
48
50
|
def deep_fetch_multi(hash, *key_paths, separator: FWD_SLASH)
|
49
|
-
key_paths.map{|k| deep_fetch(hash, k, separator: separator) }
|
51
|
+
key_paths.map { |k| deep_fetch(hash, k, separator: separator) }
|
50
52
|
end
|
51
|
-
|
53
|
+
|
52
54
|
# Fetches a deeply nested key from each of the Hashes in a given Array.
|
53
55
|
#
|
54
56
|
# arr = [
|
@@ -62,9 +64,9 @@ module HashTools
|
|
62
64
|
# @param separator [String] the path separator, defaults to '/'
|
63
65
|
# @return [Array] the fetched values
|
64
66
|
def deep_map_value(enum_of_hashes, path, separator: FWD_SLASH)
|
65
|
-
enum_of_hashes.map{|h| deep_fetch(h, path, separator: separator)}
|
67
|
+
enum_of_hashes.map { |h| deep_fetch(h, path, separator: separator) }
|
66
68
|
end
|
67
|
-
|
69
|
+
|
68
70
|
# Recursively transform string keys and values of a passed
|
69
71
|
# Hash or Array using the passed transformer
|
70
72
|
#
|
@@ -74,7 +76,7 @@ module HashTools
|
|
74
76
|
def transform_string_keys_and_values_of(any, &transformer)
|
75
77
|
transform_string_values_of(transform_keys_of(any, &transformer), &transformer)
|
76
78
|
end
|
77
|
-
|
79
|
+
|
78
80
|
# Recursively convert string values in nested hashes and
|
79
81
|
# arrays using a passed block. The block will receive the String
|
80
82
|
# to transform and should return a transformed string.
|
@@ -83,11 +85,12 @@ module HashTools
|
|
83
85
|
# @param transformer The block applied to each string value, recursively
|
84
86
|
# @return the transformed value
|
85
87
|
def transform_string_values_of(any, &transformer)
|
86
|
-
|
88
|
+
case any
|
89
|
+
when String
|
87
90
|
transformer.call(any)
|
88
|
-
|
89
|
-
any.map{|e| transform_string_values_of(e, &transformer) }
|
90
|
-
|
91
|
+
when Array
|
92
|
+
any.map { |e| transform_string_values_of(e, &transformer) }
|
93
|
+
when Hash
|
91
94
|
h = {}
|
92
95
|
any.each_pair do |k, v|
|
93
96
|
h[k] = transform_string_values_of(v, &transformer)
|
@@ -97,7 +100,7 @@ module HashTools
|
|
97
100
|
any
|
98
101
|
end
|
99
102
|
end
|
100
|
-
|
103
|
+
|
101
104
|
# Recursively convert hash keys using a block.
|
102
105
|
# using a passed block. The block will receive a hash key
|
103
106
|
# to be transformed and should return a transformed key
|
@@ -110,9 +113,10 @@ module HashTools
|
|
110
113
|
# @param transformer the block to apply to each key, recursively
|
111
114
|
# @return [Hash] the transformed Hash
|
112
115
|
def transform_keys_of(any, &transformer)
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
+
case any
|
117
|
+
when Array
|
118
|
+
any.map { |e| transform_keys_of(e, &transformer) }
|
119
|
+
when Hash
|
116
120
|
h = {}
|
117
121
|
any.each_pair do |k, v|
|
118
122
|
h[transformer.call(k.to_s)] = transform_keys_of(v, &transformer)
|
@@ -122,7 +126,7 @@ module HashTools
|
|
122
126
|
any
|
123
127
|
end
|
124
128
|
end
|
125
|
-
|
129
|
+
|
126
130
|
# Returns an {HashTools::Indifferent} wrapper for the given Hash.
|
127
131
|
#
|
128
132
|
# @param hash [Hash] the Hash to wrap
|
@@ -130,6 +134,6 @@ module HashTools
|
|
130
134
|
def indifferent(hash)
|
131
135
|
Indifferent.new(hash)
|
132
136
|
end
|
133
|
-
|
137
|
+
|
134
138
|
extend self
|
135
139
|
end
|
metadata
CHANGED
@@ -1,90 +1,100 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Julik Tarkhanov
|
8
|
-
|
9
|
-
|
8
|
+
- grdw
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
+
date: 2022-04-20 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
15
|
+
name: bundler
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
16
17
|
requirements:
|
17
|
-
- - "<"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '3.3'
|
20
18
|
- - "~>"
|
21
19
|
- !ruby/object:Gem::Version
|
22
|
-
version:
|
20
|
+
version: '2'
|
23
21
|
type: :development
|
24
22
|
prerelease: false
|
25
23
|
version_requirements: !ruby/object:Gem::Requirement
|
26
24
|
requirements:
|
27
|
-
- - "
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '2'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
28
33
|
- !ruby/object:Gem::Version
|
29
|
-
version: '
|
34
|
+
version: '13.0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
30
39
|
- - "~>"
|
31
40
|
- !ruby/object:Gem::Version
|
32
|
-
version:
|
41
|
+
version: '13.0'
|
33
42
|
- !ruby/object:Gem::Dependency
|
34
|
-
name:
|
43
|
+
name: rspec
|
35
44
|
requirement: !ruby/object:Gem::Requirement
|
36
45
|
requirements:
|
37
46
|
- - "~>"
|
38
47
|
- !ruby/object:Gem::Version
|
39
|
-
version: '
|
48
|
+
version: '3.0'
|
40
49
|
type: :development
|
41
50
|
prerelease: false
|
42
51
|
version_requirements: !ruby/object:Gem::Requirement
|
43
52
|
requirements:
|
44
53
|
- - "~>"
|
45
54
|
- !ruby/object:Gem::Version
|
46
|
-
version: '
|
55
|
+
version: '3.0'
|
47
56
|
- !ruby/object:Gem::Dependency
|
48
|
-
name:
|
57
|
+
name: rubocop
|
49
58
|
requirement: !ruby/object:Gem::Requirement
|
50
59
|
requirements:
|
51
60
|
- - "~>"
|
52
61
|
- !ruby/object:Gem::Version
|
53
|
-
version:
|
62
|
+
version: '1.21'
|
54
63
|
type: :development
|
55
64
|
prerelease: false
|
56
65
|
version_requirements: !ruby/object:Gem::Requirement
|
57
66
|
requirements:
|
58
67
|
- - "~>"
|
59
68
|
- !ruby/object:Gem::Version
|
60
|
-
version:
|
69
|
+
version: '1.21'
|
61
70
|
description: Do useful things to Ruby Hashes
|
62
|
-
email:
|
71
|
+
email:
|
72
|
+
- me@julik.nl
|
73
|
+
- gerard@wetransfer.com
|
63
74
|
executables: []
|
64
75
|
extensions: []
|
65
|
-
extra_rdoc_files:
|
66
|
-
- LICENSE.txt
|
67
|
-
- README.md
|
76
|
+
extra_rdoc_files: []
|
68
77
|
files:
|
69
|
-
- ".document"
|
70
78
|
- ".rspec"
|
71
|
-
- ".
|
72
|
-
-
|
79
|
+
- ".rubocop.yml"
|
80
|
+
- CODE_OF_CONDUCT.md
|
73
81
|
- Gemfile
|
82
|
+
- Gemfile.lock
|
74
83
|
- LICENSE.txt
|
75
84
|
- README.md
|
76
85
|
- Rakefile
|
77
|
-
-
|
86
|
+
- bin/console
|
87
|
+
- bin/setup
|
78
88
|
- lib/hash_tools.rb
|
79
89
|
- lib/hash_tools/indifferent.rb
|
80
|
-
-
|
81
|
-
|
82
|
-
- spec/spec_helper.rb
|
83
|
-
homepage: http://github.com/julik/hash_tools
|
90
|
+
- lib/hash_tools/version.rb
|
91
|
+
homepage: https://github.com/WeTransfer/hash_tools
|
84
92
|
licenses:
|
85
93
|
- MIT
|
86
|
-
metadata:
|
87
|
-
|
94
|
+
metadata:
|
95
|
+
homepage_uri: https://github.com/WeTransfer/hash_tools
|
96
|
+
source_code_uri: https://github.com/WeTransfer/hash_tools
|
97
|
+
post_install_message:
|
88
98
|
rdoc_options: []
|
89
99
|
require_paths:
|
90
100
|
- lib
|
@@ -92,16 +102,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
92
102
|
requirements:
|
93
103
|
- - ">="
|
94
104
|
- !ruby/object:Gem::Version
|
95
|
-
version:
|
105
|
+
version: 2.6.0
|
96
106
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
107
|
requirements:
|
98
108
|
- - ">="
|
99
109
|
- !ruby/object:Gem::Version
|
100
110
|
version: '0'
|
101
111
|
requirements: []
|
102
|
-
|
103
|
-
|
104
|
-
signing_key:
|
112
|
+
rubygems_version: 3.2.22
|
113
|
+
signing_key:
|
105
114
|
specification_version: 4
|
106
115
|
summary: Do useful things to Ruby Hashes
|
107
116
|
test_files: []
|
data/.document
DELETED
data/.travis.yml
DELETED
data/.yardopts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
--markup markdown
|
data/hash_tools.gemspec
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: hash_tools 1.2.2 ruby lib
|
6
|
-
|
7
|
-
Gem::Specification.new do |s|
|
8
|
-
s.name = "hash_tools"
|
9
|
-
s.version = "1.2.2"
|
10
|
-
|
11
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
|
-
s.require_paths = ["lib"]
|
13
|
-
s.authors = ["Julik Tarkhanov"]
|
14
|
-
s.date = "2015-12-06"
|
15
|
-
s.description = "Do useful things to Ruby Hashes"
|
16
|
-
s.email = "me@julik.nl"
|
17
|
-
s.extra_rdoc_files = [
|
18
|
-
"LICENSE.txt",
|
19
|
-
"README.md"
|
20
|
-
]
|
21
|
-
s.files = [
|
22
|
-
".document",
|
23
|
-
".rspec",
|
24
|
-
".travis.yml",
|
25
|
-
".yardopts",
|
26
|
-
"Gemfile",
|
27
|
-
"LICENSE.txt",
|
28
|
-
"README.md",
|
29
|
-
"Rakefile",
|
30
|
-
"hash_tools.gemspec",
|
31
|
-
"lib/hash_tools.rb",
|
32
|
-
"lib/hash_tools/indifferent.rb",
|
33
|
-
"spec/hash_tools/indifferent_spec.rb",
|
34
|
-
"spec/hash_tools_spec.rb",
|
35
|
-
"spec/spec_helper.rb"
|
36
|
-
]
|
37
|
-
s.homepage = "http://github.com/julik/hash_tools"
|
38
|
-
s.licenses = ["MIT"]
|
39
|
-
s.rubygems_version = "2.2.2"
|
40
|
-
s.summary = "Do useful things to Ruby Hashes"
|
41
|
-
|
42
|
-
if s.respond_to? :specification_version then
|
43
|
-
s.specification_version = 4
|
44
|
-
|
45
|
-
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
46
|
-
s.add_development_dependency(%q<rspec>, ["< 3.3", "~> 3.2.0"])
|
47
|
-
s.add_development_dependency(%q<bundler>, ["~> 1.0"])
|
48
|
-
s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
|
49
|
-
else
|
50
|
-
s.add_dependency(%q<rspec>, ["< 3.3", "~> 3.2.0"])
|
51
|
-
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
52
|
-
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
53
|
-
end
|
54
|
-
else
|
55
|
-
s.add_dependency(%q<rspec>, ["< 3.3", "~> 3.2.0"])
|
56
|
-
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
57
|
-
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
@@ -1,90 +0,0 @@
|
|
1
|
-
require_relative '../spec_helper'
|
2
|
-
|
3
|
-
describe HashTools::Indifferent do
|
4
|
-
it 'supports indifferent access' do
|
5
|
-
h_syms = {a: 1, 'b' => 2}
|
6
|
-
wrapper = described_class.new(h_syms)
|
7
|
-
|
8
|
-
expect(wrapper['a']).to eq(1)
|
9
|
-
expect(wrapper[:a]).to eq(1)
|
10
|
-
expect(wrapper.fetch('a')).to eq(1)
|
11
|
-
expect(wrapper.fetch(:a)).to eq(1)
|
12
|
-
|
13
|
-
expect(wrapper['b']).to eq(2)
|
14
|
-
expect(wrapper[:b]).to eq(2)
|
15
|
-
expect(wrapper.fetch('b')).to eq(2)
|
16
|
-
expect(wrapper.fetch(:b)).to eq(2)
|
17
|
-
|
18
|
-
expect(wrapper.a).to eq(1)
|
19
|
-
expect(wrapper.b).to eq(2)
|
20
|
-
|
21
|
-
expect(wrapper.keys).to eq(h_syms.keys)
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'serializes as a JSON object when used as an array member' do
|
25
|
-
require 'json'
|
26
|
-
h = {a: 1}
|
27
|
-
i = described_class.new(h)
|
28
|
-
array = [h, i]
|
29
|
-
dumped = JSON.dump(array)
|
30
|
-
loaded = JSON.load(dumped)
|
31
|
-
expect(loaded[1]).to eq(loaded[0]) # The object representations should be equivalent
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'raises NoMethodError when accessing missing keys via dot notation' do
|
35
|
-
h_syms = {a: 1, 'b' => 2}
|
36
|
-
wrapper = described_class.new(h_syms)
|
37
|
-
|
38
|
-
expect {
|
39
|
-
wrapper.c
|
40
|
-
}.to raise_error(NoMethodError)
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'supports indifferent access to deeply nested hashes' do
|
44
|
-
h_deep = {a: {:b => 1, 'c' => 2}}
|
45
|
-
|
46
|
-
wrapper = described_class.new(h_deep)
|
47
|
-
expect(wrapper[:a][:b]).to eq(1)
|
48
|
-
expect(wrapper['a']['b']).to eq(1)
|
49
|
-
|
50
|
-
expect(wrapper[:a][:c]).to eq(2)
|
51
|
-
expect(wrapper['a']['c']).to eq(2)
|
52
|
-
|
53
|
-
expect(wrapper.a.b).to eq(1)
|
54
|
-
expect(wrapper.a.c).to eq(2)
|
55
|
-
|
56
|
-
expect(wrapper.keys).to eq(h_deep.keys)
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'supports has_non_empty?' do
|
60
|
-
h_deep = {:a => 1, :b => '', :c => false, :d => nil}
|
61
|
-
wrapper = described_class.new(h_deep)
|
62
|
-
|
63
|
-
expect(wrapper).to be_value_present(:a)
|
64
|
-
|
65
|
-
expect(wrapper).not_to be_value_present(:z)
|
66
|
-
expect(wrapper).not_to be_value_present('z')
|
67
|
-
|
68
|
-
expect(wrapper).not_to be_value_present(:b)
|
69
|
-
expect(wrapper).not_to be_value_present(:c)
|
70
|
-
expect(wrapper).not_to be_value_present(:d)
|
71
|
-
end
|
72
|
-
|
73
|
-
it 'supports map' do
|
74
|
-
h_deep = {:a => {:b => 1}, 'b' => {'b' => 2}}
|
75
|
-
wrapper = described_class.new(h_deep)
|
76
|
-
|
77
|
-
wrapper.map do |(k, v)|
|
78
|
-
expect(v['b']).not_to be_nil
|
79
|
-
expect(v[:b]).not_to be_nil
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
it 'supports indifferent access to inner arrays' do
|
84
|
-
h_deep = {:a => [{:b => 1}]}
|
85
|
-
wrapper = described_class.new(h_deep)
|
86
|
-
|
87
|
-
expect(wrapper[:a][0][:b]).to eq(1)
|
88
|
-
expect(wrapper['a'][0]['b']).to eq(1)
|
89
|
-
end
|
90
|
-
end
|
data/spec/hash_tools_spec.rb
DELETED
@@ -1,171 +0,0 @@
|
|
1
|
-
require_relative 'spec_helper'
|
2
|
-
|
3
|
-
describe HashTools do
|
4
|
-
let(:t) do
|
5
|
-
Class.new do
|
6
|
-
include HashTools
|
7
|
-
end.new
|
8
|
-
end
|
9
|
-
let(:uppercase) { ->(k){ k.upcase } }
|
10
|
-
|
11
|
-
describe '.indifferent' do
|
12
|
-
it 'returns an indifferent wrapper' do
|
13
|
-
h = {'foo' => {bar: 1}}
|
14
|
-
ind = t.indifferent(h)
|
15
|
-
expect(ind[:foo][:bar]).to eq(1)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'transforms hash keys' do
|
20
|
-
s = {"set" => 10}
|
21
|
-
ref = {"SET" => 10}
|
22
|
-
expect(t.transform_keys_of(s, &uppercase)).to eq(ref)
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'transforms nested hash keys' do
|
26
|
-
s = {"set" => {"two"=>123}}
|
27
|
-
ref = {"SET"=>{"TWO"=>123}}
|
28
|
-
expect(t.transform_keys_of(s, &uppercase)).to eq(ref)
|
29
|
-
end
|
30
|
-
|
31
|
-
it 'does nothing to the array' do
|
32
|
-
a = %w( a b c d)
|
33
|
-
expect(t.transform_keys_of(a, &uppercase)).to eq(a)
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'transforms hashes embedded in arrays' do
|
37
|
-
a = [{"me"=>"Julik"}]
|
38
|
-
ref = [{"ME"=>"Julik"}]
|
39
|
-
expect(t.transform_keys_of(a, &uppercase)).to eq(ref)
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'transforms nested hashes in an array' do
|
43
|
-
a = {"foo" => [{"me"=>"Julik"}]}
|
44
|
-
ref = {"FOO"=>[{"ME"=>"Julik"}]}
|
45
|
-
expect(t.transform_keys_of(a, &uppercase)).to eq(ref)
|
46
|
-
end
|
47
|
-
|
48
|
-
it 'exposes methods on the module itself' do
|
49
|
-
expect(HashTools).to respond_to(:transform_keys_of)
|
50
|
-
end
|
51
|
-
|
52
|
-
describe '.transform_string_values_of' do
|
53
|
-
it 'transforms the string value' do
|
54
|
-
x = "foo"
|
55
|
-
expect(t.transform_string_values_of(x, &uppercase)).to eq('FOO')
|
56
|
-
end
|
57
|
-
|
58
|
-
it 'transforms strings in array' do
|
59
|
-
x = %w( foo bar baz)
|
60
|
-
ref = %w( FOO BAR BAZ )
|
61
|
-
expect(t.transform_string_values_of(x, &uppercase)).to eq(ref)
|
62
|
-
end
|
63
|
-
|
64
|
-
it 'transforms string values in a Hash' do
|
65
|
-
x = {"foo" => "bar"}
|
66
|
-
ref = {"foo" => "BAR"}
|
67
|
-
expect(t.transform_string_values_of(x, &uppercase)).to eq(ref)
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
describe '.transform_string_keys_and_values_of' do
|
72
|
-
it 'transforms both keys and values' do
|
73
|
-
x = {"foo" => "bar"}
|
74
|
-
ref = {"FOO" => "BAR"}
|
75
|
-
expect(t.transform_string_keys_and_values_of(x, &uppercase)).to eq(ref)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
describe '.deep_fetch' do
|
80
|
-
let(:deep) {
|
81
|
-
{
|
82
|
-
'foo' =>1,
|
83
|
-
'bar' => {
|
84
|
-
'baz' => 2
|
85
|
-
},
|
86
|
-
'array' => [1,2,3],
|
87
|
-
'array-with-hashes' => [{'name' => 'Joe'}, {'name' => 'Jane'}]
|
88
|
-
}
|
89
|
-
}
|
90
|
-
|
91
|
-
it 'accepts a block for a default value' do
|
92
|
-
v = described_class.deep_fetch(deep, 'bar/nonexistent') { :default}
|
93
|
-
expect(v).to eq(:default)
|
94
|
-
end
|
95
|
-
|
96
|
-
it 'fetches deep keys from a hash keyed by strings' do
|
97
|
-
expect(described_class.deep_fetch(deep, 'foo')).to eq(deep.fetch('foo'))
|
98
|
-
expect(described_class.deep_fetch(deep, 'bar/baz')).to eq(deep.fetch('bar').fetch('baz'))
|
99
|
-
end
|
100
|
-
|
101
|
-
it 'fetches deep keys with a custom separator' do
|
102
|
-
expect(described_class.deep_fetch(deep, 'bar.baz', separator: '.')).to eq(deep.fetch('bar').fetch('baz'))
|
103
|
-
end
|
104
|
-
|
105
|
-
it 'causes a KeyError to be raised for missing keys' do
|
106
|
-
expect {
|
107
|
-
described_class.deep_fetch(deep, 'bar/nonexistent')
|
108
|
-
}.to raise_error(KeyError, 'key not found: "nonexistent"')
|
109
|
-
end
|
110
|
-
|
111
|
-
it 'allows fetches from arrays' do
|
112
|
-
expect(described_class.deep_fetch(deep, 'array/0')).to eq(1)
|
113
|
-
expect(described_class.deep_fetch(deep, 'array/-1')).to eq(3)
|
114
|
-
end
|
115
|
-
|
116
|
-
it 'allows fetches from hashes within arrays' do
|
117
|
-
expect(described_class.deep_fetch(deep, 'array-with-hashes/0/name')).to eq('Joe')
|
118
|
-
expect {
|
119
|
-
described_class.deep_fetch(deep, 'array-with-hashes/10/name')
|
120
|
-
}.to raise_error(IndexError, /index 10 outside of array bounds/)
|
121
|
-
|
122
|
-
default_value = described_class.deep_fetch(deep, 'array-with-hashes/0/jake') { :default }
|
123
|
-
expect(default_value).to eq(:default)
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
describe '.deep_fetch_multi' do
|
128
|
-
let(:deep) {
|
129
|
-
{
|
130
|
-
'foo' =>1,
|
131
|
-
'bar' => {
|
132
|
-
'baz' => 2
|
133
|
-
},
|
134
|
-
'array' => [1,2,3],
|
135
|
-
'array-with-hashes' => [{'name' => 'Joe'}, {'name' => 'Jane'}]
|
136
|
-
}
|
137
|
-
}
|
138
|
-
|
139
|
-
it 'fetches mutiple keys' do
|
140
|
-
expect(described_class.deep_fetch_multi(deep, 'foo', 'bar/baz')).to eq([1,2])
|
141
|
-
end
|
142
|
-
|
143
|
-
it 'fetches deep keys with a custom separator' do
|
144
|
-
expect(described_class.deep_fetch_multi(deep, 'foo', 'bar.baz', separator: '.')).to eq([1,2])
|
145
|
-
end
|
146
|
-
|
147
|
-
it 'causes a KeyError to be raised for missing keys' do
|
148
|
-
expect {
|
149
|
-
described_class.deep_fetch_multi(deep, 'foo', 'nonexistent')
|
150
|
-
}.to raise_error(KeyError)
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
describe '.deep_map_value' do
|
155
|
-
it 'deep maps the values' do
|
156
|
-
v = [
|
157
|
-
{'foo' => 5},
|
158
|
-
{'foo' => 6},
|
159
|
-
]
|
160
|
-
expect(described_class.deep_map_value(v, "foo")).to eq([5,6])
|
161
|
-
end
|
162
|
-
|
163
|
-
it 'deep maps the values with a custom separator' do
|
164
|
-
v = [
|
165
|
-
{'foo' => {'bar' => 1}},
|
166
|
-
{'foo' => {'bar' => 2}},
|
167
|
-
]
|
168
|
-
expect(described_class.deep_map_value(v, "foo-bar", separator: '-')).to eq([1,2])
|
169
|
-
end
|
170
|
-
end
|
171
|
-
end
|
data/spec/spec_helper.rb
DELETED