Class: YAML::DBM

Inherits:
DBM
  • Object
show all
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 =
"0.1"

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object

Return value associated with key from database.

Returns nil if there is no such key.



23
24
25
# File 'lib/yaml/dbm.rb', line 23

def []( key )
    fetch( key )
end

#[]=(key, val) ⇒ Object

:call-seq:

[]=( key, value )

Set key to value in database.

value will be converted to YAML before storage.



33
34
35
# File 'lib/yaml/dbm.rb', line 33

def []=( key, val )
    store( key, val )
end

#delete(key) ⇒ Object

Deletes value from database associated with key.

Returns value or nil.



82
83
84
85
86
87
88
# File 'lib/yaml/dbm.rb', line 82

def delete( key )
    v = super( key )
    if String === v
        v = YAML.load( v )
    end
    v
end

#delete_ifObject

Calls the given block once for each key, value pair in the database. Deletes all entries for which the block returns true.

Returns self.



94
95
96
97
98
99
# File 'lib/yaml/dbm.rb', line 94

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_pairObject Also known as: each

Calls the given block once for each key, value pair in the database.

Returns self.



111
112
113
114
# File 'lib/yaml/dbm.rb', line 111

def each_pair # :yields: [key, value]
    keys.each { |k| yield k, fetch( k ) }
    self
end

#each_valueObject

Calls the given block for each value in database.

Returns self.



119
120
121
122
# File 'lib/yaml/dbm.rb', line 119

def each_value # :yields: value
    super { |v| yield YAML.load( v ) }
    self
end

#fetch(keystr, ifnone = nil) ⇒ Object

:call-seq:

fetch( key, ifnone = nil )
fetch( key, &block )

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.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/yaml/dbm.rb', line 46

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

Returns true if specified value is found in the database.

Returns:

  • (Boolean)


130
131
132
133
# File 'lib/yaml/dbm.rb', line 130

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.



66
67
68
# File 'lib/yaml/dbm.rb', line 66

def index( keystr )
    super( keystr.to_yaml )
end

#invertObject

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.



140
141
142
143
144
# File 'lib/yaml/dbm.rb', line 140

def invert
    h = {}
    keys.each { |k| h[ self.fetch( k ) ] = k }
    h
end

#key(keystr) ⇒ Object



70
71
72
# File 'lib/yaml/dbm.rb', line 70

def key( keystr )
    invert[keystr]
end

#rejectObject

Converts the contents of the database to an in-memory Hash, then calls Hash#reject with the specified code block, returning a new Hash.



103
104
105
106
# File 'lib/yaml/dbm.rb', line 103

def reject
    hsh = self.to_hash
    hsh.reject { |k,v| yield k, v }
end

#replace(hsh) ⇒ Object

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.



149
150
151
152
# File 'lib/yaml/dbm.rb', line 149

def replace( hsh )
    clear
    update( hsh )
end

#select(*keys) ⇒ Object

:call-seq:

select( &block )
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



172
173
174
175
176
177
178
# File 'lib/yaml/dbm.rb', line 172

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

#shiftObject

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.



158
159
160
161
162
# File 'lib/yaml/dbm.rb', line 158

def shift
    a = super
    a[1] = YAML.load( a[1] ) if a
    a
end

#store(key, val) ⇒ Object

:call-seq:

store( key, value )

Stores value in database with key as the index. value is converted to YAML before being stored.

Returns value



187
188
189
190
# File 'lib/yaml/dbm.rb', line 187

def store( key, val )
    super( key, val.to_yaml )
    val
end

#to_aObject

Converts the contents of the database to an array of [key, value] arrays, and returns it.



206
207
208
209
210
# File 'lib/yaml/dbm.rb', line 206

def to_a
    a = []
    keys.each { |k| a.push [ k, self.fetch( k ) ] }
    a
end

#to_hashObject

Converts the contents of the database to an in-memory Hash object, and returns it.



215
216
217
218
219
# File 'lib/yaml/dbm.rb', line 215

def to_hash
    h = {}
    keys.each { |k| h[ k ] = self.fetch( k ) }
    h
end

#update(hsh) ⇒ Object

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.



197
198
199
200
201
202
# File 'lib/yaml/dbm.rb', line 197

def update( hsh )
    hsh.each_pair do |k,v|
        self.store( k, v )
    end
    self
end

#valuesObject

Returns an array of values from the database.



125
126
127
# File 'lib/yaml/dbm.rb', line 125

def values
    super.collect { |v| YAML.load( v ) }
end

#values_at(*keys) ⇒ Object

Returns an array containing the values associated with the given keys.



75
76
77
# File 'lib/yaml/dbm.rb', line 75

def values_at( *keys )
    keys.collect { |k| fetch( k ) }
end