hash_cabinet 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +0 -34
- data/lib/hash_cabinet.rb +56 -18
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4938741a78a6296230b6c7f4585b8dec8cf1ee3da6e7680e13d9192bb4110782
|
4
|
+
data.tar.gz: 92fc7f0ade034f713f7516a54e7a087dbed103853699de85236543eaec38579b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 02a46e648da4b005b1581660392493af0e35bd32de322d45237ee53948bfbe849c4ce75e5db301199c044e58adb5c304c8bc8caddea72c68dc9f647ff238cd63
|
7
|
+
data.tar.gz: 12f82246384e4887f3ecd99932a3fadb74e39552eddd5e314c27fe83a78f7c22ee24283796ff236017594ba0a4245ff2b96c441d7b9eeacda56a61018dcccebc
|
data/README.md
CHANGED
@@ -49,40 +49,6 @@ p cabinet.to_h
|
|
49
49
|
|
50
50
|
```
|
51
51
|
|
52
|
-
Quick Reference
|
53
|
-
--------------------------------------------------
|
54
|
-
|
55
|
-
| Method | Description |
|
56
|
-
|--------|-------------|
|
57
|
-
| `cabinet.transaction { ... }` | Yields the `SDBM` object. (like `SDBM.open`) |
|
58
|
-
| `cabinet[key]` | Returns the value at key |
|
59
|
-
| `cabinet[key] = value` | Saves the value at key |
|
60
|
-
| `cabinet.clear` | Deletes all data |
|
61
|
-
| `cabinet.delete key` | Deletes a key |
|
62
|
-
| `cabinet.delete_if { \|k, v\| ... }`| Deletes keys based on the block result |
|
63
|
-
| `cabinet.each { \|k, v\| ... }` | Iterates over the data |
|
64
|
-
| `cabinet.each_key { \|k\| ... }` | Iterates over the keys |
|
65
|
-
| `cabinet.each_value { \|v\| ... }` | Iterates over the values |
|
66
|
-
| `cabinet.emoty?` | Returns true if the database is empty |
|
67
|
-
| `cabinet.has_key? key` | Returns true if the key exists |
|
68
|
-
| `cabinet.has_value? value` | Returns true if the value exists |
|
69
|
-
| `cabinet.include? key` | Same as `cabinet.has_key?` |
|
70
|
-
| `cabinet.key value` | Returns the key associated with the value |
|
71
|
-
| `cabinet.key? key` | Same as `cabinet.has_key?` |
|
72
|
-
| `cabinet.keys` | Returns all the keys |
|
73
|
-
| `cabinet.length` | Returns the number of key-value pairs |
|
74
|
-
| `cabinet.replace data` | Reset the database with new data |
|
75
|
-
| `cabinet.select { \|k, v\| ... }` | Returns a hash based on the block result |
|
76
|
-
| `cabinet.shift` | Removes and returns one key-value pair |
|
77
|
-
| `cabinet.size` | Same as `cabinet.length` |
|
78
|
-
| `cabinet.to_a` | Returns an array of `[key, value]` pairs |
|
79
|
-
| `cabinet.to_h` | Returns a hash with all key-value pairs |
|
80
|
-
| `cabinet.update data` | Insert or update new data |
|
81
|
-
| `cabinet.value? value` | Returns true if the value is in the database |
|
82
|
-
| `cabinet.values` | Returns an array of all the values |
|
83
|
-
| `cabinet.values_at key, ...` | Returns an array of values corresponding to the given keys |
|
84
|
-
|
85
|
-
|
86
52
|
Documentation
|
87
53
|
--------------------------------------------------
|
88
54
|
|
data/lib/hash_cabinet.rb
CHANGED
@@ -28,17 +28,20 @@ class HashCabinet
|
|
28
28
|
@path = path
|
29
29
|
end
|
30
30
|
|
31
|
-
# Yields the +SDBM+ object to the block.
|
32
|
-
# method should not be used directly, as it is used by all other methods.
|
33
|
-
#
|
34
|
-
# Example:
|
31
|
+
# Yields the +SDBM+ object to the block.
|
35
32
|
#
|
33
|
+
# @example
|
36
34
|
# cabinet = HashCabinet.new 'filename'
|
37
35
|
#
|
38
36
|
# cabinet.transaction do |db|
|
39
37
|
# db.clear
|
40
38
|
# end
|
41
39
|
#
|
40
|
+
# @note
|
41
|
+
# Under most circumstances, this method should not be used directly,
|
42
|
+
# as it is used by all other methods.
|
43
|
+
#
|
44
|
+
# @yieldparam [SDBM] db the {SDBM} instance
|
42
45
|
def transaction(&block)
|
43
46
|
SDBM.open path, &block
|
44
47
|
end
|
@@ -65,6 +68,16 @@ class HashCabinet
|
|
65
68
|
|
66
69
|
# Iterates over the key-value pairs in the database, deleting those for
|
67
70
|
# which the block returns true.
|
71
|
+
#
|
72
|
+
# @example Delete all records with +age < 18+.
|
73
|
+
# cabinet = HashCabinet.new 'filename'
|
74
|
+
#
|
75
|
+
# cabinet.delete_if do |key, value|
|
76
|
+
# value[:age] < 18
|
77
|
+
# end
|
78
|
+
#
|
79
|
+
# @yieldparam [String] key the pair key
|
80
|
+
# @yieldparam [Object] value the pair value
|
68
81
|
def delete_if(&block)
|
69
82
|
transaction do |db|
|
70
83
|
db.delete_if do |key, value|
|
@@ -74,6 +87,9 @@ class HashCabinet
|
|
74
87
|
end
|
75
88
|
|
76
89
|
# Iterates over each key-value pair in the database.
|
90
|
+
#
|
91
|
+
# @yieldparam [String] key the pair key
|
92
|
+
# @yieldparam [Object] value the pair value
|
77
93
|
def each(&block)
|
78
94
|
transaction do |db|
|
79
95
|
db.each do |key, value|
|
@@ -83,11 +99,15 @@ class HashCabinet
|
|
83
99
|
end
|
84
100
|
|
85
101
|
# Iterates over each key in the database.
|
102
|
+
#
|
103
|
+
# @yieldparam [String] key the pair key
|
86
104
|
def each_key(&block)
|
87
105
|
transaction { |db| db.each_key &block }
|
88
106
|
end
|
89
107
|
|
90
108
|
# Iterates over each key-value pair in the database.
|
109
|
+
#
|
110
|
+
# @yieldparam [Object] value the pair value
|
91
111
|
def each_value(&block)
|
92
112
|
transaction do |db|
|
93
113
|
db.each_value do |value|
|
@@ -96,36 +116,39 @@ class HashCabinet
|
|
96
116
|
end
|
97
117
|
end
|
98
118
|
|
99
|
-
#
|
119
|
+
# @return [Boolean] +true+ if the database is empty.
|
100
120
|
def empty?
|
101
121
|
transaction { |db| db.empty? }
|
102
122
|
end
|
103
123
|
|
104
|
-
#
|
124
|
+
# @return [Boolean] +true+ if the database contains the given key.
|
105
125
|
def has_key?(key)
|
106
126
|
transaction { |db| db.has_key? key.to_s }
|
107
127
|
end
|
108
128
|
alias include? has_key?
|
109
129
|
alias key? has_key?
|
110
130
|
|
111
|
-
#
|
131
|
+
# @return [Boolean] +true+ if the database contains the given value.
|
112
132
|
def has_value?(value)
|
113
133
|
transaction { |db| db.has_value? value.to_yaml }
|
114
134
|
end
|
115
135
|
|
116
|
-
# Returns the key associated with the given value.
|
117
|
-
#
|
136
|
+
# Returns the key associated with the given value.
|
137
|
+
#
|
138
|
+
# If more than one key corresponds to the given value, then the first key will be returned.
|
118
139
|
# If no keys are found, +nil+ will be returned.
|
140
|
+
#
|
141
|
+
# @return [String] the key associated with the given value.
|
119
142
|
def key(value)
|
120
143
|
transaction { |db| db.key value.to_yaml }
|
121
144
|
end
|
122
145
|
|
123
|
-
#
|
146
|
+
# @return [Array] a new Array containing the keys in the database.
|
124
147
|
def keys
|
125
148
|
transaction { |db| db.keys }
|
126
149
|
end
|
127
150
|
|
128
|
-
#
|
151
|
+
# @return [Integer] the number of keys in the database.
|
129
152
|
def length
|
130
153
|
transaction { |db| db.length }
|
131
154
|
end
|
@@ -138,6 +161,12 @@ class HashCabinet
|
|
138
161
|
# method, such as a Hash, or with any object that implements an +#each+
|
139
162
|
# method, such as an Array. In this case, the array will be converted to
|
140
163
|
# a `key=key` hash before storing it.
|
164
|
+
#
|
165
|
+
# @example
|
166
|
+
# cabinet = HashCabinet.new 'filename'
|
167
|
+
# cabinet.replace key1: 'value1', key2: 'value2'
|
168
|
+
#
|
169
|
+
# @param [Object] data the data to store
|
141
170
|
def replace(data)
|
142
171
|
if !data.respond_to? :each_pair and data.respond_to? :each
|
143
172
|
data = array_to_hash data
|
@@ -147,7 +176,7 @@ class HashCabinet
|
|
147
176
|
transaction { |db| db.replace data }
|
148
177
|
end
|
149
178
|
|
150
|
-
#
|
179
|
+
# @return [Hash] a new Hash of key-value pairs for which the block returns true.
|
151
180
|
def select(&block)
|
152
181
|
transaction do |db|
|
153
182
|
db.select do |key, value|
|
@@ -159,6 +188,8 @@ class HashCabinet
|
|
159
188
|
# Removes a key-value pair from the database and returns them as an Array.
|
160
189
|
#
|
161
190
|
# If the database is empty, returns nil.
|
191
|
+
#
|
192
|
+
# @return [Array] the key and value.
|
162
193
|
def shift
|
163
194
|
transaction do |db|
|
164
195
|
result = db.shift
|
@@ -166,26 +197,33 @@ class HashCabinet
|
|
166
197
|
end
|
167
198
|
end
|
168
199
|
|
169
|
-
#
|
200
|
+
# @return [Array] a new array containing each key-value pair in the
|
201
|
+
# database.
|
170
202
|
def to_a
|
171
203
|
transaction do |db|
|
172
204
|
db.to_a.map { |pair| [pair[0], pair[1].from_yaml] }
|
173
205
|
end
|
174
206
|
end
|
175
207
|
|
176
|
-
#
|
208
|
+
# @return [hash] a new hash containing each key-value pair in the database.
|
177
209
|
def to_h
|
178
210
|
transaction do |db|
|
179
211
|
db.to_h.transform_values &:from_yaml
|
180
212
|
end
|
181
213
|
end
|
182
214
|
|
183
|
-
#
|
215
|
+
# Inserts or updates key-value pairs.
|
184
216
|
#
|
185
217
|
# This method will work with any object which implements an +#each_pair+
|
186
218
|
# method, such as a Hash, or with any object that implements an +#each+
|
187
219
|
# method, such as an Array. In this case, the array will be converted to
|
188
220
|
# a `key=key` hash before storing it.
|
221
|
+
#
|
222
|
+
# @example
|
223
|
+
# cabinet = HashCabinet.new 'filename'
|
224
|
+
# cabinet.update key1: 'value1', key2: 'value2'
|
225
|
+
#
|
226
|
+
# @param [Object] data the data to store
|
189
227
|
def update(data)
|
190
228
|
if !data.respond_to? :each_pair and data.respond_to? :each
|
191
229
|
data = array_to_hash data
|
@@ -195,19 +233,19 @@ class HashCabinet
|
|
195
233
|
transaction { |db| db.update data }
|
196
234
|
end
|
197
235
|
|
198
|
-
#
|
236
|
+
# @return [Boolean] +true+ if the database contains the given value.
|
199
237
|
def value?(value)
|
200
238
|
transaction { |db| db.value? value.to_yaml }
|
201
239
|
end
|
202
240
|
|
203
|
-
#
|
241
|
+
# @return [Array] a new array containing the values in the database.
|
204
242
|
def values
|
205
243
|
transaction do |db|
|
206
244
|
db.values.map &:from_yaml
|
207
245
|
end
|
208
246
|
end
|
209
247
|
|
210
|
-
#
|
248
|
+
# @return [Array] an Array of values corresponding to the given keys.
|
211
249
|
def values_at(*key)
|
212
250
|
transaction do |db|
|
213
251
|
db.values_at(*(key.map &:to_s)).map &:from_yaml
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_cabinet
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danny Ben Shitrit
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-09-
|
11
|
+
date: 2019-09-19 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Store objects in a file using hash-like syntax
|
14
14
|
email: db@dannyben.com
|