hashery 2.1.2 → 2.2.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.
@@ -2,15 +2,51 @@ require 'helper'
2
2
 
3
3
  test_case LRUHash do
4
4
 
5
+ # Build a full hash of :a => 1, :b => 2, :c => 3, used least recently
6
+ # in that order, i.e. :a is next to be evicted.
7
+ def abc(max_size=3)
8
+ h = LRUHash.new(max_size)
9
+ h[:a] = 1
10
+ h[:b] = 2
11
+ h[:c] = 3
12
+ h
13
+ end
14
+
5
15
  class_method :new do
6
- h = LRUHash.new(10)
7
- LRUHash.assert === h
16
+ test do
17
+ h = LRUHash.new(10)
18
+ LRUHash.assert === h
19
+ end
20
+
21
+ test "max_size is converted with #to_i" do
22
+ LRUHash.new("5").max_size.assert == 5
23
+ LRUHash.new(2.9).max_size.assert == 2
24
+ end
25
+
26
+ test "max_size must be positive" do
27
+ ArgumentError.assert.raised? { LRUHash.new(0) }
28
+ ArgumentError.assert.raised? { LRUHash.new(-1) }
29
+ ArgumentError.assert.raised? { LRUHash.new(nil) }
30
+ end
31
+
32
+ test "default value" do
33
+ h = LRUHash.new(10, :none)
34
+ h[:x].assert == :none
35
+ end
8
36
  end
9
37
 
10
38
  method :max_size= do
11
39
  test do
12
40
  h = LRUHash.new(10)
13
41
  h.max_size = 100
42
+ h.max_size.assert == 100
43
+ end
44
+
45
+ test "shrinking evicts least recently used entries" do
46
+ h = abc
47
+ h[:a]
48
+ h.max_size = 1
49
+ h.keys.assert == [:a]
14
50
  end
15
51
  end
16
52
 
@@ -18,7 +54,35 @@ test_case LRUHash do
18
54
  test do
19
55
  h = LRUHash.new(10)
20
56
  h.store(:a, 1)
21
- h[:a].assert == 1
57
+ h[:a].assert == 1
58
+ end
59
+
60
+ test "returns the value" do
61
+ h = LRUHash.new(10)
62
+ h.store(:a, 1).assert == 1
63
+ end
64
+
65
+ test "evicts the least recently used entry when full" do
66
+ h = abc
67
+ h.store(:d, 4)
68
+ h.keys.assert == [:d, :c, :b]
69
+ end
70
+
71
+ test "overwriting a key does not evict" do
72
+ h = abc
73
+ h.store(:a, 10)
74
+ h.size.assert == 3
75
+ h.keys.assert == [:a, :c, :b]
76
+ h[:a].assert == 10
77
+ end
78
+
79
+ test "unfrozen String keys are copied and frozen" do
80
+ h = LRUHash.new(10)
81
+ k = String.new("k")
82
+ h.store(k, 1)
83
+ k << "x"
84
+ h.keys.assert == ["k"]
85
+ h.keys.first.assert.frozen?
22
86
  end
23
87
  end
24
88
 
@@ -33,6 +97,64 @@ test_case LRUHash do
33
97
  h = LRUHash.new(10){ |h,k| h[k] = 0 }
34
98
  h[:a].assert == 0
35
99
  end
100
+
101
+ test "marks the entry most recently used" do
102
+ h = abc
103
+ h[:a]
104
+ h[:d] = 4
105
+ h.keys.assert == [:d, :a, :c]
106
+ end
107
+
108
+ test "nil values are found" do
109
+ h = LRUHash.new(10, :default)
110
+ h[:a] = nil
111
+ h[:a].assert == nil
112
+ end
113
+ end
114
+
115
+ method :fetch do
116
+ test do
117
+ h = LRUHash.new(10)
118
+ h[:a] = 1
119
+ h.fetch(:a).assert == 1
120
+ end
121
+
122
+ test "missing key raises KeyError" do
123
+ h = LRUHash.new(10)
124
+ KeyError.assert.raised? { h.fetch(:x) }
125
+ end
126
+
127
+ test "KeyError names the key and receiver" do
128
+ h = LRUHash.new(10)
129
+ e = (h.fetch(:x) rescue $!)
130
+ KeyError.assert === e
131
+ e.message.assert == "key not found: :x"
132
+ e.key.assert == :x
133
+ e.receiver.assert.equal?(h)
134
+ end
135
+
136
+ test "missing key with block" do
137
+ h = LRUHash.new(10)
138
+ h.fetch(:x){ |k| "no #{k}" }.assert == "no x"
139
+ end
140
+
141
+ test "missing key with default argument" do
142
+ h = LRUHash.new(10)
143
+ h.fetch(:x, 0).assert == 0
144
+ h.fetch(:x, nil).assert == nil
145
+ end
146
+
147
+ test "ignores the hash default" do
148
+ h = LRUHash.new(10, :default)
149
+ KeyError.assert.raised? { h.fetch(:x) }
150
+ end
151
+
152
+ test "marks the entry most recently used" do
153
+ h = abc
154
+ h.fetch(:a)
155
+ h[:d] = 4
156
+ h.keys.assert == [:d, :a, :c]
157
+ end
36
158
  end
37
159
 
38
160
  method :empty? do
@@ -42,6 +164,13 @@ test_case LRUHash do
42
164
  end
43
165
  end
44
166
 
167
+ method :size do
168
+ test do
169
+ abc.size.assert == 3
170
+ abc.length.assert == 3
171
+ end
172
+ end
173
+
45
174
  method :key do
46
175
  test do
47
176
  h = LRUHash.new(10)
@@ -56,6 +185,12 @@ test_case LRUHash do
56
185
  h[:a] = 1
57
186
  h.keys.assert == [:a]
58
187
  end
188
+
189
+ test "most recently used first" do
190
+ h = abc
191
+ h[:a]
192
+ h.keys.assert == [:a, :c, :b]
193
+ end
59
194
  end
60
195
 
61
196
  method :values do
@@ -64,6 +199,12 @@ test_case LRUHash do
64
199
  h[:a] = 1
65
200
  h.values.assert == [1]
66
201
  end
202
+
203
+ test "most recently used first" do
204
+ h = abc
205
+ h[:a]
206
+ h.values.assert == [1, 3, 2]
207
+ end
67
208
  end
68
209
 
69
210
  method :values_at do
@@ -73,6 +214,12 @@ test_case LRUHash do
73
214
  h[:b] = 2
74
215
  h.values_at(:a).assert == [1]
75
216
  end
217
+
218
+ test "missing keys give the default" do
219
+ h = LRUHash.new(10, :none)
220
+ h[:a] = 1
221
+ h.values_at(:a, :x).assert == [1, :none]
222
+ end
76
223
  end
77
224
 
78
225
  method :has_key? do
@@ -80,6 +227,17 @@ test_case LRUHash do
80
227
  h = LRUHash.new(10)
81
228
  h[:a] = 1
82
229
  h.assert.has_key?(:a)
230
+ h.assert.key?(:a)
231
+ h.assert.member?(:a)
232
+ h.assert.include?(:a)
233
+ h.refute.has_key?(:b)
234
+ end
235
+
236
+ test "does not affect LRU order" do
237
+ h = abc
238
+ h.has_key?(:a)
239
+ h[:d] = 4
240
+ h.refute.has_key?(:a)
83
241
  end
84
242
  end
85
243
 
@@ -88,6 +246,7 @@ test_case LRUHash do
88
246
  h = LRUHash.new(10)
89
247
  h[:a] = 1
90
248
  h.assert.has_value?(1)
249
+ h.refute.has_value?(2)
91
250
  end
92
251
  end
93
252
 
@@ -97,6 +256,7 @@ test_case LRUHash do
97
256
  h[:a] = 1
98
257
  h[:b] = 2
99
258
  h.assoc(:a).assert == [:a,1]
259
+ h.assoc(:x).assert == nil
100
260
  end
101
261
  end
102
262
 
@@ -106,6 +266,26 @@ test_case LRUHash do
106
266
  h[:a] = 1
107
267
  h[:b] = 2
108
268
  h.rassoc(1).assert == [:a,1]
269
+ h.rassoc(9).assert == nil
270
+ end
271
+ end
272
+
273
+ method :each do
274
+ test "most recently used first" do
275
+ h = abc
276
+ h[:a]
277
+ h.to_a.assert == [[:a, 1], [:c, 3], [:b, 2]]
278
+ end
279
+
280
+ test "returns an enumerator without a block" do
281
+ Enumerator.assert === abc.each
282
+ abc.each.size.assert == 3
283
+ end
284
+
285
+ test "allows modification while iterating" do
286
+ h = abc
287
+ h.each { |k, v| h[k] = v * 10 }
288
+ h.values.sort.assert == [10, 20, 30]
109
289
  end
110
290
  end
111
291
 
@@ -145,8 +325,18 @@ test_case LRUHash do
145
325
  test do
146
326
  h = LRUHash.new(10)
147
327
  h[:a] = 1
148
- h.delete(:a)
149
- h.assert.empty?
328
+ h.delete(:a).assert == 1
329
+ h.assert.empty?
330
+ end
331
+
332
+ test "missing key returns nil" do
333
+ h = LRUHash.new(10)
334
+ h.delete(:a).assert == nil
335
+ end
336
+
337
+ test "missing key with block" do
338
+ h = LRUHash.new(10)
339
+ h.delete(:a){ |k| "no #{k}" }.assert == "no a"
150
340
  end
151
341
  end
152
342
 
@@ -155,7 +345,87 @@ test_case LRUHash do
155
345
  h = LRUHash.new(10)
156
346
  h[:a] = 1
157
347
  h.delete_if{ |k,v| k == :a }
158
- h.assert.empty?
348
+ h.assert.empty?
349
+ end
350
+
351
+ test "returns self" do
352
+ h = abc
353
+ h.delete_if{ |k,v| v.odd? }.assert.equal?(h)
354
+ h.keys.assert == [:b]
355
+ end
356
+
357
+ test "returns an enumerator without a block" do
358
+ Enumerator.assert === abc.delete_if
359
+ end
360
+ end
361
+
362
+ method :release_proc= do
363
+ test "called for evicted, deleted and cleared entries" do
364
+ log = []
365
+ h = LRUHash.new(2)
366
+ h.release_proc = lambda { |k, v| log << [k, v] }
367
+ h[:a] = 1
368
+ h[:b] = 2
369
+ h[:c] = 3 # evicts :a
370
+ h.delete(:b)
371
+ h[:d] = 4
372
+ h.clear # least recently used first
373
+ log.assert == [[:a, 1], [:b, 2], [:c, 3], [:d, 4]]
374
+ end
375
+
376
+ test "called when shrinking and in delete_if" do
377
+ log = []
378
+ h = abc
379
+ h.release_proc = lambda { |k, v| log << k }
380
+ h.max_size = 2
381
+ h.delete_if { |k, v| k == :c }
382
+ log.assert == [:a, :c]
383
+ end
384
+
385
+ test "not called when a value is overwritten" do
386
+ log = []
387
+ h = LRUHash.new(2)
388
+ h.release_proc = lambda { |k, v| log << k }
389
+ h[:a] = 1
390
+ h[:a] = 2
391
+ log.assert == []
392
+ end
393
+ end
394
+
395
+ method :dup do
396
+ test "copy is independent of the original" do
397
+ a = abc
398
+ b = a.dup
399
+ b[:d] = 4
400
+ b.delete(:b)
401
+ a.to_a.assert == [[:c, 3], [:b, 2], [:a, 1]]
402
+ b.to_a.assert == [[:d, 4], [:c, 3]]
403
+ end
404
+
405
+ test "copy keeps LRU order" do
406
+ a = abc
407
+ a[:a]
408
+ a.dup.keys.assert == [:a, :c, :b]
409
+ end
410
+ end
411
+
412
+ method :inspect do
413
+ test "looks like a Hash, most recently used first" do
414
+ h = LRUHash.new(10)
415
+ h[:a] = "x"
416
+ h[nil] = 1
417
+ h.inspect.assert == {nil => 1, :a => "x"}.inspect
418
+ h.to_s.assert == h.inspect
419
+ end
420
+
421
+ test "empty" do
422
+ LRUHash.new(10).inspect.assert == "{}"
423
+ end
424
+ end
425
+
426
+ method :to_h do
427
+ test do
428
+ abc.to_h.assert == {:a => 1, :b => 2, :c => 3}
159
429
  end
160
430
  end
161
431
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashery
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.2
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Trans
@@ -11,53 +11,10 @@ authors:
11
11
  - George Moschovitis
12
12
  - Jeena Paradies
13
13
  - Erik Veenstra
14
- autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
- date: 2016-05-01 00:00:00.000000000 Z
18
- dependencies:
19
- - !ruby/object:Gem::Dependency
20
- name: qed
21
- requirement: !ruby/object:Gem::Requirement
22
- requirements:
23
- - - ">="
24
- - !ruby/object:Gem::Version
25
- version: '0'
26
- type: :development
27
- prerelease: false
28
- version_requirements: !ruby/object:Gem::Requirement
29
- requirements:
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: '0'
33
- - !ruby/object:Gem::Dependency
34
- name: lemon
35
- requirement: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - ">="
38
- - !ruby/object:Gem::Version
39
- version: '0'
40
- type: :development
41
- prerelease: false
42
- version_requirements: !ruby/object:Gem::Requirement
43
- requirements:
44
- - - ">="
45
- - !ruby/object:Gem::Version
46
- version: '0'
47
- - !ruby/object:Gem::Dependency
48
- name: rubytest-cli
49
- requirement: !ruby/object:Gem::Requirement
50
- requirements:
51
- - - ">="
52
- - !ruby/object:Gem::Version
53
- version: '0'
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- requirements:
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- version: '0'
16
+ date: 1980-01-02 00:00:00.000000000 Z
17
+ dependencies: []
61
18
  description: The Hashery is a tight collection of Hash-like classes. Included among
62
19
  its many offerings are the auto-sorting Dictionary class, the efficient LRUHash,
63
20
  the flexible OpenHash and the convenient KeyHash. Nearly every class is a subclass
@@ -68,15 +25,13 @@ email:
68
25
  executables: []
69
26
  extensions: []
70
27
  extra_rdoc_files:
28
+ - HISTORY.md
71
29
  - LICENSE.txt
72
30
  - NOTICE.txt
73
- - HISTORY.md
74
31
  - README.md
75
32
  files:
76
- - ".index"
77
33
  - ".yardopts"
78
34
  - HISTORY.md
79
- - Index.yml
80
35
  - LICENSE.txt
81
36
  - NOTICE.txt
82
37
  - README.md
@@ -104,7 +59,6 @@ files:
104
59
  - demo/applique/ae.rb
105
60
  - demo/applique/hashery.rb
106
61
  - lib/hashery.rb
107
- - lib/hashery.yml
108
62
  - lib/hashery/association.rb
109
63
  - lib/hashery/casting_hash.rb
110
64
  - lib/hashery/core_ext.rb
@@ -123,6 +77,7 @@ files:
123
77
  - lib/hashery/query_hash.rb
124
78
  - lib/hashery/stash.rb
125
79
  - lib/hashery/static_hash.rb
80
+ - lib/hashery/version.rb
126
81
  - test/case_association.rb
127
82
  - test/case_casting_hash.rb
128
83
  - test/case_core_ext.rb
@@ -138,11 +93,15 @@ files:
138
93
  - test/case_query_hash.rb
139
94
  - test/fixture/example.ini
140
95
  - test/helper.rb
141
- homepage: http://rubyworks.github.com/hashery
96
+ homepage: https://rubyworks.github.io/hashery
142
97
  licenses:
143
98
  - BSD-2-Clause
144
- metadata: {}
145
- post_install_message:
99
+ metadata:
100
+ homepage_uri: https://rubyworks.github.io/hashery
101
+ source_code_uri: https://github.com/rubyworks/hashery
102
+ bug_tracker_uri: https://github.com/rubyworks/hashery/issues
103
+ changelog_uri: https://github.com/rubyworks/hashery/blob/master/HISTORY.md
104
+ documentation_uri: https://rubydoc.info/gems/hashery
146
105
  rdoc_options: []
147
106
  require_paths:
148
107
  - lib
@@ -150,30 +109,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
150
109
  requirements:
151
110
  - - ">="
152
111
  - !ruby/object:Gem::Version
153
- version: '0'
112
+ version: '2.7'
154
113
  required_rubygems_version: !ruby/object:Gem::Requirement
155
114
  requirements:
156
115
  - - ">="
157
116
  - !ruby/object:Gem::Version
158
117
  version: '0'
159
118
  requirements: []
160
- rubyforge_project:
161
- rubygems_version: 2.5.1
162
- signing_key:
119
+ rubygems_version: 3.6.9
163
120
  specification_version: 4
164
121
  summary: Facets-bread collection of Hash-like classes.
165
- test_files:
166
- - test/case_core_ext.rb
167
- - test/case_open_hash.rb
168
- - test/case_ini_hash.rb
169
- - test/case_linked_list.rb
170
- - test/case_association.rb
171
- - test/case_key_hash.rb
172
- - test/case_dictionary.rb
173
- - test/case_property_hash.rb
174
- - test/helper.rb
175
- - test/case_lru_hash.rb
176
- - test/case_query_hash.rb
177
- - test/case_crud_hash.rb
178
- - test/case_casting_hash.rb
179
- - test/case_open_cascade.rb
122
+ test_files: []
data/.index DELETED
@@ -1,78 +0,0 @@
1
- ---
2
- revision: 2013
3
- type: ruby
4
- sources:
5
- - Index.yml
6
- - Gemfile
7
- authors:
8
- - name: Trans
9
- email: transfire@gmail.com
10
- - name: Kirk Haines
11
- - name: Robert Klemme
12
- - name: Jan Molic
13
- - name: George Moschovitis
14
- - name: Jeena Paradies
15
- - name: Erik Veenstra
16
- organizations:
17
- - name: RubyWorks (http://rubyworks.github.com/)
18
- requirements:
19
- - groups:
20
- - development
21
- - test
22
- version: ">= 0"
23
- name: qed
24
- - groups:
25
- - development
26
- - test
27
- version: ">= 0"
28
- name: lemon
29
- - groups:
30
- - development
31
- - test
32
- version: ">= 0"
33
- name: rubytest-cli
34
- conflicts: []
35
- alternatives: []
36
- resources:
37
- - type: home
38
- uri: http://rubyworks.github.com/hashery
39
- label: Homepage
40
- - type: code
41
- uri: http://github.com/rubyworks/hashery
42
- label: Source Code
43
- - type: mail
44
- uri: http://groups.google.com/group/rubyworks-mailinglist
45
- label: Mailing List
46
- - type: docs
47
- uri: http://rubydoc.info/github/rubyworks/hashery/master/frames
48
- label: Documentation
49
- - type: wiki
50
- uri: http://wiki.github.com/rubyworks/hashery
51
- label: User Guide
52
- - type: gems
53
- uri: http://rubygems.org/gems/hashery
54
- repositories:
55
- - name: upstream
56
- scm: git
57
- uri: git://github.com/rubyworks/hashery.git
58
- categories: []
59
- copyrights:
60
- - holder: Rubyworks
61
- year: '2010'
62
- license: BSD-2-Clause
63
- customs: []
64
- paths:
65
- lib:
66
- - lib
67
- - alt
68
- name: hashery
69
- title: Hashery
70
- version: 2.1.2
71
- summary: Facets-bread collection of Hash-like classes.
72
- description: The Hashery is a tight collection of Hash-like classes. Included among
73
- its many offerings are the auto-sorting Dictionary class, the efficient LRUHash,
74
- the flexible OpenHash and the convenient KeyHash. Nearly every class is a subclass
75
- of the CRUDHash which defines a CRUD model on top of Ruby's standard Hash making
76
- it a snap to subclass and augment to fit any specific use case.
77
- created: '2010-04-21'
78
- date: '2016-05-01'
data/Index.yml DELETED
@@ -1,52 +0,0 @@
1
- ---
2
- name:
3
- hashery
4
-
5
- version:
6
- 2.1.2
7
-
8
- title:
9
- Hashery
10
-
11
- summary:
12
- Facets-bread collection of Hash-like classes.
13
-
14
- description:
15
- The Hashery is a tight collection of Hash-like classes. Included among its many
16
- offerings are the auto-sorting Dictionary class, the efficient LRUHash, the
17
- flexible OpenHash and the convenient KeyHash. Nearly every class is a subclass
18
- of the CRUDHash which defines a CRUD model on top of Ruby's standard Hash
19
- making it a snap to subclass and augment to fit any specific use case.
20
-
21
- authors:
22
- - Trans <transfire@gmail.com>
23
- - Kirk Haines
24
- - Robert Klemme
25
- - Jan Molic
26
- - George Moschovitis
27
- - Jeena Paradies
28
- - Erik Veenstra
29
-
30
- resources:
31
- home: http://rubyworks.github.com/hashery
32
- code: http://github.com/rubyworks/hashery
33
- mail: http://groups.google.com/group/rubyworks-mailinglist
34
- docs: http://rubydoc.info/github/rubyworks/hashery/master/frames
35
- wiki: http://wiki.github.com/rubyworks/hashery
36
- gems: http://rubygems.org/gems/hashery
37
-
38
- repositories:
39
- upstream: "git://github.com/rubyworks/hashery.git"
40
-
41
- organizations:
42
- - RubyWorks (http://rubyworks.github.com/)
43
-
44
- copyrights:
45
- - 2010 Rubyworks (BSD-2-Clause)
46
-
47
- load_path:
48
- - lib
49
- - alt
50
-
51
- created: '2010-04-21'
52
-