hashery 2.1.2 → 2.2.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 +5 -5
- data/HISTORY.md +77 -0
- data/README.md +59 -42
- data/lib/hashery/core_ext.rb +1 -1
- data/lib/hashery/crud_hash.rb +2 -2
- data/lib/hashery/dictionary.rb +125 -46
- data/lib/hashery/fuzzy_hash.rb +1 -1
- data/lib/hashery/ini_hash.rb +4 -4
- data/lib/hashery/linked_list.rb +1 -1
- data/lib/hashery/lru_hash.rb +167 -160
- data/lib/hashery/open_hash.rb +2 -2
- data/lib/hashery/path_hash.rb +120 -46
- data/lib/hashery/static_hash.rb +2 -0
- data/lib/hashery/version.rb +3 -0
- data/lib/hashery.rb +1 -3
- data/test/case_association.rb +2 -2
- data/test/case_dictionary.rb +270 -15
- data/test/case_ini_hash.rb +28 -2
- data/test/case_lru_hash.rb +276 -6
- data/test/case_path_hash.rb +225 -0
- metadata +16 -72
- data/.index +0 -78
- data/Index.yml +0 -52
- data/lib/hashery.yml +0 -78
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 0c7b1dd1acaad0bf94b26375d8b89edf9d7a6a16974c3a119258ef568bb751fc
|
|
4
|
+
data.tar.gz: 96bf33585b82497053d226118733daa5aa975c15bf2de95e618046c13632e8f2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c675066ca2fe40a9164a4ac073eab96799d2f7484abd6e91efaece52de4483076d6e6da6e0b01d4562646ad4bc4e82691452fb8593a576800b807159c1c7022c
|
|
7
|
+
data.tar.gz: e41776c5b3c31e93db0e3f6616a048e8e5b3fb9193e4c917c34fcfd3293a4ceef4cd1e0290314057f9aa7ed851242ad72fb91a6eac1c669b7820ae65b64d22b2
|
data/HISTORY.md
CHANGED
|
@@ -1,5 +1,82 @@
|
|
|
1
1
|
# RELEASE HISTORY
|
|
2
2
|
|
|
3
|
+
## 2.2.2 / 2026-11-17
|
|
4
|
+
|
|
5
|
+
This release fixes PathHash, which had been broken for years: reading any
|
|
6
|
+
key raised NameError. PathHash also has tests of its own now. LRUHash is
|
|
7
|
+
unchanged.
|
|
8
|
+
|
|
9
|
+
Changes:
|
|
10
|
+
|
|
11
|
+
* Fix PathHash. `[]`, `==`, `expand`, `ordered!` and `index` raised
|
|
12
|
+
errors; `size`, `each`, `key?`, `values` and `to_a` saw no entries;
|
|
13
|
+
copies shared entries with the original; a key also matched longer keys
|
|
14
|
+
that started with it (`a` matched `ab`); `fetch` ignored its block; and
|
|
15
|
+
`delete` returned an empty PathHash instead of nil for a missing key.
|
|
16
|
+
|
|
17
|
+
## 2.2.1 / 2026-11-17
|
|
18
|
+
|
|
19
|
+
This release fixes a number of bugs in Dictionary. Most notably, a
|
|
20
|
+
Dictionary kept in order with `order_by_key` or `order_by_value` could no
|
|
21
|
+
longer be read at all (not even inspected) once it held a key or value
|
|
22
|
+
that didn't compare with the others, such as `nil` (#7). Now sorting by
|
|
23
|
+
such an order raises and leaves the Dictionary as it was, and adding an
|
|
24
|
+
entry the order can't place raises without storing it. LRUHash is
|
|
25
|
+
unchanged.
|
|
26
|
+
|
|
27
|
+
Changes:
|
|
28
|
+
|
|
29
|
+
* Fix Dictionary sort orders that can't sort the entries (#7).
|
|
30
|
+
* Fix Dictionary#dup and #clone, which shared state with the original
|
|
31
|
+
(clone), or dropped the sort order and default (dup).
|
|
32
|
+
* Fix Dictionary#reject!, which returned a filtered copy instead of
|
|
33
|
+
removing entries.
|
|
34
|
+
* Fix Dictionary#shift on an empty Dictionary, and #shift and #pop with
|
|
35
|
+
a `nil` key.
|
|
36
|
+
* Fix Dictionary[hash] and #replace, which shared the given hash or
|
|
37
|
+
order instead of copying it.
|
|
38
|
+
* Fix Dictionary#insert with an existing key or an index past the end.
|
|
39
|
+
* Fix Dictionary#[]= with an index and a `nil` or `false` value.
|
|
40
|
+
* Fix Dictionary#delete removing 1.0 from the order along with 1.
|
|
41
|
+
* Fix Dictionary#== for a sorted Dictionary changed since it was read.
|
|
42
|
+
|
|
43
|
+
## 2.2.0 / 2026-11-16
|
|
44
|
+
|
|
45
|
+
This release concentrates on LRUHash, by far the most used part of the
|
|
46
|
+
Hashery. Its public API is unchanged, but it has been reimplemented on top
|
|
47
|
+
of Ruby's ordered Hash instead of a linked list of nodes, which makes it
|
|
48
|
+
about twice as fast, and a number of bugs have been fixed. It also
|
|
49
|
+
modernizes the project: Ruby 2.7 or newer is now required, the gem is
|
|
50
|
+
built from a plain gemspec, and CI runs on GitHub Actions.
|
|
51
|
+
|
|
52
|
+
Changes:
|
|
53
|
+
|
|
54
|
+
* LRUHash is about 2x faster, using Ruby's ordered Hash internally.
|
|
55
|
+
* Fix LRUHash#dup and #clone, which shared state with the original.
|
|
56
|
+
* Fix LRUHash#inspect and #to_s, which showed keys and values with #to_s
|
|
57
|
+
and failed under frozen string literals.
|
|
58
|
+
* LRUHash#keys and #values now list most recently used first, like #each.
|
|
59
|
+
* LRUHash#fetch accepts a default argument and its KeyError names the key.
|
|
60
|
+
* LRUHash#delete accepts a block for missing keys, like Hash#delete.
|
|
61
|
+
* LRUHash#delete_if returns an Enumerator when called without a block.
|
|
62
|
+
* Add LRUHash#length.
|
|
63
|
+
* Fix IniHash.new with a Hash, and so IniHash.text and .write_to_file.
|
|
64
|
+
* Fix IniHash.write_to_file with a comment (used String#each).
|
|
65
|
+
* Fix `require 'hashery/static_hash'` on its own.
|
|
66
|
+
* Prepare for frozen string literals and fix warnings under `ruby -w`.
|
|
67
|
+
* Require Ruby 2.7 or newer.
|
|
68
|
+
* Replace Indexer metadata (.index, Index.yml) with hashery.gemspec.
|
|
69
|
+
* Replace Travis CI with GitHub Actions.
|
|
70
|
+
|
|
71
|
+
## 2.1.2 / 2016-05-02
|
|
72
|
+
|
|
73
|
+
Minor release fixes a test for KeyHash. And that's it.
|
|
74
|
+
|
|
75
|
+
Changes:
|
|
76
|
+
|
|
77
|
+
* Fix initialize test for KeyHash class.
|
|
78
|
+
|
|
79
|
+
|
|
3
80
|
## 2.1.1 / 2013-08-21
|
|
4
81
|
|
|
5
82
|
This minor release clarifies the licensing. The entire library is now
|
data/README.md
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
# Hashery
|
|
2
2
|
|
|
3
|
-
[](http://github.com/rubyworks/hashery/issues)
|
|
7
|
-
[](https://www.gittip.com/rubyworksorg/)
|
|
3
|
+
[](https://rubygems.org/gems/hashery)
|
|
4
|
+
[](https://github.com/rubyworks/hashery/actions/workflows/ci.yml)
|
|
5
|
+
[](https://github.com/rubyworks/hashery/issues)
|
|
8
6
|
|
|
9
|
-
[Homepage](
|
|
10
|
-
[Development](
|
|
11
|
-
[Report Issue](
|
|
12
|
-
[Chat](https://kiwiirc.com/client/irc.freenode.net/?nick=user1|?#rubyworks)
|
|
7
|
+
[Homepage](https://rubyworks.github.io/hashery) ·
|
|
8
|
+
[Development](https://github.com/rubyworks/hashery) ·
|
|
9
|
+
[Report Issue](https://github.com/rubyworks/hashery/issues)
|
|
13
10
|
|
|
14
11
|
|
|
15
12
|
## Description
|
|
@@ -19,11 +16,10 @@ of Hash-like classes. To better support this collection
|
|
|
19
16
|
of libraries it was deemed prudent to create a new project
|
|
20
17
|
specifically for them. Hence the *Facets Hashery*.
|
|
21
18
|
|
|
22
|
-
Included in this collection are the widely used
|
|
23
|
-
the
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
other yummy morsels.
|
|
19
|
+
Included in this collection are the widely used LRUHash,
|
|
20
|
+
the auto-sorting Dictionary class, a number of _open_ classes
|
|
21
|
+
similar to the standard OpenStruct, some variations of the
|
|
22
|
+
standard Hash class and a few other yummy morsels.
|
|
27
23
|
|
|
28
24
|
|
|
29
25
|
## Usage
|
|
@@ -33,6 +29,36 @@ included in this collection and read the demo documents which give
|
|
|
33
29
|
examples of almost all features.
|
|
34
30
|
|
|
35
31
|
|
|
32
|
+
### LRUHash
|
|
33
|
+
|
|
34
|
+
`Hashery::LRUHash` is a size-limited Hash that evicts the least recently
|
|
35
|
+
used entry when full. It can be required on its own:
|
|
36
|
+
|
|
37
|
+
```ruby
|
|
38
|
+
require 'hashery/lru_hash'
|
|
39
|
+
|
|
40
|
+
cache = Hashery::LRUHash.new(2)
|
|
41
|
+
cache[:a] = 1
|
|
42
|
+
cache[:b] = 2
|
|
43
|
+
cache[:a] # reading :a makes it the most recently used
|
|
44
|
+
cache[:c] = 3 # full, so :b, the least recently used, is evicted
|
|
45
|
+
|
|
46
|
+
cache.keys # => [:c, :a] (most recently used first)
|
|
47
|
+
cache.fetch(:b, 0) # => 0
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Reading with `#[]`, `#fetch`, `#assoc`, `#rassoc`, `#key` or `#values_at`
|
|
51
|
+
marks an entry as most recently used; `#has_key?`, `#has_value?` and iteration
|
|
52
|
+
do not. Like `Hash.new`, it takes an optional default value or block. Set
|
|
53
|
+
`#release_proc` to be told about every entry that leaves the cache:
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
cache.release_proc = ->(key, value) { value.close }
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
An LRUHash is not thread-safe; guard it with a `Mutex` if threads share it.
|
|
60
|
+
|
|
61
|
+
|
|
36
62
|
### Core Extensions
|
|
37
63
|
|
|
38
64
|
Hashery adds four core extensions of Ruby's Hash class: `#retrieve`,
|
|
@@ -45,17 +71,15 @@ sorely missing for Ruby itself, which is why they are provided here.
|
|
|
45
71
|
|
|
46
72
|
## Installation
|
|
47
73
|
|
|
48
|
-
To install with RubyGems simply
|
|
74
|
+
Hashery requires Ruby 2.7 or newer. To install with RubyGems simply
|
|
75
|
+
open a console and type:
|
|
49
76
|
|
|
50
|
-
$
|
|
77
|
+
$ gem install hashery
|
|
51
78
|
|
|
52
79
|
Or add it as a dependency to your project's Gemfile:
|
|
53
80
|
|
|
54
81
|
gem "hashery"
|
|
55
82
|
|
|
56
|
-
Tarball packages are available for manual site installations
|
|
57
|
-
via [Ruby Setup](http://rubyworks.github.com/setup).
|
|
58
|
-
|
|
59
83
|
|
|
60
84
|
## Authors
|
|
61
85
|
|
|
@@ -77,41 +101,34 @@ Well *git* in there!
|
|
|
77
101
|
|
|
78
102
|
### Communicating
|
|
79
103
|
|
|
80
|
-
The project is hosted on
|
|
81
|
-
is via the [Issues](
|
|
82
|
-
For more direct communication you can try the **#rubyworks** IRC channel
|
|
83
|
-
on **irc.freenode.net**.
|
|
84
|
-
|
|
85
|
-
[](https://kiwiirc.com/client/irc.freenode.net/?nick=user1|?#rubyworks)
|
|
104
|
+
The project is hosted on GitHub so the easiest means of communication
|
|
105
|
+
is via the [Issues](https://github.com/rubyworks/hashery/issues) page.
|
|
86
106
|
|
|
87
107
|
### Testing
|
|
88
108
|
|
|
89
|
-
Hashery uses [QED](
|
|
90
|
-
[Lemon](
|
|
109
|
+
Hashery uses [QED](https://rubyworks.github.io/qed) and
|
|
110
|
+
[Lemon](https://rubyworks.github.io/lemon) test frameworks.
|
|
91
111
|
The QED framework to create well tested high-level documentation.
|
|
92
|
-
|
|
112
|
+
Lemon is used to create low-level unit tests. Install the development
|
|
113
|
+
dependencies and run both via:
|
|
114
|
+
|
|
115
|
+
$ bundle install
|
|
116
|
+
$ bundle exec rake
|
|
93
117
|
|
|
94
|
-
|
|
118
|
+
Or run the QED specs on their own via:
|
|
95
119
|
|
|
96
|
-
|
|
120
|
+
$ bundle exec qed -Ilib demo/
|
|
97
121
|
|
|
98
|
-
|
|
122
|
+
And the Lemon unit tests via Rubytest:
|
|
123
|
+
|
|
124
|
+
$ bundle exec rubytest -Ilib -Itest test/
|
|
99
125
|
|
|
100
126
|
### Patches
|
|
101
127
|
|
|
102
|
-
Hashery's repository is hosted on [GitHub](
|
|
128
|
+
Hashery's repository is hosted on [GitHub](https://github.com/rubyworks/hashery).
|
|
103
129
|
If you'd like to offer up a fix or feature, fork the repo and submit a pull
|
|
104
130
|
request (preferably in a topic branch). I assume you have heard
|
|
105
|
-
all the talk about proper
|
|
106
|
-
so I won't bug you with it yet again.
|
|
107
|
-
|
|
108
|
-
### Donations
|
|
109
|
-
|
|
110
|
-
Yes, we FOSS programmers need to eat too! ;-) No seriously, any help you can
|
|
111
|
-
offer goes a long way toward continued development of Rubyworks projects,
|
|
112
|
-
including Hashery. Hop over to our [Gittips](https://www.gittip.com/rubyworksorg/)
|
|
113
|
-
page, or see the upper right-hand corner on the [Rubyworks](http://rubyworks.github.com) homepage.
|
|
114
|
-
Thanks!
|
|
131
|
+
all the talk about proper practices, so I won't bug you with it yet again.
|
|
115
132
|
|
|
116
133
|
|
|
117
134
|
## Copyrights
|
data/lib/hashery/core_ext.rb
CHANGED
data/lib/hashery/crud_hash.rb
CHANGED
|
@@ -324,7 +324,7 @@ module Hashery
|
|
|
324
324
|
#
|
|
325
325
|
def replace(other)
|
|
326
326
|
super cast(other)
|
|
327
|
-
|
|
327
|
+
end
|
|
328
328
|
|
|
329
329
|
#
|
|
330
330
|
# Get the values at.
|
|
@@ -334,7 +334,7 @@ module Hashery
|
|
|
334
334
|
# Returns `Array` of values.
|
|
335
335
|
#
|
|
336
336
|
def values_at(*keys)
|
|
337
|
-
super
|
|
337
|
+
super(*keys.map{ |key| cast_key(key) })
|
|
338
338
|
end
|
|
339
339
|
|
|
340
340
|
# Convert CRUDHash to regular Hash.
|
data/lib/hashery/dictionary.rb
CHANGED
|
@@ -128,15 +128,25 @@ module Hashery
|
|
|
128
128
|
def initialize(*args, &blk)
|
|
129
129
|
@order = []
|
|
130
130
|
@order_by = nil
|
|
131
|
+
@block = blk
|
|
131
132
|
if blk
|
|
132
|
-
|
|
133
|
-
oblk = lambda{ |hsh, key| blk[dict,key] } # dictionary rather then just the interal hash.
|
|
134
|
-
@hash = Hash.new(*args, &oblk)
|
|
133
|
+
@hash = Hash.new(*args, &default_proc_for(blk))
|
|
135
134
|
else
|
|
136
135
|
@hash = Hash.new(*args)
|
|
137
136
|
end
|
|
138
137
|
end
|
|
139
138
|
|
|
139
|
+
#
|
|
140
|
+
# Make a copy independent of the original, as #dup and #clone do. It
|
|
141
|
+
# keeps the original's order, sort rule and default.
|
|
142
|
+
#
|
|
143
|
+
def initialize_copy(source)
|
|
144
|
+
super
|
|
145
|
+
@order = source.order.dup
|
|
146
|
+
@hash = @hash.dup
|
|
147
|
+
@hash.default_proc = default_proc_for(@block) if @block
|
|
148
|
+
end
|
|
149
|
+
|
|
140
150
|
#
|
|
141
151
|
# Order of keys.
|
|
142
152
|
#
|
|
@@ -148,16 +158,20 @@ module Hashery
|
|
|
148
158
|
end
|
|
149
159
|
|
|
150
160
|
#
|
|
151
|
-
# Keep dictionary sorted by a specific sort order.
|
|
161
|
+
# Keep dictionary sorted by a specific sort order. Without a block,
|
|
162
|
+
# stop sorting and keep the current order.
|
|
163
|
+
#
|
|
164
|
+
# If the entries can't be sorted this way (a +nil+ key among others,
|
|
165
|
+
# or keys of types that don't compare), it raises and the dictionary
|
|
166
|
+
# keeps its previous order. While a sort order is set, adding an entry
|
|
167
|
+
# it can't place raises too, and nothing is stored.
|
|
152
168
|
#
|
|
153
169
|
# block - Ordering procedure.
|
|
154
170
|
#
|
|
155
171
|
# Returns +self+.
|
|
156
172
|
#
|
|
157
173
|
def order_by( &block )
|
|
158
|
-
|
|
159
|
-
order
|
|
160
|
-
self
|
|
174
|
+
sort_with(block)
|
|
161
175
|
end
|
|
162
176
|
|
|
163
177
|
#
|
|
@@ -179,12 +193,10 @@ module Hashery
|
|
|
179
193
|
#
|
|
180
194
|
def order_by_key
|
|
181
195
|
if block_given?
|
|
182
|
-
|
|
196
|
+
sort_with(Proc.new{ |k,v| yield(k) })
|
|
183
197
|
else
|
|
184
|
-
|
|
198
|
+
sort_with(Proc.new{ |k,v| k })
|
|
185
199
|
end
|
|
186
|
-
order
|
|
187
|
-
self
|
|
188
200
|
end
|
|
189
201
|
|
|
190
202
|
#
|
|
@@ -202,12 +214,10 @@ module Hashery
|
|
|
202
214
|
#
|
|
203
215
|
def order_by_value
|
|
204
216
|
if block_given?
|
|
205
|
-
|
|
217
|
+
sort_with(Proc.new{ |k,v| yield(v) })
|
|
206
218
|
else
|
|
207
|
-
|
|
219
|
+
sort_with(Proc.new{ |k,v| v })
|
|
208
220
|
end
|
|
209
|
-
order
|
|
210
|
-
self
|
|
211
221
|
end
|
|
212
222
|
|
|
213
223
|
#
|
|
@@ -231,7 +241,7 @@ module Hashery
|
|
|
231
241
|
#
|
|
232
242
|
def ==(hsh2)
|
|
233
243
|
if hsh2.is_a?( Dictionary )
|
|
234
|
-
|
|
244
|
+
order == hsh2.order &&
|
|
235
245
|
@hash == hsh2.instance_variable_get("@hash")
|
|
236
246
|
else
|
|
237
247
|
false
|
|
@@ -261,16 +271,18 @@ module Hashery
|
|
|
261
271
|
#
|
|
262
272
|
# h[key,index] = value
|
|
263
273
|
#
|
|
264
|
-
def []=(k, i=nil, v=
|
|
265
|
-
if v
|
|
266
|
-
insert(i,k,v)
|
|
267
|
-
else
|
|
274
|
+
def []=(k, i=nil, v=NO_VALUE)
|
|
275
|
+
if NO_VALUE.equal?(v)
|
|
268
276
|
store(k,i)
|
|
277
|
+
else
|
|
278
|
+
insert(i,k,v)
|
|
269
279
|
end
|
|
270
280
|
end
|
|
271
281
|
|
|
272
282
|
#
|
|
273
|
-
# Insert entry into dictionary at specific index position.
|
|
283
|
+
# Insert entry into dictionary at specific index position. If +key+ is
|
|
284
|
+
# already present, it moves to that position. An index past the end
|
|
285
|
+
# adds the entry at the end.
|
|
274
286
|
#
|
|
275
287
|
# index - [Integer] Position of order placement.
|
|
276
288
|
# key - [Object] Key to associate with value.
|
|
@@ -279,6 +291,9 @@ module Hashery
|
|
|
279
291
|
# Returns `value` stored.
|
|
280
292
|
#
|
|
281
293
|
def insert(index, key, value)
|
|
294
|
+
check_order(key, value)
|
|
295
|
+
remove_from_order(key) if @hash.key?(key)
|
|
296
|
+
index = @order.size if index > @order.size
|
|
282
297
|
@order.insert(index, key)
|
|
283
298
|
@hash.store(key, value)
|
|
284
299
|
end
|
|
@@ -289,6 +304,7 @@ module Hashery
|
|
|
289
304
|
# Returns `value`.
|
|
290
305
|
#
|
|
291
306
|
def store(key, value)
|
|
307
|
+
check_order(key, value)
|
|
292
308
|
@order.push(key) unless @hash.has_key?(key)
|
|
293
309
|
@hash.store(key, value)
|
|
294
310
|
end
|
|
@@ -305,7 +321,7 @@ module Hashery
|
|
|
305
321
|
# Delete the entry with given +key+.
|
|
306
322
|
#
|
|
307
323
|
def delete(key)
|
|
308
|
-
@
|
|
324
|
+
remove_from_order(key) if @hash.key?(key)
|
|
309
325
|
@hash.delete(key)
|
|
310
326
|
end
|
|
311
327
|
|
|
@@ -385,41 +401,50 @@ module Hashery
|
|
|
385
401
|
end
|
|
386
402
|
|
|
387
403
|
#
|
|
388
|
-
#
|
|
389
|
-
#
|
|
390
|
-
# Returns [Hash] of rejected entries.
|
|
404
|
+
# Remove entries for which the block is true.
|
|
391
405
|
#
|
|
392
|
-
#
|
|
406
|
+
# Returns +self+, or +nil+ if nothing was removed.
|
|
393
407
|
#
|
|
394
408
|
def reject!( &block )
|
|
395
|
-
|
|
396
|
-
|
|
409
|
+
before = length
|
|
410
|
+
delete_if(&block)
|
|
411
|
+
length == before ? nil : self
|
|
397
412
|
end
|
|
398
413
|
|
|
399
414
|
#
|
|
400
|
-
# Replace dictionary entries with new table.
|
|
415
|
+
# Replace dictionary entries with new table. The dictionary copies the
|
|
416
|
+
# entries, so later changes to either one don't affect the other. If a
|
|
417
|
+
# sort order is set and can't sort the new entries, it raises and the
|
|
418
|
+
# dictionary keeps its old entries.
|
|
401
419
|
#
|
|
402
420
|
def replace(hsh2)
|
|
421
|
+
old_hash, old_order = @hash, @order
|
|
403
422
|
case hsh2
|
|
404
423
|
when Dictionary
|
|
405
|
-
@order = hsh2.order
|
|
424
|
+
@order = hsh2.order.dup
|
|
406
425
|
@hash = hsh2.to_h
|
|
407
426
|
when Hash
|
|
408
|
-
@hash = hsh2
|
|
427
|
+
@hash = hsh2.dup
|
|
409
428
|
@order = @hash.keys
|
|
410
429
|
else
|
|
411
430
|
@hash = hsh2.to_h
|
|
412
431
|
@order = @hash.keys
|
|
413
432
|
end
|
|
414
433
|
reorder
|
|
434
|
+
rescue Exception
|
|
435
|
+
@hash, @order = old_hash, old_order
|
|
436
|
+
raise
|
|
415
437
|
end
|
|
416
438
|
|
|
417
439
|
#
|
|
418
440
|
# Remove entry from the to top of dictionary.
|
|
419
441
|
#
|
|
442
|
+
# Returns the [key, value] pair, or +nil+ if the dictionary is empty.
|
|
443
|
+
#
|
|
420
444
|
def shift
|
|
445
|
+
return nil if empty?
|
|
421
446
|
key = order.first
|
|
422
|
-
|
|
447
|
+
[key,delete(key)]
|
|
423
448
|
end
|
|
424
449
|
|
|
425
450
|
#
|
|
@@ -427,6 +452,7 @@ module Hashery
|
|
|
427
452
|
#
|
|
428
453
|
def unshift( k,v )
|
|
429
454
|
unless @hash.include?( k )
|
|
455
|
+
check_order( k,v )
|
|
430
456
|
@order.unshift( k )
|
|
431
457
|
@hash.store( k,v )
|
|
432
458
|
true
|
|
@@ -447,6 +473,7 @@ module Hashery
|
|
|
447
473
|
#
|
|
448
474
|
def push(k,v)
|
|
449
475
|
unless @hash.include?( k )
|
|
476
|
+
check_order( k,v )
|
|
450
477
|
@order.push( k )
|
|
451
478
|
@hash.store( k,v )
|
|
452
479
|
true
|
|
@@ -458,9 +485,12 @@ module Hashery
|
|
|
458
485
|
#
|
|
459
486
|
# Pop entry off the bottom of dictionary.
|
|
460
487
|
#
|
|
488
|
+
# Returns the [key, value] pair, or +nil+ if the dictionary is empty.
|
|
489
|
+
#
|
|
461
490
|
def pop
|
|
491
|
+
return nil if empty?
|
|
462
492
|
key = order.last
|
|
463
|
-
|
|
493
|
+
[key,delete(key)]
|
|
464
494
|
end
|
|
465
495
|
|
|
466
496
|
#
|
|
@@ -474,17 +504,6 @@ module Hashery
|
|
|
474
504
|
'{' + ary.join(", ") + '}'
|
|
475
505
|
end
|
|
476
506
|
|
|
477
|
-
#
|
|
478
|
-
# Duplicate dictionary.
|
|
479
|
-
#
|
|
480
|
-
# Returns [Dictionary].
|
|
481
|
-
#
|
|
482
|
-
def dup
|
|
483
|
-
a = []
|
|
484
|
-
each{ |k,v| a << k; a << v }
|
|
485
|
-
self.class[*a]
|
|
486
|
-
end
|
|
487
|
-
|
|
488
507
|
#
|
|
489
508
|
# Update dictionary with other hash.
|
|
490
509
|
#
|
|
@@ -529,12 +548,15 @@ module Hashery
|
|
|
529
548
|
end
|
|
530
549
|
|
|
531
550
|
#
|
|
532
|
-
# Reverse the order of duplicte dictionary.
|
|
551
|
+
# Reverse the order of duplicte dictionary. The copy doesn't keep the
|
|
552
|
+
# sort order, if one is set, since that would undo the reversal.
|
|
533
553
|
#
|
|
534
554
|
# Returns [Dictionary].
|
|
535
555
|
#
|
|
536
556
|
def reverse
|
|
537
|
-
dup
|
|
557
|
+
copy = dup
|
|
558
|
+
copy.order_by
|
|
559
|
+
copy.reverse!
|
|
538
560
|
end
|
|
539
561
|
|
|
540
562
|
#
|
|
@@ -636,6 +658,63 @@ module Hashery
|
|
|
636
658
|
@hash
|
|
637
659
|
end
|
|
638
660
|
|
|
661
|
+
private
|
|
662
|
+
|
|
663
|
+
# Marks the value argument of #[]= as not given, since +nil+ and
|
|
664
|
+
# +false+ are values that can be stored.
|
|
665
|
+
NO_VALUE = Object.new.freeze
|
|
666
|
+
private_constant :NO_VALUE
|
|
667
|
+
|
|
668
|
+
#
|
|
669
|
+
# Default proc for the internal hash, which passes the block this
|
|
670
|
+
# dictionary rather than the internal hash, so automatic key entry
|
|
671
|
+
# goes through the dictionary.
|
|
672
|
+
#
|
|
673
|
+
def default_proc_for(blk)
|
|
674
|
+
dict = self
|
|
675
|
+
lambda{ |hsh, key| blk[dict,key] }
|
|
676
|
+
end
|
|
677
|
+
|
|
678
|
+
#
|
|
679
|
+
# Set the sort rule and sort by it. If sorting fails, put back the
|
|
680
|
+
# previous rule before raising, so the dictionary stays readable.
|
|
681
|
+
#
|
|
682
|
+
def sort_with(rule)
|
|
683
|
+
previous, @order_by = @order_by, rule
|
|
684
|
+
begin
|
|
685
|
+
order
|
|
686
|
+
rescue Exception
|
|
687
|
+
@order_by = previous
|
|
688
|
+
raise
|
|
689
|
+
end
|
|
690
|
+
self
|
|
691
|
+
end
|
|
692
|
+
|
|
693
|
+
#
|
|
694
|
+
# While a sort rule is set, raise before storing an entry the rule
|
|
695
|
+
# can't place among the others, since every read would fail after.
|
|
696
|
+
# Comparing with one other entry is enough to catch a +nil+, or a key
|
|
697
|
+
# of a type that doesn't compare with the rest, without sorting on
|
|
698
|
+
# every write.
|
|
699
|
+
#
|
|
700
|
+
def check_order(key, value)
|
|
701
|
+
return unless @order_by
|
|
702
|
+
i = @order.index{ |k| !k.eql?(key) }
|
|
703
|
+
return unless i
|
|
704
|
+
other = @order[i]
|
|
705
|
+
[[key, value], [other, @hash[other]]].sort_by(&@order_by)
|
|
706
|
+
nil
|
|
707
|
+
end
|
|
708
|
+
|
|
709
|
+
#
|
|
710
|
+
# Remove +key+ from the order, matching it as the hash does (with
|
|
711
|
+
# #eql?), so a key like 1.0 isn't taken for 1.
|
|
712
|
+
#
|
|
713
|
+
def remove_from_order(key)
|
|
714
|
+
i = @order.index{ |k| k.eql?(key) }
|
|
715
|
+
@order.delete_at(i) if i
|
|
716
|
+
end
|
|
717
|
+
|
|
639
718
|
end
|
|
640
719
|
|
|
641
720
|
end
|
data/lib/hashery/fuzzy_hash.rb
CHANGED
data/lib/hashery/ini_hash.rb
CHANGED
|
@@ -115,7 +115,7 @@ module Hashery
|
|
|
115
115
|
@path = path if String === path
|
|
116
116
|
@inihash = (Hash === path ? path.dup : {})
|
|
117
117
|
|
|
118
|
-
if load or ( load.nil? and FileTest.readable_real? @path )
|
|
118
|
+
if load or ( load.nil? and @path and FileTest.readable_real? @path )
|
|
119
119
|
restore()
|
|
120
120
|
end
|
|
121
121
|
end
|
|
@@ -185,7 +185,7 @@ module Hashery
|
|
|
185
185
|
# Returns a string in the ini file format.
|
|
186
186
|
#
|
|
187
187
|
def to_s
|
|
188
|
-
str = ""
|
|
188
|
+
str = +""
|
|
189
189
|
inihash.each do |key, value|
|
|
190
190
|
if value.is_a? Hash
|
|
191
191
|
str << "[#{key.to_s}]\n"
|
|
@@ -259,7 +259,7 @@ module Hashery
|
|
|
259
259
|
# Returns a `String` with the comments from the beginning of the INI file.
|
|
260
260
|
#
|
|
261
261
|
def self.read_comment_from_file(path)
|
|
262
|
-
comment = ""
|
|
262
|
+
comment = +""
|
|
263
263
|
|
|
264
264
|
IO.foreach(path) do |line|
|
|
265
265
|
line.strip!
|
|
@@ -289,7 +289,7 @@ module Hashery
|
|
|
289
289
|
File.open(path, "w") { |file|
|
|
290
290
|
|
|
291
291
|
unless comment.nil?
|
|
292
|
-
comment.
|
|
292
|
+
comment.each_line do |line|
|
|
293
293
|
file << "# #{line}"
|
|
294
294
|
end
|
|
295
295
|
end
|