multi_key_hash 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +3 -0
- data/LICENSE +21 -0
- data/README.md +39 -0
- data/Rakefile +6 -0
- data/bin/console +7 -0
- data/bin/setup +6 -0
- data/lib/multi_key_hash/version.rb +3 -0
- data/lib/multi_key_hash.rb +91 -0
- data/multi_key_hash.gemspec +25 -0
- data/spec/multi_key_hash_spec.rb +95 -0
- data/spec/spec_helper.rb +12 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f409aff203d1cb86eeff4b85a1a3e5ab6767d7e4
|
4
|
+
data.tar.gz: 3d39adf2113f383c2b4b23f9c86ca4915b3bdba8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 51adba605fff6cb264f86b2015ad02f45a2616c70eba209bbe49ca570a0be1c924047b37fa2bf50c7916c3996fd15c857b099a5a3db434c8902d962d8d3c8cec
|
7
|
+
data.tar.gz: c6f5498239b4a510c475e06092e4e25519cdb6735dd7be79071a5790807d21387d4277753b4f9a5b255c7fb7fa021bd802f7c101747d7a9d1c599e5f3401eaef
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 SamirH
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# MultiKeyHash
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/multi_key_hash`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'multi_key_hash'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install multi_key_hash
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
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.
|
30
|
+
|
31
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/multi_key_hash.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'multi_key_hash/version'
|
2
|
+
require 'digest'
|
3
|
+
|
4
|
+
class MultiKeyHash
|
5
|
+
def initialize(values = nil)
|
6
|
+
@next_key = 1
|
7
|
+
@outer = @inner = {}
|
8
|
+
order_values(values)
|
9
|
+
end
|
10
|
+
|
11
|
+
def [](outer_key)
|
12
|
+
@outer[outer_key].nil? ? nil : @inner[@outer[outer_key]]
|
13
|
+
end
|
14
|
+
|
15
|
+
def []=(outer_key, new_value)
|
16
|
+
if outer_key.is_a?(Array) && !outer_key.empty?
|
17
|
+
order_values(outer_key, new_value)
|
18
|
+
else
|
19
|
+
inner_key = @inner.select do |_, existing|
|
20
|
+
existing == new_value
|
21
|
+
end.map do |key, _|
|
22
|
+
key
|
23
|
+
end.first
|
24
|
+
|
25
|
+
if inner_key
|
26
|
+
@outer[outer_key] = inner_key
|
27
|
+
else
|
28
|
+
remove_hex_key(outer_key)
|
29
|
+
uniq_key = generate_uniq
|
30
|
+
@outer[outer_key] = uniq_key
|
31
|
+
@inner[uniq_key] = new_value
|
32
|
+
@next_key += 1
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_h
|
38
|
+
converted = {}
|
39
|
+
@inner.each do |i_key, i_value|
|
40
|
+
@outer.each do |o_key, o_value|
|
41
|
+
if i_value == o_key
|
42
|
+
converted[i_key] = o_value
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
converted
|
48
|
+
end
|
49
|
+
|
50
|
+
def delete(key)
|
51
|
+
remove_hex_key(key)
|
52
|
+
delete_key(key)
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def order_values(values, ar = nil)
|
58
|
+
values&.each do |keys, value|
|
59
|
+
if keys.is_a?(Array) && !keys.empty?
|
60
|
+
keys.each do |key|
|
61
|
+
self[key] = value
|
62
|
+
end
|
63
|
+
else
|
64
|
+
self[keys] = ar || value
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def remove_hex_key(outer_key)
|
70
|
+
check_key = @outer[outer_key]
|
71
|
+
|
72
|
+
if multiple_presence(check_key) == 1
|
73
|
+
delete_key(check_key)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def multiple_presence(key)
|
78
|
+
@outer.count do |el|
|
79
|
+
el[1] == key
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def delete_key(key)
|
84
|
+
@outer.delete(key)
|
85
|
+
@inner.delete(key)
|
86
|
+
end
|
87
|
+
|
88
|
+
def generate_uniq
|
89
|
+
Digest::SHA1.hexdigest(@next_key.to_s)[8..16]
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "multi_key_hash/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'multi_key_hash'
|
8
|
+
spec.version = MultiKeyHash::VERSION
|
9
|
+
spec.authors = ['SamirHodzic']
|
10
|
+
spec.email = ['samir.sgd@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'something'
|
13
|
+
spec.description = 'somethingaa'
|
14
|
+
spec.homepage = 'http://something.com'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files`.split($/)
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_development_dependency 'rake', '~> 0'
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
24
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
25
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
RSpec.describe MultiKeyHash do
|
2
|
+
it 'has a version number' do
|
3
|
+
expect(MultiKeyHash::VERSION).not_to be nil
|
4
|
+
end
|
5
|
+
|
6
|
+
describe '#new' do
|
7
|
+
let(:payload) do
|
8
|
+
{
|
9
|
+
%w[one two three] => 'number',
|
10
|
+
%i[a b c] => 'letter',
|
11
|
+
'single' => 'value'
|
12
|
+
}
|
13
|
+
end
|
14
|
+
let(:multi_hash) { described_class.new(payload) }
|
15
|
+
|
16
|
+
context 'creates hash' do
|
17
|
+
it 'when accessing to multi keys' do
|
18
|
+
expect(multi_hash['one']).to eq 'number'
|
19
|
+
expect(multi_hash[:a]).to eq 'letter'
|
20
|
+
expect(multi_hash['single']).to eq 'value'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'modifies hash' do
|
25
|
+
it 'modifying defined multi key (string)' do
|
26
|
+
multi_hash['one'] = 'first_number'
|
27
|
+
|
28
|
+
expect(multi_hash['one']).to eq 'first_number'
|
29
|
+
expect(multi_hash['two']).to eq 'number'
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'modifying defined multi key (symbol)' do
|
33
|
+
multi_hash[:a] = 'first_letter'
|
34
|
+
|
35
|
+
expect(multi_hash[:a]).to eq 'first_letter'
|
36
|
+
expect(multi_hash[:b]).to eq 'letter'
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'modifying defined single key' do
|
40
|
+
multi_hash['single'] = 'another_value'
|
41
|
+
|
42
|
+
expect(multi_hash['single']).to eq 'another_value'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'adds new keys to hash' do
|
47
|
+
it 'adds single key value' do
|
48
|
+
multi_hash[:new_single_key] = 'new_single_value'
|
49
|
+
|
50
|
+
expect(multi_hash[:new_single_key]).to eq 'new_single_value'
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'adds multi key value' do
|
54
|
+
multi_hash[['c++', 'php', 'ruby', 'js']] = 'language'
|
55
|
+
|
56
|
+
expect(multi_hash['c++']).to eq 'language'
|
57
|
+
expect(multi_hash['php']).to eq 'language'
|
58
|
+
expect(multi_hash['ruby']).to eq 'language'
|
59
|
+
expect(multi_hash['js']).to eq 'language'
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context 'deletes keys from hash' do
|
64
|
+
it 'deletes single defined key' do
|
65
|
+
multi_hash.delete('something')
|
66
|
+
|
67
|
+
expect(multi_hash['something']).to eq nil
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'deletes multi defined key' do
|
71
|
+
multi_hash.delete(:a)
|
72
|
+
|
73
|
+
expect(multi_hash[:a]).to eq nil
|
74
|
+
expect(multi_hash[:b]).to eq 'letter'
|
75
|
+
expect(multi_hash[:c]).to eq 'letter'
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context 'converts to regular hash' do
|
80
|
+
it 'without multi keys' do
|
81
|
+
regular_hash = multi_hash.to_h
|
82
|
+
|
83
|
+
expect(regular_hash).to eq ({
|
84
|
+
'one' => 'number',
|
85
|
+
'two' => 'number',
|
86
|
+
'three' => 'number',
|
87
|
+
:a => 'letter',
|
88
|
+
:b => 'letter',
|
89
|
+
:c => 'letter',
|
90
|
+
'single' => 'value'
|
91
|
+
})
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: multi_key_hash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- SamirHodzic
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-12-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: somethingaa
|
56
|
+
email:
|
57
|
+
- samir.sgd@gmail.com
|
58
|
+
executables:
|
59
|
+
- console
|
60
|
+
- setup
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- ".rspec"
|
66
|
+
- ".travis.yml"
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- lib/multi_key_hash.rb
|
74
|
+
- lib/multi_key_hash/version.rb
|
75
|
+
- multi_key_hash.gemspec
|
76
|
+
- spec/multi_key_hash_spec.rb
|
77
|
+
- spec/spec_helper.rb
|
78
|
+
homepage: http://something.com
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata: {}
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 2.6.13
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: something
|
102
|
+
test_files:
|
103
|
+
- spec/multi_key_hash_spec.rb
|
104
|
+
- spec/spec_helper.rb
|