5
# - Same interface as DBM class
8
# YAML::DBM provides the same interface as ::DBM.
10
# However, while DBM only allows strings for both keys and values,
11
# this library allows one to use most Ruby objects for values
12
# by first converting them to YAML. Keys must be strings.
14
# Conversion to and from YAML is performed automatically.
16
# See the documentation for ::DBM and ::YAML for more information.
20
# Return value associated with +key+ from database.
22
# Returns +nil+ if there is no such +key+.
30
# Set +key+ to +value+ in database.
32
# +value+ will be converted to YAML before storage.
14
33
def []=( key, val )
38
# fetch( key, ifnone = nil )
39
# fetch( key, &block )
41
# Return value associated with +key+.
43
# If there is no value for +key+ and no block is given, returns +ifnone+.
45
# Otherwise, calls block passing in the given +key+.
17
46
def fetch( keystr, ifnone = nil )
19
48
val = super( keystr )
59
# Deprecated, used YAML::DBM#key instead.
29
60
def index( keystr )
30
61
super( keystr.to_yaml )
64
# Returns an array containing the values associated with the given keys.
32
65
def values_at( *keys )
33
66
keys.collect { |k| fetch( k ) }
69
# Deletes value from database associated with +key+.
71
# Returns value or +nil+.
80
# Calls the given block once for each +key+, +value+ pair in the database.
81
# Deletes all entries for which the block returns true.
84
def delete_if # :yields: [key, value]
43
85
del_keys = keys.dup
44
86
del_keys.delete_if { |k| yield( k, fetch( k ) ) == false }
45
87
del_keys.each { |k| delete( k ) }
91
# Converts the contents of the database to an in-memory Hash, then calls
92
# Hash#reject with the specified code block, returning a new Hash.
50
95
hsh.reject { |k,v| yield k, v }
98
# Calls the given block once for each +key+, +value+ pair in the database.
101
def each_pair # :yields: [key, value]
53
102
keys.each { |k| yield k, fetch( k ) }
106
# Calls the given block for each value in database.
109
def each_value # :yields: value
57
110
super { |v| yield YAML.load( v ) }
114
# Returns an array of values from the database.
61
116
super.collect { |v| YAML.load( v ) }
119
# Returns true if specified value is found in the database.
63
120
def has_value?( val )
64
121
each_value { |v| return true if v == val }
125
# Returns a Hash (not a DBM database) created by using each value in the
126
# database as a key, with the corresponding key as its value.
128
# Note that all values in the hash will be Strings, but the keys will be
69
132
keys.each { |k| h[ self.fetch( k ) ] = k }
136
# Replaces the contents of the database with the contents of the specified
137
# object. Takes any object which implements the each_pair method, including
138
# Hash and DBM objects.
72
139
def replace( hsh )
144
# Removes a [key, value] pair from the database, and returns it.
145
# If the database is empty, returns +nil+.
147
# The order in which values are removed/returned is not guaranteed.
78
150
a[1] = YAML.load( a[1] ) if a
158
# If a block is provided, returns a new array containing [key, value] pairs
159
# for which the block returns true.
161
# Otherwise, same as #values_at
81
162
def select( *keys )
83
164
self.keys.collect { |k| v = self[k]; [k, v] if yield k, v }.compact
85
166
values_at( *keys )
171
# store( key, value )
173
#Stores +value+ in database with +key+ as the index. +value+ is converted
174
#to YAML before being stored.
88
177
def store( key, val )
89
178
super( key, val.to_yaml )
182
# Updates the database with multiple values from the specified object.
183
# Takes any object which implements the each_pair method, including
184
# Hash and DBM objects.
93
188
hsh.keys.each do |k|
94
189
self.store( k, hsh.fetch( k ) )
194
# Converts the contents of the database to an array of [key, value] arrays,
100
198
keys.each { |k| a.push [ k, self.fetch( k ) ] }
203
# Converts the contents of the database to an in-memory Hash object, and
105
207
keys.each { |k| h[ k ] = self.fetch( k ) }
108
211
alias :each :each_pair