hash-initializer 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1c546f1d927cf15383abdc00af089666eadd3a46
4
- data.tar.gz: 4393e60dc200c66e63e8580c94e08efa337e2677
3
+ metadata.gz: 2776e940d986a5716d5de404d7eb06da8eed9200
4
+ data.tar.gz: 813e2dcf325bb64f6869c070a5b9a4ddf2ae4aa7
5
5
  SHA512:
6
- metadata.gz: 42f271242154d02fe796a8dc3cdae2ed8002290b3bef329c6afe0773fc0ba3441f30b77663eec00baa0ddf7e5cf63c8bb95635984141b38817cc9ad096225d8f
7
- data.tar.gz: d45564b4312c683698e3a8b417773a63c2ac94ae1261fddd62547e68ea22882640d5e4ed032e8a82dded5de07990b32682e0d57eb583cad60836850b8f962696
6
+ metadata.gz: bb1de11208a0c1445f708a418ae86416119922e6bb07f91501596603ce6962ca55a308727e55c70d43d6392f1914fe691a8e680895e8b1b0af1d5ef27942267b
7
+ data.tar.gz: 733650fd90f5db9dae7298d2951cbcbd16c7f8fed17a435935eec9a8807fead5bb5c3a0e3ca935643d651663a10d4cc390e9262ec8020edb4d0f4aab208effa9
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: hash-initializer 0.1.1 ruby lib
5
+ # stub: hash-initializer 0.1.2 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "hash-initializer".freeze
9
- s.version = "0.1.1"
9
+ s.version = "0.1.2"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
13
13
  s.authors = ["Jits".freeze]
14
- s.date = "2017-12-11"
14
+ s.date = "2017-12-12"
15
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
16
  s.email = "nospam@jits.co.uk".freeze
17
17
  s.extra_rdoc_files = [
@@ -12,9 +12,8 @@ module HashInitializer
12
12
 
13
13
  return {} if levels.empty?
14
14
 
15
- Hash.new do |h,k|
16
- h[k] = build_levels(levels)
17
- end
15
+ defaults_proc = build_levels(levels)
16
+ Hash.new(&defaults_proc)
18
17
  end
19
18
 
20
19
  private
@@ -28,24 +27,17 @@ module HashInitializer
28
27
  end
29
28
 
30
29
  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
30
+ return Proc.new { |h,k| h[k] = Hash.new } if levels.one? && levels.first == :hash
40
31
 
41
- def build_level previous, level
32
+ level = levels.first
42
33
  case level
43
34
  when :hash
44
- Hash.new { |h,k| h[k] = previous }
35
+ inner_proc = build_levels(levels.drop(1))
36
+ Proc.new { |h,k| h[k] = Hash.new(&inner_proc) }
45
37
  when :array
46
- Array.new
38
+ Proc.new { |h,k| h[k] = Array.new }
47
39
  else
48
- level
40
+ Proc.new { |h,k| h[k] = level }
49
41
  end
50
42
  end
51
43
 
@@ -104,6 +104,28 @@ RSpec.describe HashInitializer do
104
104
  end
105
105
  end
106
106
 
107
+ context 'Hash with hash values with hash values' do
108
+ subject do
109
+ HashInitializer[
110
+ :hash,
111
+ :hash
112
+ ]
113
+ end
114
+
115
+ it 'should initialise a new hash with the expected levels of default values' do
116
+ expect(subject[:foo][:bar]).to eq Hash.new
117
+ expect(subject[:foo][:bar][:baz]).to eq nil
118
+ subject[:foo][:bar][:baz] = 1
119
+ expect(subject[:foo][:bar][:baz]).to eq 1
120
+
121
+ expect(subject[:foo][:bar][:bob]).to eq nil
122
+
123
+ expect(subject[:aaa]).to eq Hash.new
124
+ expect(subject[:aaa][:bar]).to eq Hash.new
125
+ expect(subject[:aaa][:bar][:baz]).to eq nil
126
+ end
127
+ end
128
+
107
129
  context 'Hash with hash values with hash values with default float values' do
108
130
  subject do
109
131
  HashInitializer[
@@ -119,6 +141,32 @@ RSpec.describe HashInitializer do
119
141
  expect(subject[:foo][:bar][:baz]).to eq 2.5
120
142
 
121
143
  expect(subject[:foo][:bar][:bob]).to eq 1.0
144
+
145
+ expect(subject[:aaa]).to eq Hash.new
146
+ expect(subject[:aaa][:bar]).to eq Hash.new
147
+ expect(subject[:aaa][:bar][:baz]).to eq 1.0
148
+ end
149
+ end
150
+
151
+ context 'Hash with hash values with hash values with array values' do
152
+ subject do
153
+ HashInitializer[
154
+ :hash,
155
+ :hash,
156
+ :array
157
+ ]
158
+ end
159
+
160
+ it 'should initialise a new hash with the expected levels of default values' do
161
+ expect(subject[:foo][:bar][:baz]).to eq []
162
+ subject[:foo][:bar][:baz] << 'a'
163
+ expect(subject[:foo][:bar][:baz]).to eq ['a']
164
+
165
+ expect(subject[:foo][:bar][:bob]).to eq []
166
+
167
+ expect(subject[:aaa]).to eq Hash.new
168
+ expect(subject[:aaa][:bar]).to eq Hash.new
169
+ expect(subject[:aaa][:bar][:baz]).to eq []
122
170
  end
123
171
  end
124
172
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash-initializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jits
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-11 00:00:00.000000000 Z
11
+ date: 2017-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec