Class: HashCabinet
- Inherits:
-
Object
- Object
- HashCabinet
- Defined in:
- lib/hash_cabinet.rb
Defined Under Namespace
Modules: Refinements
Instance Attribute Summary collapse
-
#path ⇒ String
readonly
The path to the database file.
Instance Method Summary collapse
-
#[](key) ⇒ Object
The value in the database associated with the given
key
. -
#[]=(key, value) ⇒ Object
Inserts or updates a value in the database with the given key as an index.
-
#clear ⇒ Object
Deletes all key-value pairs from the database.
-
#delete(key) ⇒ Object
Deletes the given ‘key` from the database.
-
#delete_if {|key, value| ... } ⇒ Object
Iterates over the key-value pairs in the database, deleting those for which the block returns true.
-
#each {|key, value| ... } ⇒ Object
Iterates over each key-value pair in the database.
-
#each_key {|key| ... } ⇒ Object
Iterates over each key in the database.
-
#each_value {|value| ... } ⇒ Object
Iterates over each key-value pair in the database.
-
#empty? ⇒ Boolean
true
if the database is empty. -
#has_key?(key) ⇒ Boolean
(also: #include?, #key?)
true
if the database contains the given key. -
#has_value?(value) ⇒ Boolean
true
if the database contains the given value. -
#initialize(path) ⇒ HashCabinet
constructor
Initializes a new database file at #path.
-
#key(value) ⇒ String
Returns the key associated with the given value.
-
#keys ⇒ Array
A new Array containing the keys in the database.
-
#length ⇒ Integer
(also: #size, #count)
The number of keys in the database.
-
#replace(data) ⇒ Object
Empties the database, then inserts the given key-value pairs.
-
#select(&block) ⇒ Hash
A new Hash of key-value pairs for which the block returns true.
-
#shift ⇒ Array
Removes a key-value pair from the database and returns them as an Array.
-
#to_a ⇒ Array
A new array containing each key-value pair in the database.
-
#to_h ⇒ hash
A new hash containing each key-value pair in the database.
-
#transaction {|db| ... } ⇒ Object
Yields the
SDBM
object to the block. -
#update(data) ⇒ Object
Inserts or updates key-value pairs.
-
#value?(value) ⇒ Boolean
true
if the database contains the given value. -
#values ⇒ Array
A new array containing the values in the database.
-
#values_at(*key) ⇒ Array
An Array of values corresponding to the given keys.
Constructor Details
#initialize(path) ⇒ HashCabinet
Initializes a new database file at #path
30 31 32 |
# File 'lib/hash_cabinet.rb', line 30 def initialize(path) @path = path end |
Instance Attribute Details
#path ⇒ String (readonly)
Returns the path to the database file.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/hash_cabinet.rb', line 6 class HashCabinet # Refinements for internal use module Refinements refine String do def from_yaml YAML.load self end end refine NilClass do def from_yaml nil end end end using Refinements attr_reader :path # Initializes a new database file at {path} # # @param [String] path the path to the database file def initialize(path) @path = path end # Yields the +SDBM+ object to the block. # # @example # cabinet = HashCabinet.new 'filename' # # cabinet.transaction do |db| # db.clear # end # # @note # Under most circumstances, this method should not be used directly, # as it is used by all other methods. # # @yieldparam [SDBM] db the {SDBM} instance def transaction(&block) SDBM.open path, &block end # @return [Object] the value in the database associated with the given +key+. def [](key) transaction { |db| db[key.to_s].from_yaml } end # Inserts or updates a value in the database with the given key as an index. def []=(key, value) transaction { |db| db[key.to_s] = value.to_yaml } end # Deletes all key-value pairs from the database. def clear transaction { |db| db.clear } end # Deletes the given `key` from the database. def delete(key) transaction { |db| db.delete key.to_s } end # Iterates over the key-value pairs in the database, deleting those for # which the block returns true. # # @example Delete all records with +age < 18+. # cabinet = HashCabinet.new 'filename' # # cabinet.delete_if do |key, value| # value[:age] < 18 # end # # @yieldparam [String] key the pair key # @yieldparam [Object] value the pair value def delete_if(&block) transaction do |db| db.delete_if do |key, value| yield key, value.from_yaml end end end # Iterates over each key-value pair in the database. # # @yieldparam [String] key the pair key # @yieldparam [Object] value the pair value def each(&block) transaction do |db| db.each do |key, value| yield key, value.from_yaml end end end # Iterates over each key in the database. # # @yieldparam [String] key the pair key def each_key(&block) transaction { |db| db.each_key(&block) } end # Iterates over each key-value pair in the database. # # @yieldparam [Object] value the pair value def each_value(&block) transaction do |db| db.each_value do |value| yield value.from_yaml end end end # @return [Boolean] +true+ if the database is empty. def empty? transaction { |db| db.empty? } end # @return [Boolean] +true+ if the database contains the given key. def has_key?(key) transaction { |db| db.has_key? key.to_s } end alias include? has_key? alias key? has_key? # @return [Boolean] +true+ if the database contains the given value. def has_value?(value) transaction { |db| db.has_value? value.to_yaml } end # Returns the key associated with the given value. # # If more than one key corresponds to the given value, then the first key will be returned. # If no keys are found, +nil+ will be returned. # # @return [String] the key associated with the given value. def key(value) transaction { |db| db.key value.to_yaml } end # @return [Array] a new Array containing the keys in the database. def keys transaction { |db| db.keys } end # @return [Integer] the number of keys in the database. def length transaction { |db| db.length } end alias size length alias count length # Empties the database, then inserts the given key-value pairs. # # This method will work with any object which implements an +#each_pair+ # method, such as a Hash, or with any object that implements an +#each+ # method, such as an Array. In this case, the array will be converted to # a `key=key` hash before storing it. # # @example # cabinet = HashCabinet.new 'filename' # cabinet.replace key1: 'value1', key2: 'value2' # # @param [Object] data the data to store def replace(data) if !data.respond_to? :each_pair and data.respond_to? :each data = array_to_hash data end data = normalize_types data transaction { |db| db.replace data } end # @return [Hash] a new Hash of key-value pairs for which the block returns true. def select(&block) transaction do |db| db.select do |key, value| yield key, value.from_yaml end.to_h.transform_values &:from_yaml end end # Removes a key-value pair from the database and returns them as an Array. # # If the database is empty, returns nil. # # @return [Array] the key and value. def shift transaction do |db| result = db.shift [result[0], result[1].from_yaml] end end # @return [Array] a new array containing each key-value pair in the # database. def to_a transaction do |db| db.to_a.map { |pair| [pair[0], pair[1].from_yaml] } end end # @return [hash] a new hash containing each key-value pair in the database. def to_h transaction do |db| db.to_h.transform_values &:from_yaml end end # Inserts or updates key-value pairs. # # This method will work with any object which implements an +#each_pair+ # method, such as a Hash, or with any object that implements an +#each+ # method, such as an Array. In this case, the array will be converted to # a `key=key` hash before storing it. # # @example # cabinet = HashCabinet.new 'filename' # cabinet.update key1: 'value1', key2: 'value2' # # @param [Object] data the data to store def update(data) if !data.respond_to? :each_pair and data.respond_to? :each data = array_to_hash data end data = normalize_types data transaction { |db| db.update data } end # @return [Boolean] +true+ if the database contains the given value. def value?(value) transaction { |db| db.value? value.to_yaml } end # @return [Array] a new array containing the values in the database. def values transaction do |db| db.values.map &:from_yaml end end # @return [Array] an Array of values corresponding to the given keys. def values_at(*key) transaction do |db| db.values_at(*(key.map &:to_s)).map &:from_yaml end end private def array_to_hash(array) array.map { |item| [item, item] }.to_h end def normalize_types(hash) hash.map do |key, value| [key.to_s, value.to_yaml] end.to_h end end |
Instance Method Details
#[](key) ⇒ Object
Returns the value in the database associated with the given key
.
53 54 55 |
# File 'lib/hash_cabinet.rb', line 53 def [](key) transaction { |db| db[key.to_s].from_yaml } end |
#[]=(key, value) ⇒ Object
Inserts or updates a value in the database with the given key as an index.
58 59 60 |
# File 'lib/hash_cabinet.rb', line 58 def []=(key, value) transaction { |db| db[key.to_s] = value.to_yaml } end |
#clear ⇒ Object
Deletes all key-value pairs from the database.
63 64 65 |
# File 'lib/hash_cabinet.rb', line 63 def clear transaction { |db| db.clear } end |
#delete(key) ⇒ Object
Deletes the given ‘key` from the database.
68 69 70 |
# File 'lib/hash_cabinet.rb', line 68 def delete(key) transaction { |db| db.delete key.to_s } end |
#delete_if {|key, value| ... } ⇒ Object
Iterates over the key-value pairs in the database, deleting those for which the block returns true.
84 85 86 87 88 89 90 |
# File 'lib/hash_cabinet.rb', line 84 def delete_if(&block) transaction do |db| db.delete_if do |key, value| yield key, value.from_yaml end end end |
#each {|key, value| ... } ⇒ Object
Iterates over each key-value pair in the database.
96 97 98 99 100 101 102 |
# File 'lib/hash_cabinet.rb', line 96 def each(&block) transaction do |db| db.each do |key, value| yield key, value.from_yaml end end end |
#each_key {|key| ... } ⇒ Object
Iterates over each key in the database.
107 108 109 |
# File 'lib/hash_cabinet.rb', line 107 def each_key(&block) transaction { |db| db.each_key(&block) } end |
#each_value {|value| ... } ⇒ Object
Iterates over each key-value pair in the database.
114 115 116 117 118 119 120 |
# File 'lib/hash_cabinet.rb', line 114 def each_value(&block) transaction do |db| db.each_value do |value| yield value.from_yaml end end end |
#empty? ⇒ Boolean
Returns true
if the database is empty.
123 124 125 |
# File 'lib/hash_cabinet.rb', line 123 def empty? transaction { |db| db.empty? } end |
#has_key?(key) ⇒ Boolean Also known as: include?, key?
Returns true
if the database contains the given key.
128 129 130 |
# File 'lib/hash_cabinet.rb', line 128 def has_key?(key) transaction { |db| db.has_key? key.to_s } end |
#has_value?(value) ⇒ Boolean
Returns true
if the database contains the given value.
135 136 137 |
# File 'lib/hash_cabinet.rb', line 135 def has_value?(value) transaction { |db| db.has_value? value.to_yaml } end |
#key(value) ⇒ String
Returns the key associated with the given value.
If more than one key corresponds to the given value, then the first key will be returned. If no keys are found, nil
will be returned.
145 146 147 |
# File 'lib/hash_cabinet.rb', line 145 def key(value) transaction { |db| db.key value.to_yaml } end |
#keys ⇒ Array
Returns a new Array containing the keys in the database.
150 151 152 |
# File 'lib/hash_cabinet.rb', line 150 def keys transaction { |db| db.keys } end |
#length ⇒ Integer Also known as: size, count
Returns the number of keys in the database.
155 156 157 |
# File 'lib/hash_cabinet.rb', line 155 def length transaction { |db| db.length } end |
#replace(data) ⇒ Object
Empties the database, then inserts the given key-value pairs.
This method will work with any object which implements an #each_pair
method, such as a Hash, or with any object that implements an #each
method, such as an Array. In this case, the array will be converted to a ‘key=key` hash before storing it.
173 174 175 176 177 178 179 180 |
# File 'lib/hash_cabinet.rb', line 173 def replace(data) if !data.respond_to? :each_pair and data.respond_to? :each data = array_to_hash data end data = normalize_types data transaction { |db| db.replace data } end |
#select(&block) ⇒ Hash
Returns a new Hash of key-value pairs for which the block returns true.
183 184 185 186 187 188 189 |
# File 'lib/hash_cabinet.rb', line 183 def select(&block) transaction do |db| db.select do |key, value| yield key, value.from_yaml end.to_h.transform_values &:from_yaml end end |
#shift ⇒ Array
Removes a key-value pair from the database and returns them as an Array.
If the database is empty, returns nil.
196 197 198 199 200 201 |
# File 'lib/hash_cabinet.rb', line 196 def shift transaction do |db| result = db.shift [result[0], result[1].from_yaml] end end |
#to_a ⇒ Array
Returns a new array containing each key-value pair in the database.
205 206 207 208 209 |
# File 'lib/hash_cabinet.rb', line 205 def to_a transaction do |db| db.to_a.map { |pair| [pair[0], pair[1].from_yaml] } end end |
#to_h ⇒ hash
Returns a new hash containing each key-value pair in the database.
212 213 214 215 216 |
# File 'lib/hash_cabinet.rb', line 212 def to_h transaction do |db| db.to_h.transform_values &:from_yaml end end |
#transaction {|db| ... } ⇒ Object
Under most circumstances, this method should not be used directly, as it is used by all other methods.
Yields the SDBM
object to the block.
48 49 50 |
# File 'lib/hash_cabinet.rb', line 48 def transaction(&block) SDBM.open path, &block end |
#update(data) ⇒ Object
Inserts or updates key-value pairs.
This method will work with any object which implements an #each_pair
method, such as a Hash, or with any object that implements an #each
method, such as an Array. In this case, the array will be converted to a ‘key=key` hash before storing it.
230 231 232 233 234 235 236 237 |
# File 'lib/hash_cabinet.rb', line 230 def update(data) if !data.respond_to? :each_pair and data.respond_to? :each data = array_to_hash data end data = normalize_types data transaction { |db| db.update data } end |
#value?(value) ⇒ Boolean
Returns true
if the database contains the given value.
240 241 242 |
# File 'lib/hash_cabinet.rb', line 240 def value?(value) transaction { |db| db.value? value.to_yaml } end |
#values ⇒ Array
Returns a new array containing the values in the database.
245 246 247 248 249 |
# File 'lib/hash_cabinet.rb', line 245 def values transaction do |db| db.values.map &:from_yaml end end |
#values_at(*key) ⇒ Array
Returns an Array of values corresponding to the given keys.
252 253 254 255 256 |
# File 'lib/hash_cabinet.rb', line 252 def values_at(*key) transaction do |db| db.values_at(*(key.map &:to_s)).map &:from_yaml end end |