2010-04-10 21:21:29 -04:00
|
|
|
require 'yaml'
|
2003-05-09 17:25:50 -04:00
|
|
|
require 'dbm'
|
|
|
|
#
|
|
|
|
# YAML + DBM = YDBM
|
|
|
|
# - Same interface as DBM class
|
|
|
|
#
|
2010-04-10 21:21:29 -04:00
|
|
|
module YAML
|
2003-05-09 17:25:50 -04:00
|
|
|
|
|
|
|
class DBM < ::DBM
|
|
|
|
VERSION = "0.1"
|
|
|
|
def []( key )
|
|
|
|
fetch( key )
|
|
|
|
end
|
|
|
|
def []=( key, val )
|
|
|
|
store( key, val )
|
|
|
|
end
|
|
|
|
def fetch( keystr, ifnone = nil )
|
|
|
|
begin
|
|
|
|
val = super( keystr )
|
2010-04-10 21:21:29 -04:00
|
|
|
return YAML.load( val ) if String === val
|
2003-05-09 17:25:50 -04:00
|
|
|
rescue IndexError
|
|
|
|
end
|
|
|
|
if block_given?
|
|
|
|
yield keystr
|
|
|
|
else
|
|
|
|
ifnone
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def index( keystr )
|
|
|
|
super( keystr.to_yaml )
|
|
|
|
end
|
2003-07-24 14:56:09 -04:00
|
|
|
def values_at( *keys )
|
2003-05-09 17:25:50 -04:00
|
|
|
keys.collect { |k| fetch( k ) }
|
|
|
|
end
|
|
|
|
def delete( key )
|
|
|
|
v = super( key )
|
|
|
|
if String === v
|
2010-04-10 21:21:29 -04:00
|
|
|
v = YAML.load( v )
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
v
|
|
|
|
end
|
|
|
|
def delete_if
|
|
|
|
del_keys = keys.dup
|
|
|
|
del_keys.delete_if { |k| yield( k, fetch( k ) ) == false }
|
2009-03-05 22:56:38 -05:00
|
|
|
del_keys.each { |k| delete( k ) }
|
2003-05-09 17:25:50 -04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
def reject
|
|
|
|
hsh = self.to_hash
|
|
|
|
hsh.reject { |k,v| yield k, v }
|
|
|
|
end
|
|
|
|
def each_pair
|
|
|
|
keys.each { |k| yield k, fetch( k ) }
|
|
|
|
self
|
|
|
|
end
|
|
|
|
def each_value
|
2010-04-10 21:21:29 -04:00
|
|
|
super { |v| yield YAML.load( v ) }
|
2003-05-09 17:25:50 -04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
def values
|
2010-04-10 21:21:29 -04:00
|
|
|
super.collect { |v| YAML.load( v ) }
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
def has_value?( val )
|
|
|
|
each_value { |v| return true if v == val }
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
def invert
|
|
|
|
h = {}
|
|
|
|
keys.each { |k| h[ self.fetch( k ) ] = k }
|
|
|
|
h
|
|
|
|
end
|
|
|
|
def replace( hsh )
|
|
|
|
clear
|
|
|
|
update( hsh )
|
|
|
|
end
|
|
|
|
def shift
|
|
|
|
a = super
|
2010-04-10 21:21:29 -04:00
|
|
|
a[1] = YAML.load( a[1] ) if a
|
2003-05-09 17:25:50 -04:00
|
|
|
a
|
|
|
|
end
|
|
|
|
def select( *keys )
|
|
|
|
if block_given?
|
|
|
|
self.keys.collect { |k| v = self[k]; [k, v] if yield k, v }.compact
|
|
|
|
else
|
2003-07-24 14:56:09 -04:00
|
|
|
values_at( *keys )
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
def store( key, val )
|
|
|
|
super( key, val.to_yaml )
|
|
|
|
val
|
|
|
|
end
|
|
|
|
def update( hsh )
|
|
|
|
hsh.keys.each do |k|
|
|
|
|
self.store( k, hsh.fetch( k ) )
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
def to_a
|
|
|
|
a = []
|
|
|
|
keys.each { |k| a.push [ k, self.fetch( k ) ] }
|
|
|
|
a
|
|
|
|
end
|
|
|
|
def to_hash
|
|
|
|
h = {}
|
|
|
|
keys.each { |k| h[ k ] = self.fetch( k ) }
|
|
|
|
h
|
|
|
|
end
|
|
|
|
alias :each :each_pair
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|