hash-initializer 0.1.1
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/.document +5 -0
- data/.rspec +3 -0
- data/.ruby-version +1 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +83 -0
- data/LICENSE.txt +20 -0
- data/README.markdown +110 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/hash-initializer.gemspec +67 -0
- data/lib/hash-initializer.rb +1 -0
- data/lib/hash_initializer.rb +52 -0
- data/spec/hash_initializer_spec.rb +125 -0
- data/spec/spec_helper.rb +87 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1c546f1d927cf15383abdc00af089666eadd3a46
|
4
|
+
data.tar.gz: 4393e60dc200c66e63e8580c94e08efa337e2677
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 42f271242154d02fe796a8dc3cdae2ed8002290b3bef329c6afe0773fc0ba3441f30b77663eec00baa0ddf7e5cf63c8bb95635984141b38817cc9ad096225d8f
|
7
|
+
data.tar.gz: d45564b4312c683698e3a8b417773a63c2ac94ae1261fddd62547e68ea22882640d5e4ed032e8a82dded5de07990b32682e0d57eb583cad60836850b8f962696
|
data/.document
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.5
|
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rspec", "~> 3.5.0"
|
10
|
+
gem "rdoc", "~> 3.12"
|
11
|
+
gem "bundler", "~> 1.0"
|
12
|
+
gem "juwelier", "~> 2.1.0"
|
13
|
+
gem "simplecov", ">= 0"
|
14
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
GEM
|
2
|
+
remote: https://rubygems.org/
|
3
|
+
specs:
|
4
|
+
addressable (2.5.2)
|
5
|
+
public_suffix (>= 2.0.2, < 4.0)
|
6
|
+
builder (3.2.3)
|
7
|
+
descendants_tracker (0.0.4)
|
8
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
9
|
+
diff-lcs (1.3)
|
10
|
+
docile (1.1.5)
|
11
|
+
faraday (0.12.2)
|
12
|
+
multipart-post (>= 1.2, < 3)
|
13
|
+
git (1.3.0)
|
14
|
+
github_api (0.18.2)
|
15
|
+
addressable (~> 2.4)
|
16
|
+
descendants_tracker (~> 0.0.4)
|
17
|
+
faraday (~> 0.8)
|
18
|
+
hashie (~> 3.5, >= 3.5.2)
|
19
|
+
oauth2 (~> 1.0)
|
20
|
+
hashie (3.5.6)
|
21
|
+
highline (1.7.10)
|
22
|
+
json (1.8.6)
|
23
|
+
juwelier (2.1.3)
|
24
|
+
builder
|
25
|
+
bundler (>= 1.13)
|
26
|
+
git (>= 1.2.5)
|
27
|
+
github_api
|
28
|
+
highline (>= 1.6.15)
|
29
|
+
nokogiri (>= 1.5.10)
|
30
|
+
rake
|
31
|
+
rdoc
|
32
|
+
semver
|
33
|
+
jwt (1.5.6)
|
34
|
+
mini_portile2 (2.3.0)
|
35
|
+
multi_json (1.12.2)
|
36
|
+
multi_xml (0.6.0)
|
37
|
+
multipart-post (2.0.0)
|
38
|
+
nokogiri (1.8.1)
|
39
|
+
mini_portile2 (~> 2.3.0)
|
40
|
+
oauth2 (1.4.0)
|
41
|
+
faraday (>= 0.8, < 0.13)
|
42
|
+
jwt (~> 1.0)
|
43
|
+
multi_json (~> 1.3)
|
44
|
+
multi_xml (~> 0.5)
|
45
|
+
rack (>= 1.2, < 3)
|
46
|
+
public_suffix (3.0.1)
|
47
|
+
rack (2.0.3)
|
48
|
+
rake (12.3.0)
|
49
|
+
rdoc (3.12.2)
|
50
|
+
json (~> 1.4)
|
51
|
+
rspec (3.5.0)
|
52
|
+
rspec-core (~> 3.5.0)
|
53
|
+
rspec-expectations (~> 3.5.0)
|
54
|
+
rspec-mocks (~> 3.5.0)
|
55
|
+
rspec-core (3.5.4)
|
56
|
+
rspec-support (~> 3.5.0)
|
57
|
+
rspec-expectations (3.5.0)
|
58
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
59
|
+
rspec-support (~> 3.5.0)
|
60
|
+
rspec-mocks (3.5.0)
|
61
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
62
|
+
rspec-support (~> 3.5.0)
|
63
|
+
rspec-support (3.5.0)
|
64
|
+
semver (1.0.1)
|
65
|
+
simplecov (0.15.1)
|
66
|
+
docile (~> 1.1.0)
|
67
|
+
json (>= 1.8, < 3)
|
68
|
+
simplecov-html (~> 0.10.0)
|
69
|
+
simplecov-html (0.10.2)
|
70
|
+
thread_safe (0.3.6)
|
71
|
+
|
72
|
+
PLATFORMS
|
73
|
+
ruby
|
74
|
+
|
75
|
+
DEPENDENCIES
|
76
|
+
bundler (~> 1.0)
|
77
|
+
juwelier (~> 2.1.0)
|
78
|
+
rdoc (~> 3.12)
|
79
|
+
rspec (~> 3.5.0)
|
80
|
+
simplecov
|
81
|
+
|
82
|
+
BUNDLED WITH
|
83
|
+
1.16.0
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2017 Jits
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# hash-initializer
|
2
|
+
|
3
|
+
Initialise Ruby hashes with multiple levels of default values
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
```shell
|
8
|
+
gem install hash-initializer
|
9
|
+
```
|
10
|
+
|
11
|
+
Or add to your `Gemfile` and go forth.
|
12
|
+
|
13
|
+
## Example usage
|
14
|
+
|
15
|
+
### With numeric values
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
my_hash = HashInitializer[0]
|
19
|
+
|
20
|
+
my_hash[:foo] # => 0
|
21
|
+
my_hash[:foo] += 1
|
22
|
+
my_hash[:foo] # => 1
|
23
|
+
my_hash[:bar] # => 0
|
24
|
+
```
|
25
|
+
|
26
|
+
### With hash values
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
my_hash = HashInitializer[:hash]
|
30
|
+
|
31
|
+
my_hash[:foo] # => {}
|
32
|
+
my_hash[:foo][:bar] # => nil
|
33
|
+
my_hash[:foo][:bar] = 'a'
|
34
|
+
my_hash[:foo][:bar] # 'a'
|
35
|
+
```
|
36
|
+
|
37
|
+
### How about array values
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
my_hash = HashInitializer[:array]
|
41
|
+
|
42
|
+
my_hash[:foo] # => []
|
43
|
+
my_hash[:foo] << 'a'
|
44
|
+
my_hash[:foo] # ['a']
|
45
|
+
```
|
46
|
+
|
47
|
+
### Watch out though, you can end up sharing the same array instance (you probably shouldn't do this!)
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
my_hash = HashInitializer[[]]
|
51
|
+
|
52
|
+
my_hash[:foo] # => []
|
53
|
+
my_hash[:bar] # => []
|
54
|
+
my_hash[:foo] << 'a'
|
55
|
+
my_hash[:foo] # ['a']
|
56
|
+
my_hash[:bar] # ['a']
|
57
|
+
my_hash[:foo].equal?(my_hash[:bar]) # true
|
58
|
+
```
|
59
|
+
|
60
|
+
### … and you can end up sharing the same hash instance!
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
my_hash = HashInitializer[{}]
|
64
|
+
|
65
|
+
my_hash[:foo] # {}
|
66
|
+
my_hash[:foo][:bar] = 1
|
67
|
+
my_hash[:foo][:bar] # => 1
|
68
|
+
my_hash[:bob] # => { bar: 1 }
|
69
|
+
my_hash[:foo].equal?(my_hash[:bob]) # true
|
70
|
+
```
|
71
|
+
|
72
|
+
### Nested hashes
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
my_hash = HashInitializer[
|
76
|
+
:hash,
|
77
|
+
:hash,
|
78
|
+
1.0
|
79
|
+
]
|
80
|
+
|
81
|
+
my_hash[:foo][:bar][:baz] # => 1.0
|
82
|
+
my_hash[:foo][:bar][:baz] += 1.5
|
83
|
+
my_hash[:foo][:bar][:baz] # => 2.5
|
84
|
+
```
|
85
|
+
|
86
|
+
## Caution
|
87
|
+
|
88
|
+
Use with consideration! Great for temporary data (e.g. whilst processing), or for playing around. But for anything more substantial consider building a proper model layer with first class objects / structs.
|
89
|
+
|
90
|
+
## Contributing to hash-initializer
|
91
|
+
|
92
|
+
- Check out the latest master to make sure the feature hasn't been
|
93
|
+
implemented or the bug hasn't been fixed yet.
|
94
|
+
- Check out the issue tracker to make sure someone already hasn't
|
95
|
+
requested it and/or contributed it.
|
96
|
+
- Fork the project.
|
97
|
+
- Start a feature/bugfix branch.
|
98
|
+
- Commit and push until you are happy with your contribution.
|
99
|
+
- Make sure to add tests for it. This is important so I don't break it
|
100
|
+
in a future version unintentionally.
|
101
|
+
- Please try not to mess with the Rakefile, version, or history. If
|
102
|
+
you want to have your own version, or is otherwise necessary, that
|
103
|
+
is fine, but please isolate to its own commit so I can cherry-pick
|
104
|
+
around it.
|
105
|
+
|
106
|
+
|
107
|
+
## Copyright
|
108
|
+
|
109
|
+
Copyright (c) 2017 Jits. See
|
110
|
+
LICENSE.txt for further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
require 'juwelier'
|
14
|
+
Juwelier::Tasks.new do |gem|
|
15
|
+
# gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
|
16
|
+
gem.name = "hash-initializer"
|
17
|
+
gem.homepage = "http://github.com/jits/hash-initializer"
|
18
|
+
gem.license = "MIT"
|
19
|
+
gem.summary = %Q{Initialise Ruby hashes with multiple levels of default values}
|
20
|
+
gem.description = %Q{I heard you like hashes within your hashes, so I made a thing to initialise hashes within your hashes (and more!)}
|
21
|
+
gem.email = "nospam@jits.co.uk"
|
22
|
+
gem.authors = ["Jits"]
|
23
|
+
gem.required_ruby_version = '>= 2.3.0'
|
24
|
+
|
25
|
+
# dependencies defined in Gemfile
|
26
|
+
end
|
27
|
+
Juwelier::RubygemsDotOrgTasks.new
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
desc "Code coverage detail"
|
35
|
+
task :simplecov do
|
36
|
+
ENV['COVERAGE'] = "true"
|
37
|
+
Rake::Task['spec'].execute
|
38
|
+
end
|
39
|
+
|
40
|
+
task :default => :spec
|
41
|
+
|
42
|
+
require 'rdoc/task'
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
45
|
+
|
46
|
+
rdoc.rdoc_dir = 'rdoc'
|
47
|
+
rdoc.title = "hash-initializer #{version}"
|
48
|
+
rdoc.rdoc_files.include('README*')
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.1
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# Generated by juwelier
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: hash-initializer 0.1.1 ruby lib
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "hash-initializer".freeze
|
9
|
+
s.version = "0.1.1"
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
12
|
+
s.require_paths = ["lib".freeze]
|
13
|
+
s.authors = ["Jits".freeze]
|
14
|
+
s.date = "2017-12-11"
|
15
|
+
s.description = "I heard you like hashes within your hashes, so I made a thing to initialise hashes within your hashes (and more!)".freeze
|
16
|
+
s.email = "nospam@jits.co.uk".freeze
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE.txt",
|
19
|
+
"README.markdown"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
".document",
|
23
|
+
".rspec",
|
24
|
+
".ruby-version",
|
25
|
+
"Gemfile",
|
26
|
+
"Gemfile.lock",
|
27
|
+
"LICENSE.txt",
|
28
|
+
"README.markdown",
|
29
|
+
"Rakefile",
|
30
|
+
"VERSION",
|
31
|
+
"hash-initializer.gemspec",
|
32
|
+
"lib/hash-initializer.rb",
|
33
|
+
"lib/hash_initializer.rb",
|
34
|
+
"spec/hash_initializer_spec.rb",
|
35
|
+
"spec/spec_helper.rb"
|
36
|
+
]
|
37
|
+
s.homepage = "http://github.com/jits/hash-initializer".freeze
|
38
|
+
s.licenses = ["MIT".freeze]
|
39
|
+
s.required_ruby_version = Gem::Requirement.new(">= 2.3.0".freeze)
|
40
|
+
s.rubygems_version = "2.6.13".freeze
|
41
|
+
s.summary = "Initialise Ruby hashes with multiple levels of default values".freeze
|
42
|
+
|
43
|
+
if s.respond_to? :specification_version then
|
44
|
+
s.specification_version = 4
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
47
|
+
s.add_development_dependency(%q<rspec>.freeze, ["~> 3.5.0"])
|
48
|
+
s.add_development_dependency(%q<rdoc>.freeze, ["~> 3.12"])
|
49
|
+
s.add_development_dependency(%q<bundler>.freeze, ["~> 1.0"])
|
50
|
+
s.add_development_dependency(%q<juwelier>.freeze, ["~> 2.1.0"])
|
51
|
+
s.add_development_dependency(%q<simplecov>.freeze, [">= 0"])
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<rspec>.freeze, ["~> 3.5.0"])
|
54
|
+
s.add_dependency(%q<rdoc>.freeze, ["~> 3.12"])
|
55
|
+
s.add_dependency(%q<bundler>.freeze, ["~> 1.0"])
|
56
|
+
s.add_dependency(%q<juwelier>.freeze, ["~> 2.1.0"])
|
57
|
+
s.add_dependency(%q<simplecov>.freeze, [">= 0"])
|
58
|
+
end
|
59
|
+
else
|
60
|
+
s.add_dependency(%q<rspec>.freeze, ["~> 3.5.0"])
|
61
|
+
s.add_dependency(%q<rdoc>.freeze, ["~> 3.12"])
|
62
|
+
s.add_dependency(%q<bundler>.freeze, ["~> 1.0"])
|
63
|
+
s.add_dependency(%q<juwelier>.freeze, ["~> 2.1.0"])
|
64
|
+
s.add_dependency(%q<simplecov>.freeze, [">= 0"])
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
require_relative 'hash_initializer'
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module HashInitializer
|
2
|
+
|
3
|
+
class InvalidLevels < StandardError
|
4
|
+
end
|
5
|
+
|
6
|
+
extend self
|
7
|
+
|
8
|
+
def [](*levels)
|
9
|
+
unless levels_okay?(levels)
|
10
|
+
raise InvalidLevels.new('Levels specified are invalid - note that you can only have a non :hash level at the very end')
|
11
|
+
end
|
12
|
+
|
13
|
+
return {} if levels.empty?
|
14
|
+
|
15
|
+
Hash.new do |h,k|
|
16
|
+
h[k] = build_levels(levels)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def levels_okay? levels
|
23
|
+
return true if levels.size < 2
|
24
|
+
|
25
|
+
levels
|
26
|
+
.take(levels.size - 1)
|
27
|
+
.all? { |l| l == :hash }
|
28
|
+
end
|
29
|
+
|
30
|
+
def build_levels levels
|
31
|
+
return Hash.new if levels.one? && levels.first == :hash
|
32
|
+
|
33
|
+
last = build_level(nil, levels.last)
|
34
|
+
|
35
|
+
levels
|
36
|
+
.reverse
|
37
|
+
.drop(1)
|
38
|
+
.reduce(last, &method(:build_level))
|
39
|
+
end
|
40
|
+
|
41
|
+
def build_level previous, level
|
42
|
+
case level
|
43
|
+
when :hash
|
44
|
+
Hash.new { |h,k| h[k] = previous }
|
45
|
+
when :array
|
46
|
+
Array.new
|
47
|
+
else
|
48
|
+
level
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
RSpec.describe HashInitializer do
|
2
|
+
|
3
|
+
context 'Base case' do
|
4
|
+
subject do
|
5
|
+
HashInitializer[]
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should initialise a hash with no default value' do
|
9
|
+
expect(subject).to eq Hash.new
|
10
|
+
expect(subject[:foo]).to be nil
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'Invalid levels' do
|
15
|
+
it 'should error accordingly' do
|
16
|
+
expect {
|
17
|
+
HashInitializer[:array, :hash]
|
18
|
+
}.to raise_error(
|
19
|
+
HashInitializer::InvalidLevels,
|
20
|
+
'Levels specified are invalid - note that you can only have a non :hash level at the very end'
|
21
|
+
)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'Hash with 0 values' do
|
26
|
+
subject do
|
27
|
+
HashInitializer[0]
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should initialise a hash with 0 values' do
|
31
|
+
expect(subject[:foo]).to eq 0
|
32
|
+
expect(subject[:bar]).to eq 0
|
33
|
+
expect(subject[:baz]).to eq 0
|
34
|
+
|
35
|
+
subject[:foo] += 1
|
36
|
+
subject[:bar] += 5
|
37
|
+
expect(subject[:foo]).to eq 1
|
38
|
+
expect(subject[:bar]).to eq 5
|
39
|
+
expect(subject[:baz]).to eq 0
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'Hash with hash values' do
|
44
|
+
subject do
|
45
|
+
HashInitializer[:hash]
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should initialise a hash with hash values' do
|
49
|
+
expect(subject[:foo]).to eq Hash.new
|
50
|
+
expect(subject[:foo][:bar]).to eq nil
|
51
|
+
expect(subject[:foo][:baz]).to be nil
|
52
|
+
|
53
|
+
subject[:foo][:bar] = 'a'
|
54
|
+
expect(subject[:foo][:bar]).to eq 'a'
|
55
|
+
expect(subject[:foo][:baz]).to be nil
|
56
|
+
|
57
|
+
expect(subject[:aaa][:bar]).to be nil
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'Hash with array values' do
|
62
|
+
subject do
|
63
|
+
HashInitializer[:array]
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should initialise a hash with array values' do
|
67
|
+
expect(subject[:foo]).to eq []
|
68
|
+
expect(subject[:bar]).to eq []
|
69
|
+
|
70
|
+
subject[:foo] << 'a'
|
71
|
+
expect(subject[:foo]).to eq ['a']
|
72
|
+
expect(subject[:bar]).to eq []
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'Hash with same array instance' do
|
77
|
+
subject do
|
78
|
+
HashInitializer[[]]
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'should initialise a hash with the same array instance for each value' do
|
82
|
+
expect(subject[:foo]).to eq []
|
83
|
+
expect(subject[:bar]).to eq []
|
84
|
+
|
85
|
+
subject[:foo] << 'a'
|
86
|
+
expect(subject[:foo]).to eq ['a']
|
87
|
+
expect(subject[:bar]).to eq ['a']
|
88
|
+
expect(subject[:foo]).to be subject[:bar]
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
context 'Hash with same hash instance' do
|
93
|
+
subject do
|
94
|
+
HashInitializer[{}]
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'should initialise a hash with the same hash instance for each value' do
|
98
|
+
expect(subject[:foo]).to eq Hash.new
|
99
|
+
|
100
|
+
subject[:foo][:bar] = 1
|
101
|
+
expect(subject[:foo][:bar]).to eq 1
|
102
|
+
expect(subject[:bob]).to eq({ bar: 1 })
|
103
|
+
expect(subject[:foo]).to be subject[:bar]
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
context 'Hash with hash values with hash values with default float values' do
|
108
|
+
subject do
|
109
|
+
HashInitializer[
|
110
|
+
:hash,
|
111
|
+
:hash,
|
112
|
+
1.0
|
113
|
+
]
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'should initialise a new hash with the expected levels of default values' do
|
117
|
+
expect(subject[:foo][:bar][:baz]).to eq 1.0
|
118
|
+
subject[:foo][:bar][:baz] += 1.5
|
119
|
+
expect(subject[:foo][:bar][:baz]).to eq 2.5
|
120
|
+
|
121
|
+
expect(subject[:foo][:bar][:bob]).to eq 1.0
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'hash-initializer'
|
2
|
+
|
3
|
+
RSpec.configure do |config|
|
4
|
+
# rspec-expectations config goes here. You can use an alternate
|
5
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
6
|
+
# assertions if you prefer.
|
7
|
+
config.expect_with :rspec do |expectations|
|
8
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
9
|
+
# and `failure_message` of custom matchers include text for helper methods
|
10
|
+
# defined using `chain`, e.g.:
|
11
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
12
|
+
# # => "be bigger than 2 and smaller than 4"
|
13
|
+
# ...rather than:
|
14
|
+
# # => "be bigger than 2"
|
15
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
16
|
+
end
|
17
|
+
|
18
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
19
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
20
|
+
config.mock_with :rspec do |mocks|
|
21
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
22
|
+
# a real object. This is generally recommended, and will default to
|
23
|
+
# `true` in RSpec 4.
|
24
|
+
mocks.verify_partial_doubles = true
|
25
|
+
end
|
26
|
+
|
27
|
+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
|
28
|
+
# have no way to turn it off -- the option exists only for backwards
|
29
|
+
# compatibility in RSpec 3). It causes shared context metadata to be
|
30
|
+
# inherited by the metadata hash of host groups and examples, rather than
|
31
|
+
# triggering implicit auto-inclusion in groups with matching metadata.
|
32
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
33
|
+
|
34
|
+
# The settings below are suggested to provide a good initial experience
|
35
|
+
# with RSpec, but feel free to customize to your heart's content.
|
36
|
+
=begin
|
37
|
+
# This allows you to limit a spec run to individual examples or groups
|
38
|
+
# you care about by tagging them with `:focus` metadata. When nothing
|
39
|
+
# is tagged with `:focus`, all examples get run. RSpec also provides
|
40
|
+
# aliases for `it`, `describe`, and `context` that include `:focus`
|
41
|
+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
42
|
+
config.filter_run_when_matching :focus
|
43
|
+
|
44
|
+
# Allows RSpec to persist some state between runs in order to support
|
45
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
46
|
+
# you configure your source control system to ignore this file.
|
47
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
48
|
+
|
49
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
50
|
+
# recommended. For more details, see:
|
51
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
52
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
53
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
54
|
+
config.disable_monkey_patching!
|
55
|
+
|
56
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
57
|
+
# be too noisy due to issues in dependencies.
|
58
|
+
config.warnings = true
|
59
|
+
|
60
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
61
|
+
# file, and it's useful to allow more verbose output when running an
|
62
|
+
# individual spec file.
|
63
|
+
if config.files_to_run.one?
|
64
|
+
# Use the documentation formatter for detailed output,
|
65
|
+
# unless a formatter has already been configured
|
66
|
+
# (e.g. via a command-line flag).
|
67
|
+
config.default_formatter = 'doc'
|
68
|
+
end
|
69
|
+
|
70
|
+
# Print the 10 slowest examples and example groups at the
|
71
|
+
# end of the spec run, to help surface which specs are running
|
72
|
+
# particularly slow.
|
73
|
+
config.profile_examples = 10
|
74
|
+
|
75
|
+
# Run specs in random order to surface order dependencies. If you find an
|
76
|
+
# order dependency and want to debug it, you can fix the order by providing
|
77
|
+
# the seed, which is printed after each run.
|
78
|
+
# --seed 1234
|
79
|
+
config.order = :random
|
80
|
+
|
81
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
82
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
83
|
+
# test failures related to randomization by passing the same `--seed` value
|
84
|
+
# as the one that triggered the failure.
|
85
|
+
Kernel.srand config.seed
|
86
|
+
=end
|
87
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash-initializer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jits
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-12-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.5.0
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.5.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rdoc
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.12'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.12'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.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.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: juwelier
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.1.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.1.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: I heard you like hashes within your hashes, so I made a thing to initialise
|
84
|
+
hashes within your hashes (and more!)
|
85
|
+
email: nospam@jits.co.uk
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files:
|
89
|
+
- LICENSE.txt
|
90
|
+
- README.markdown
|
91
|
+
files:
|
92
|
+
- ".document"
|
93
|
+
- ".rspec"
|
94
|
+
- ".ruby-version"
|
95
|
+
- Gemfile
|
96
|
+
- Gemfile.lock
|
97
|
+
- LICENSE.txt
|
98
|
+
- README.markdown
|
99
|
+
- Rakefile
|
100
|
+
- VERSION
|
101
|
+
- hash-initializer.gemspec
|
102
|
+
- lib/hash-initializer.rb
|
103
|
+
- lib/hash_initializer.rb
|
104
|
+
- spec/hash_initializer_spec.rb
|
105
|
+
- spec/spec_helper.rb
|
106
|
+
homepage: http://github.com/jits/hash-initializer
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: 2.3.0
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 2.6.13
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: Initialise Ruby hashes with multiple levels of default values
|
130
|
+
test_files: []
|