hash_cabinet 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +49 -3
- data/lib/hash_cabinet.rb +41 -30
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4bddfb98bc67f51e3aabf476d95da84990f5e271ad26d404e5f5e2722b95275d
|
4
|
+
data.tar.gz: c4a45182a0becb13ddf7915b8c0aa28c2fd43cd1571185f4afff1da21032f702
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0ed28a440b890b56a368619450200c89c5b748c63f6fe52223a3c0767c0403d81771587a062c613a8f681dc06f20a708cca5c2fc10f1420eec5965b229a7926f
|
7
|
+
data.tar.gz: '09684dd6832e8a5bb6156c483c7e1a8a0645215394b7d48e288be4ad654ecc6f68ab89b9082329e745eb9f3e338582034397d4b562dbc0daa4c402cd6753d866'
|
data/README.md
CHANGED
@@ -2,6 +2,8 @@ Hash Cabinet - File based key-object store
|
|
2
2
|
==================================================
|
3
3
|
|
4
4
|
[![Gem Version](https://badge.fury.io/rb/hash_cabinet.svg)](https://badge.fury.io/rb/hash_cabinet)
|
5
|
+
[![Build Status](https://travis-ci.com/DannyBen/hash_cabinet.svg?branch=master)](https://travis-ci.com/DannyBen/hash_cabinet)
|
6
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/c69f9676cd8cd5fc33bc/maintainability)](https://codeclimate.com/github/DannyBen/hash_cabinet/maintainability)
|
5
7
|
|
6
8
|
---
|
7
9
|
|
@@ -29,10 +31,12 @@ Usage
|
|
29
31
|
--------------------------------------------------
|
30
32
|
|
31
33
|
```ruby
|
34
|
+
require 'hash_cabinet'
|
35
|
+
|
32
36
|
cabinet = HashCabinet.new 'dbfile'
|
33
37
|
|
34
38
|
# Store values
|
35
|
-
cabinet['some-key'] = 'some
|
39
|
+
cabinet['some-key'] = 'some-value'
|
36
40
|
cabinet['another-key'] = { color: 'yellow' }
|
37
41
|
|
38
42
|
# Retrieve values
|
@@ -41,8 +45,50 @@ p cabinet['another-key']
|
|
41
45
|
|
42
46
|
# Show all values
|
43
47
|
p cabinet.to_h
|
44
|
-
#=> {"some-key"=>"some=value", "another-key"=>{:color=>"yellow"}
|
48
|
+
#=> {"some-key"=>"some=value", "another-key"=>{:color=>"yellow"}}
|
45
49
|
|
46
50
|
```
|
47
51
|
|
48
|
-
|
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
|
+
Documentation
|
87
|
+
--------------------------------------------------
|
88
|
+
|
89
|
+
- [Documentation on RubyDoc][docs]
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
[SDBM]: https://ruby-doc.org/stdlib-2.6.3/libdoc/sdbm/rdoc/SDBM.html
|
94
|
+
[docs]: https://rubydoc.info/gems/hash_cabinet/HashCabinet
|
data/lib/hash_cabinet.rb
CHANGED
@@ -3,8 +3,8 @@ require 'yaml'
|
|
3
3
|
|
4
4
|
class HashCabinet
|
5
5
|
|
6
|
-
# Refinements
|
7
|
-
module
|
6
|
+
# Refinements for internal use
|
7
|
+
module Refinements
|
8
8
|
refine String do
|
9
9
|
def from_yaml
|
10
10
|
YAML.load self
|
@@ -18,7 +18,7 @@ class HashCabinet
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
using
|
21
|
+
using Refinements
|
22
22
|
|
23
23
|
# Returns the path to the database file.
|
24
24
|
attr_reader :path
|
@@ -45,12 +45,12 @@ class HashCabinet
|
|
45
45
|
|
46
46
|
# Returns the value in the database associated with the given +key+.
|
47
47
|
def [](key)
|
48
|
-
transaction { |db| db[key].from_yaml }
|
48
|
+
transaction { |db| db[key.to_s].from_yaml }
|
49
49
|
end
|
50
50
|
|
51
51
|
# Inserts or updates a value in the database with the given key as an index.
|
52
52
|
def []=(key, value)
|
53
|
-
transaction { |db| db[key] = value.to_yaml }
|
53
|
+
transaction { |db| db[key.to_s] = value.to_yaml }
|
54
54
|
end
|
55
55
|
|
56
56
|
# Deletes all key-value pairs from the database.
|
@@ -60,7 +60,7 @@ class HashCabinet
|
|
60
60
|
|
61
61
|
# Deletes the given `key` from the database.
|
62
62
|
def delete(key)
|
63
|
-
transaction { |db| db.delete key }
|
63
|
+
transaction { |db| db.delete key.to_s }
|
64
64
|
end
|
65
65
|
|
66
66
|
# Iterates over the key-value pairs in the database, deleting those for
|
@@ -81,13 +81,11 @@ class HashCabinet
|
|
81
81
|
end
|
82
82
|
end
|
83
83
|
end
|
84
|
-
|
85
84
|
|
86
85
|
# Iterates over each key in the database.
|
87
86
|
def each_key(&block)
|
88
87
|
transaction { |db| db.each_key &block }
|
89
88
|
end
|
90
|
-
|
91
89
|
|
92
90
|
# Iterates over each key-value pair in the database.
|
93
91
|
def each_value(&block)
|
@@ -105,19 +103,16 @@ class HashCabinet
|
|
105
103
|
|
106
104
|
# Returns true if the database contains the given key.
|
107
105
|
def has_key?(key)
|
108
|
-
transaction { |db| db.has_key? key }
|
106
|
+
transaction { |db| db.has_key? key.to_s }
|
109
107
|
end
|
108
|
+
alias include? has_key?
|
109
|
+
alias key? has_key?
|
110
110
|
|
111
111
|
# Returns +true+ if the database contains the given value.
|
112
112
|
def has_value?(value)
|
113
113
|
transaction { |db| db.has_value? value.to_yaml }
|
114
114
|
end
|
115
115
|
|
116
|
-
# Returns +true+ if the database contains the given key.
|
117
|
-
def include?(key)
|
118
|
-
transaction { |db| db.include? key }
|
119
|
-
end
|
120
|
-
|
121
116
|
# Returns the key associated with the given value. If more than one key
|
122
117
|
# corresponds to the given value, then the first key will be returned.
|
123
118
|
# If no keys are found, +nil+ will be returned.
|
@@ -125,11 +120,6 @@ class HashCabinet
|
|
125
120
|
transaction { |db| db.key value.to_yaml }
|
126
121
|
end
|
127
122
|
|
128
|
-
# Returns +true+ if the database contains the given key.
|
129
|
-
def key?(key)
|
130
|
-
transaction { |db| db.key? key }
|
131
|
-
end
|
132
|
-
|
133
123
|
# Returns a new Array containing the keys in the database.
|
134
124
|
def keys
|
135
125
|
transaction { |db| db.keys }
|
@@ -139,13 +129,21 @@ class HashCabinet
|
|
139
129
|
def length
|
140
130
|
transaction { |db| db.length }
|
141
131
|
end
|
132
|
+
alias size length
|
133
|
+
alias count length
|
142
134
|
|
143
135
|
# Empties the database, then inserts the given key-value pairs.
|
136
|
+
#
|
144
137
|
# This method will work with any object which implements an +#each_pair+
|
145
|
-
# method, such as a Hash
|
138
|
+
# method, such as a Hash, or with any object that implements an +#each+
|
139
|
+
# method, such as an Array. In this case, the array will be converted to
|
140
|
+
# a `key=key` hash before storing it.
|
146
141
|
def replace(data)
|
147
|
-
data
|
148
|
-
|
142
|
+
if !data.respond_to? :each_pair and data.respond_to? :each
|
143
|
+
data = array_to_hash data
|
144
|
+
end
|
145
|
+
|
146
|
+
data = normalize_types data
|
149
147
|
transaction { |db| db.replace data }
|
150
148
|
end
|
151
149
|
|
@@ -168,11 +166,6 @@ class HashCabinet
|
|
168
166
|
end
|
169
167
|
end
|
170
168
|
|
171
|
-
# Returns the number of keys in the database.
|
172
|
-
def size
|
173
|
-
transaction { |db| db.size }
|
174
|
-
end
|
175
|
-
|
176
169
|
# Returns a new Array containing each key-value pair in the database.
|
177
170
|
def to_a
|
178
171
|
transaction do |db|
|
@@ -190,9 +183,15 @@ class HashCabinet
|
|
190
183
|
# Insert or update key-value pairs.
|
191
184
|
#
|
192
185
|
# This method will work with any object which implements an +#each_pair+
|
193
|
-
# method, such as a Hash
|
186
|
+
# method, such as a Hash, or with any object that implements an +#each+
|
187
|
+
# method, such as an Array. In this case, the array will be converted to
|
188
|
+
# a `key=key` hash before storing it.
|
194
189
|
def update(data)
|
195
|
-
data
|
190
|
+
if !data.respond_to? :each_pair and data.respond_to? :each
|
191
|
+
data = array_to_hash data
|
192
|
+
end
|
193
|
+
|
194
|
+
data = normalize_types data
|
196
195
|
transaction { |db| db.update data }
|
197
196
|
end
|
198
197
|
|
@@ -211,8 +210,20 @@ class HashCabinet
|
|
211
210
|
# Returns an Array of values corresponding to the given keys.
|
212
211
|
def values_at(*key)
|
213
212
|
transaction do |db|
|
214
|
-
db.values_at(*key).map &:from_yaml
|
213
|
+
db.values_at(*(key.map &:to_s)).map &:from_yaml
|
215
214
|
end
|
216
215
|
end
|
217
216
|
|
217
|
+
private
|
218
|
+
|
219
|
+
def array_to_hash(array)
|
220
|
+
array.map { |item| [item, item] }.to_h
|
221
|
+
end
|
222
|
+
|
223
|
+
def normalize_types(hash)
|
224
|
+
hash.map do |key, value|
|
225
|
+
[key.to_s, value.to_yaml]
|
226
|
+
end.to_h
|
227
|
+
end
|
228
|
+
|
218
229
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danny Ben Shitrit
|
@@ -21,7 +21,10 @@ files:
|
|
21
21
|
homepage: https://github.com/dannyben/hash_cabinet
|
22
22
|
licenses:
|
23
23
|
- MIT
|
24
|
-
metadata:
|
24
|
+
metadata:
|
25
|
+
bug_tracker_uri: https://github.com/DannyBen/hash_cabinet/issues
|
26
|
+
documentation_uri: https://rubydoc.info/gems/hash_cabinet/HashCabinet
|
27
|
+
source_code_uri: https://github.com/dannyben/hash_cabinet
|
25
28
|
post_install_message:
|
26
29
|
rdoc_options: []
|
27
30
|
require_paths:
|
@@ -30,7 +33,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
30
33
|
requirements:
|
31
34
|
- - ">="
|
32
35
|
- !ruby/object:Gem::Version
|
33
|
-
version: 2.
|
36
|
+
version: 2.5.0
|
34
37
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
38
|
requirements:
|
36
39
|
- - ">="
|