lib/yaml/dbm.rb in yaml-0.3.0 vs lib/yaml/dbm.rb in yaml-0.4.0
- old
+ new
@@ -1,9 +1,13 @@
# frozen_string_literal: false
require 'yaml'
-require 'dbm'
+begin
+ require 'dbm'
+rescue LoadError
+end
+
module YAML
# YAML + DBM = YDBM
#
# YAML::DBM provides the same interface as ::DBM.
@@ -14,11 +18,10 @@
#
# Conversion to and from YAML is performed automatically.
#
# See the documentation for ::DBM and ::YAML for more information.
class DBM < ::DBM
- VERSION = "0.1" # :nodoc:
# :call-seq:
# ydbm[key] -> value
#
# Return value associated with +key+ from database.
@@ -54,11 +57,17 @@
#
# See ::DBM#fetch for more information.
def fetch( keystr, ifnone = nil )
begin
val = super( keystr )
- return YAML.load( val ) if String === val
+ if String === val
+ if YAML.respond_to?(:safe_load)
+ return YAML.safe_load( val )
+ else
+ return YAML.load( val )
+ end
+ end
rescue IndexError
end
if block_given?
yield keystr
else
@@ -100,11 +109,15 @@
#
# Returns value or +nil+.
def delete( key )
v = super( key )
if String === v
- v = YAML.load( v )
+ if YAML.respond_to?(:safe_load)
+ v = YAML.safe_load( v )
+ else
+ v = YAML.load( v )
+ end
end
v
end
# :call-seq:
@@ -147,20 +160,20 @@
#
# Calls the given block for each value in database.
#
# Returns +self+.
def each_value # :yields: value
- super { |v| yield YAML.load( v ) }
+ super { |v| yield YAML.respond_to?(:safe_load) ? YAML.safe_load( v ) : YAML.load( v ) }
self
end
# :call-seq:
# ydbm.values
#
# Returns an array of values from the database.
def values
- super.collect { |v| YAML.load( v ) }
+ super.collect { |v| YAML.respond_to?(:safe_load) ? YAML.safe_load( v ) : YAML.load( v ) }
end
# :call-seq:
# ydbm.has_value?(value)
#
@@ -202,11 +215,13 @@
# If the database is empty, returns +nil+.
#
# The order in which values are removed/returned is not guaranteed.
def shift
a = super
- a[1] = YAML.load( a[1] ) if a
+ if a
+ a[1] = YAML.respond_to?(:safe_load) ? YAML.safe_load( a[1] ) : YAML.load( a[1] )
+ end
a
end
# :call-seq:
# ydbm.select { |key, value| ... }
@@ -275,6 +290,6 @@
end
alias :each :each_pair
end
-end
+end if defined?(DBM)