2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2010-04-10 21:21:29 -04:00
|
|
|
require 'yaml'
|
2003-05-09 17:25:50 -04:00
|
|
|
require 'dbm'
|
2011-05-13 20:50:39 -04:00
|
|
|
|
2010-04-10 21:21:29 -04:00
|
|
|
module YAML
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2011-05-13 20:50:39 -04:00
|
|
|
# YAML + DBM = YDBM
|
2011-05-15 07:55:52 -04:00
|
|
|
#
|
2011-05-13 20:50:39 -04:00
|
|
|
# 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.
|
2003-05-09 17:25:50 -04:00
|
|
|
class DBM < ::DBM
|
2013-08-11 23:49:50 -04:00
|
|
|
VERSION = "0.1" # :nodoc:
|
2011-05-13 20:50:39 -04:00
|
|
|
|
2013-08-12 00:29:49 -04:00
|
|
|
# :call-seq:
|
|
|
|
# ydbm[key] -> value
|
|
|
|
#
|
2011-05-13 20:50:39 -04:00
|
|
|
# Return value associated with +key+ from database.
|
|
|
|
#
|
|
|
|
# Returns +nil+ if there is no such +key+.
|
2013-08-11 23:49:50 -04:00
|
|
|
#
|
|
|
|
# See #fetch for more information.
|
2003-05-09 17:25:50 -04:00
|
|
|
def []( key )
|
|
|
|
fetch( key )
|
|
|
|
end
|
2011-05-13 20:50:39 -04:00
|
|
|
|
|
|
|
# :call-seq:
|
2013-08-12 00:29:49 -04:00
|
|
|
# ydbm[key] = value
|
2011-05-13 20:50:39 -04:00
|
|
|
#
|
|
|
|
# Set +key+ to +value+ in database.
|
|
|
|
#
|
|
|
|
# +value+ will be converted to YAML before storage.
|
2013-08-11 23:49:50 -04:00
|
|
|
#
|
|
|
|
# See #store for more information.
|
2003-05-09 17:25:50 -04:00
|
|
|
def []=( key, val )
|
|
|
|
store( key, val )
|
|
|
|
end
|
2011-05-13 20:50:39 -04:00
|
|
|
|
|
|
|
# :call-seq:
|
2013-08-12 00:29:49 -04:00
|
|
|
# ydbm.fetch( key, ifnone = nil )
|
|
|
|
# ydbm.fetch( key ) { |key| ... }
|
2011-05-13 20:50:39 -04:00
|
|
|
#
|
|
|
|
# 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+.
|
2013-08-11 23:49:50 -04:00
|
|
|
#
|
|
|
|
# See ::DBM#fetch for more information.
|
2003-05-09 17:25:50 -04:00
|
|
|
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
|
2011-05-13 20:50:39 -04:00
|
|
|
|
|
|
|
# Deprecated, used YAML::DBM#key instead.
|
2011-09-25 08:05:02 -04:00
|
|
|
# ----
|
|
|
|
# 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.
|
|
|
|
#
|
2003-05-09 17:25:50 -04:00
|
|
|
def index( keystr )
|
|
|
|
super( keystr.to_yaml )
|
|
|
|
end
|
2011-05-13 20:50:39 -04:00
|
|
|
|
2013-08-11 23:49:50 -04:00
|
|
|
# :call-seq:
|
2013-08-12 00:29:49 -04:00
|
|
|
# ydbm.key(value) -> string
|
2013-08-11 23:49:50 -04:00
|
|
|
#
|
|
|
|
# Returns the key for the specified value.
|
2011-09-25 08:05:02 -04:00
|
|
|
def key( keystr )
|
|
|
|
invert[keystr]
|
|
|
|
end
|
|
|
|
|
2013-08-12 00:29:49 -04:00
|
|
|
# :call-seq:
|
|
|
|
# ydbm.values_at(*keys)
|
|
|
|
#
|
2011-05-13 20:50:39 -04:00
|
|
|
# Returns an array containing the values associated with the given keys.
|
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
|
2011-05-13 20:50:39 -04:00
|
|
|
|
2013-08-12 00:29:49 -04:00
|
|
|
# :call-seq:
|
|
|
|
# ydbm.delete(key)
|
|
|
|
#
|
2011-05-14 02:45:39 -04:00
|
|
|
# Deletes value from database associated with +key+.
|
2011-05-13 20:50:39 -04:00
|
|
|
#
|
|
|
|
# Returns value or +nil+.
|
2003-05-09 17:25:50 -04:00
|
|
|
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
|
2011-05-13 20:50:39 -04:00
|
|
|
|
2013-08-12 00:29:49 -04:00
|
|
|
# :call-seq:
|
|
|
|
# ydbm.delete_if { |key, value| ... }
|
|
|
|
#
|
2011-05-13 20:50:39 -04:00
|
|
|
# Calls the given block once for each +key+, +value+ pair in the database.
|
|
|
|
# Deletes all entries for which the block returns true.
|
|
|
|
#
|
|
|
|
# Returns +self+.
|
|
|
|
def delete_if # :yields: [key, value]
|
2003-05-09 17:25:50 -04:00
|
|
|
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
|
2011-05-13 20:50:39 -04:00
|
|
|
|
2013-08-12 00:29:49 -04:00
|
|
|
# :call-seq:
|
|
|
|
# ydbm.reject { |key, value| ... }
|
|
|
|
#
|
2011-05-13 20:50:39 -04:00
|
|
|
# Converts the contents of the database to an in-memory Hash, then calls
|
|
|
|
# Hash#reject with the specified code block, returning a new Hash.
|
2003-05-09 17:25:50 -04:00
|
|
|
def reject
|
|
|
|
hsh = self.to_hash
|
|
|
|
hsh.reject { |k,v| yield k, v }
|
|
|
|
end
|
2011-05-13 20:50:39 -04:00
|
|
|
|
2013-08-12 00:29:49 -04:00
|
|
|
# :call-seq:
|
|
|
|
# ydbm.each_pair { |key, value| ... }
|
|
|
|
#
|
2011-05-13 20:50:39 -04:00
|
|
|
# Calls the given block once for each +key+, +value+ pair in the database.
|
|
|
|
#
|
|
|
|
# Returns +self+.
|
|
|
|
def each_pair # :yields: [key, value]
|
2003-05-09 17:25:50 -04:00
|
|
|
keys.each { |k| yield k, fetch( k ) }
|
|
|
|
self
|
|
|
|
end
|
2011-05-13 20:50:39 -04:00
|
|
|
|
2013-08-12 00:29:49 -04:00
|
|
|
# :call-seq:
|
|
|
|
# ydbm.each_value { |value| ... }
|
|
|
|
#
|
2011-05-13 20:50:39 -04:00
|
|
|
# Calls the given block for each value in database.
|
|
|
|
#
|
|
|
|
# Returns +self+.
|
|
|
|
def each_value # :yields: 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
|
2011-05-13 20:50:39 -04:00
|
|
|
|
2013-08-12 00:29:49 -04:00
|
|
|
# :call-seq:
|
|
|
|
# ydbm.values
|
|
|
|
#
|
2011-05-13 20:50:39 -04:00
|
|
|
# Returns an array of values from the database.
|
2003-05-09 17:25:50 -04:00
|
|
|
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
|
2011-05-13 20:50:39 -04:00
|
|
|
|
2013-08-12 00:29:49 -04:00
|
|
|
# :call-seq:
|
|
|
|
# ydbm.has_value?(value)
|
|
|
|
#
|
|
|
|
# Returns true if specified +value+ is found in the database.
|
2003-05-09 17:25:50 -04:00
|
|
|
def has_value?( val )
|
|
|
|
each_value { |v| return true if v == val }
|
|
|
|
return false
|
|
|
|
end
|
2011-05-13 20:50:39 -04:00
|
|
|
|
2013-08-12 00:29:49 -04:00
|
|
|
# :call-seq:
|
|
|
|
# ydbm.invert -> hash
|
|
|
|
#
|
2011-05-13 20:50:39 -04:00
|
|
|
# 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.
|
2003-05-09 17:25:50 -04:00
|
|
|
def invert
|
|
|
|
h = {}
|
|
|
|
keys.each { |k| h[ self.fetch( k ) ] = k }
|
|
|
|
h
|
|
|
|
end
|
2011-05-13 20:50:39 -04:00
|
|
|
|
2013-08-12 00:29:49 -04:00
|
|
|
# :call-seq:
|
|
|
|
# ydbm.replace(hash) -> ydbm
|
|
|
|
#
|
2011-05-13 20:50:39 -04:00
|
|
|
# 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.
|
2003-05-09 17:25:50 -04:00
|
|
|
def replace( hsh )
|
|
|
|
clear
|
|
|
|
update( hsh )
|
|
|
|
end
|
2011-05-13 20:50:39 -04:00
|
|
|
|
2013-08-12 00:29:49 -04:00
|
|
|
# :call-seq:
|
|
|
|
# ydbm.shift -> [key, value]
|
|
|
|
#
|
2011-05-13 20:50:39 -04:00
|
|
|
# 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.
|
2003-05-09 17:25:50 -04:00
|
|
|
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
|
2011-05-13 20:50:39 -04:00
|
|
|
|
|
|
|
# :call-seq:
|
2013-08-12 00:29:49 -04:00
|
|
|
# ydbm.select { |key, value| ... }
|
|
|
|
# ydbm.select(*keys)
|
2011-05-13 20:50:39 -04:00
|
|
|
#
|
|
|
|
# If a block is provided, returns a new array containing [key, value] pairs
|
|
|
|
# for which the block returns true.
|
|
|
|
#
|
|
|
|
# Otherwise, same as #values_at
|
2003-05-09 17:25:50 -04:00
|
|
|
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
|
2011-05-13 20:50:39 -04:00
|
|
|
|
|
|
|
# :call-seq:
|
2013-08-12 00:29:49 -04:00
|
|
|
# ydbm.store(key, value) -> value
|
2011-05-13 20:50:39 -04:00
|
|
|
#
|
2013-08-12 00:29:49 -04:00
|
|
|
# Stores +value+ in database with +key+ as the index. +value+ is converted
|
|
|
|
# to YAML before being stored.
|
2011-05-13 20:50:39 -04:00
|
|
|
#
|
2013-08-12 00:29:49 -04:00
|
|
|
# Returns +value+
|
2003-05-09 17:25:50 -04:00
|
|
|
def store( key, val )
|
|
|
|
super( key, val.to_yaml )
|
|
|
|
val
|
|
|
|
end
|
2011-05-13 20:50:39 -04:00
|
|
|
|
2013-08-12 00:29:49 -04:00
|
|
|
# :call-seq:
|
|
|
|
# ydbm.update(hash) -> ydbm
|
|
|
|
#
|
2011-05-13 20:50:39 -04:00
|
|
|
# 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+.
|
2003-05-09 17:25:50 -04:00
|
|
|
def update( hsh )
|
2011-09-25 08:05:02 -04:00
|
|
|
hsh.each_pair do |k,v|
|
|
|
|
self.store( k, v )
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
2011-05-13 20:50:39 -04:00
|
|
|
|
2013-08-12 00:29:49 -04:00
|
|
|
# :call-seq:
|
|
|
|
# ydbm.to_a -> array
|
|
|
|
#
|
2011-05-13 20:50:39 -04:00
|
|
|
# Converts the contents of the database to an array of [key, value] arrays,
|
|
|
|
# and returns it.
|
2003-05-09 17:25:50 -04:00
|
|
|
def to_a
|
|
|
|
a = []
|
|
|
|
keys.each { |k| a.push [ k, self.fetch( k ) ] }
|
|
|
|
a
|
|
|
|
end
|
2011-05-13 20:50:39 -04:00
|
|
|
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2013-08-12 00:29:49 -04:00
|
|
|
# :call-seq:
|
|
|
|
# ydbm.to_hash -> hash
|
|
|
|
#
|
2011-05-13 20:50:39 -04:00
|
|
|
# Converts the contents of the database to an in-memory Hash object, and
|
|
|
|
# returns it.
|
2003-05-09 17:25:50 -04:00
|
|
|
def to_hash
|
|
|
|
h = {}
|
|
|
|
keys.each { |k| h[ k ] = self.fetch( k ) }
|
|
|
|
h
|
|
|
|
end
|
2011-05-13 20:50:39 -04:00
|
|
|
|
2003-05-09 17:25:50 -04:00
|
|
|
alias :each :each_pair
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|