Class: YAML::DBM
- Inherits:
-
DBM
- Object
- DBM
- YAML::DBM
- Defined in:
- lib/yaml/dbm.rb
Overview
YAML + DBM = YDBM
YAML::DBM provides the same interface as ::DBM.
However, while DBM only allows strings for both keys and values, this library allows one to use most Ruby objects for values by first converting them to YAML. Keys must be strings.
Conversion to and from YAML is performed automatically.
See the documentation for ::DBM and ::YAML for more information.
Constant Summary collapse
- VERSION =
:nodoc:
"0.1"
Instance Method Summary collapse
-
#[](key) ⇒ Object
:call-seq: ydbm -> value.
-
#[]=(key, val) ⇒ Object
:call-seq: ydbm = value.
-
#delete(key) ⇒ Object
:call-seq: ydbm.delete(key).
-
#delete_if ⇒ Object
:call-seq: ydbm.delete_if { |key, value| … }.
-
#each_pair ⇒ Object
(also: #each)
:call-seq: ydbm.each_pair { |key, value| … }.
-
#each_value ⇒ Object
:call-seq: ydbm.each_value { |value| … }.
-
#fetch(keystr, ifnone = nil) ⇒ Object
:call-seq: ydbm.fetch( key, ifnone = nil ) ydbm.fetch( key ) { |key| … }.
-
#has_value?(val) ⇒ Boolean
:call-seq: ydbm.has_value?(value).
-
#index(keystr) ⇒ Object
Deprecated, used YAML::DBM#key instead.
-
#invert ⇒ Object
:call-seq: ydbm.invert -> hash.
-
#key(keystr) ⇒ Object
:call-seq: ydbm.key(value) -> string.
-
#reject ⇒ Object
:call-seq: ydbm.reject { |key, value| … }.
-
#replace(hsh) ⇒ Object
:call-seq: ydbm.replace(hash) -> ydbm.
-
#select(*keys) ⇒ Object
:call-seq: ydbm.select { |key, value| … } ydbm.select(*keys).
-
#shift ⇒ Object
:call-seq: ydbm.shift -> [key, value].
-
#store(key, val) ⇒ Object
:call-seq: ydbm.store(key, value) -> value.
-
#to_a ⇒ Object
:call-seq: ydbm.to_a -> array.
-
#to_hash ⇒ Object
:call-seq: ydbm.to_hash -> hash.
-
#update(hsh) ⇒ Object
:call-seq: ydbm.update(hash) -> ydbm.
-
#values ⇒ Object
:call-seq: ydbm.values.
-
#values_at(*keys) ⇒ Object
:call-seq: ydbm.values_at(*keys).
Instance Method Details
#[](key) ⇒ Object
:call-seq:
ydbm[key] -> value
Return value associated with key
from database.
Returns nil
if there is no such key
.
See #fetch for more information.
28 29 30 |
# File 'lib/yaml/dbm.rb', line 28 def []( key ) fetch( key ) end |
#[]=(key, val) ⇒ Object
:call-seq:
ydbm[key] = value
Set key
to value
in database.
value
will be converted to YAML before storage.
See #store for more information.
40 41 42 |
# File 'lib/yaml/dbm.rb', line 40 def []=( key, val ) store( key, val ) end |
#delete(key) ⇒ Object
:call-seq:
ydbm.delete(key)
Deletes value from database associated with key
.
Returns value or nil
.
101 102 103 104 105 106 107 |
# File 'lib/yaml/dbm.rb', line 101 def delete( key ) v = super( key ) if String === v v = YAML.load( v ) end v end |
#delete_if ⇒ Object
:call-seq:
ydbm.delete_if { |key, value| ... }
Calls the given block once for each key
, value
pair in the database. Deletes all entries for which the block returns true.
Returns self
.
116 117 118 119 120 121 |
# File 'lib/yaml/dbm.rb', line 116 def delete_if # :yields: [key, value] del_keys = keys.dup del_keys.delete_if { |k| yield( k, fetch( k ) ) == false } del_keys.each { |k| delete( k ) } self end |
#each_pair ⇒ Object Also known as: each
:call-seq:
ydbm.each_pair { |key, value| ... }
Calls the given block once for each key
, value
pair in the database.
Returns self
.
139 140 141 142 |
# File 'lib/yaml/dbm.rb', line 139 def each_pair # :yields: [key, value] keys.each { |k| yield k, fetch( k ) } self end |
#each_value ⇒ Object
:call-seq:
ydbm.each_value { |value| ... }
Calls the given block for each value in database.
Returns self
.
150 151 152 153 |
# File 'lib/yaml/dbm.rb', line 150 def each_value # :yields: value super { |v| yield YAML.load( v ) } self end |
#fetch(keystr, ifnone = nil) ⇒ Object
:call-seq:
ydbm.fetch( key, ifnone = nil )
ydbm.fetch( key ) { |key| ... }
Return value associated with key
.
If there is no value for key
and no block is given, returns ifnone
.
Otherwise, calls block passing in the given key
.
See ::DBM#fetch for more information.
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/yaml/dbm.rb', line 55 def fetch( keystr, ifnone = nil ) begin val = super( keystr ) return YAML.load( val ) if String === val rescue IndexError end if block_given? yield keystr else ifnone end end |
#has_value?(val) ⇒ Boolean
:call-seq:
ydbm.has_value?(value)
Returns true if specified value
is found in the database.
167 168 169 170 |
# File 'lib/yaml/dbm.rb', line 167 def has_value?( val ) each_value { |v| return true if v == val } return false end |
#index(keystr) ⇒ Object
Deprecated, used YAML::DBM#key instead.
Note: YAML::DBM#index makes warning from internal of ::DBM#index. It says ‘DBM#index is deprecated; use DBM#key’, but DBM#key behaves not same as DBM#index.
75 76 77 |
# File 'lib/yaml/dbm.rb', line 75 def index( keystr ) super( keystr.to_yaml ) end |
#invert ⇒ Object
:call-seq:
ydbm.invert -> hash
Returns a Hash (not a DBM database) created by using each value in the database as a key, with the corresponding key as its value.
Note that all values in the hash will be Strings, but the keys will be actual objects.
180 181 182 183 184 |
# File 'lib/yaml/dbm.rb', line 180 def invert h = {} keys.each { |k| h[ self.fetch( k ) ] = k } h end |
#key(keystr) ⇒ Object
:call-seq:
ydbm.key(value) -> string
Returns the key for the specified value.
83 84 85 |
# File 'lib/yaml/dbm.rb', line 83 def key( keystr ) invert[keystr] end |
#reject ⇒ Object
:call-seq:
ydbm.reject { |key, value| ... }
Converts the contents of the database to an in-memory Hash, then calls Hash#reject with the specified code block, returning a new Hash.
128 129 130 131 |
# File 'lib/yaml/dbm.rb', line 128 def reject hsh = self.to_hash hsh.reject { |k,v| yield k, v } end |
#replace(hsh) ⇒ Object
:call-seq:
ydbm.replace(hash) -> ydbm
Replaces the contents of the database with the contents of the specified object. Takes any object which implements the each_pair method, including Hash and DBM objects.
192 193 194 195 |
# File 'lib/yaml/dbm.rb', line 192 def replace( hsh ) clear update( hsh ) end |
#select(*keys) ⇒ Object
:call-seq:
ydbm.select { |key, value| ... }
ydbm.select(*keys)
If a block is provided, returns a new array containing [key, value] pairs for which the block returns true.
Otherwise, same as #values_at
218 219 220 221 222 223 224 |
# File 'lib/yaml/dbm.rb', line 218 def select( *keys ) if block_given? self.keys.collect { |k| v = self[k]; [k, v] if yield k, v }.compact else values_at( *keys ) end end |
#shift ⇒ Object
:call-seq:
ydbm.shift -> [key, value]
Removes a [key, value] pair from the database, and returns it. If the database is empty, returns nil
.
The order in which values are removed/returned is not guaranteed.
204 205 206 207 208 |
# File 'lib/yaml/dbm.rb', line 204 def shift a = super a[1] = YAML.load( a[1] ) if a a end |
#store(key, val) ⇒ Object
:call-seq:
ydbm.store(key, value) -> value
Stores value
in database with key
as the index. value
is converted to YAML before being stored.
Returns value
233 234 235 236 |
# File 'lib/yaml/dbm.rb', line 233 def store( key, val ) super( key, val.to_yaml ) val end |
#to_a ⇒ Object
:call-seq:
ydbm.to_a -> array
Converts the contents of the database to an array of [key, value] arrays, and returns it.
258 259 260 261 262 |
# File 'lib/yaml/dbm.rb', line 258 def to_a a = [] keys.each { |k| a.push [ k, self.fetch( k ) ] } a end |
#to_hash ⇒ Object
:call-seq:
ydbm.to_hash -> hash
Converts the contents of the database to an in-memory Hash object, and returns it.
270 271 272 273 274 |
# File 'lib/yaml/dbm.rb', line 270 def to_hash h = {} keys.each { |k| h[ k ] = self.fetch( k ) } h end |
#update(hsh) ⇒ Object
:call-seq:
ydbm.update(hash) -> ydbm
Updates the database with multiple values from the specified object. Takes any object which implements the each_pair method, including Hash and DBM objects.
Returns self
.
246 247 248 249 250 251 |
# File 'lib/yaml/dbm.rb', line 246 def update( hsh ) hsh.each_pair do |k,v| self.store( k, v ) end self end |
#values ⇒ Object
:call-seq:
ydbm.values
Returns an array of values from the database.
159 160 161 |
# File 'lib/yaml/dbm.rb', line 159 def values super.collect { |v| YAML.load( v ) } end |
#values_at(*keys) ⇒ Object
:call-seq:
ydbm.values_at(*keys)
Returns an array containing the values associated with the given keys.
91 92 93 |
# File 'lib/yaml/dbm.rb', line 91 def values_at( *keys ) keys.collect { |k| fetch( k ) } end |