diff options
Diffstat (limited to 'lib/yaml')
| -rw-r--r-- | lib/yaml/dbm.rb | 31 | ||||
| -rw-r--r-- | lib/yaml/store.rb | 20 | ||||
| -rw-r--r-- | lib/yaml/yaml.gemspec | 13 |
3 files changed, 49 insertions, 15 deletions
diff --git a/lib/yaml/dbm.rb b/lib/yaml/dbm.rb index e2508cd74b..a3cbaeccf6 100644 --- a/lib/yaml/dbm.rb +++ b/lib/yaml/dbm.rb @@ -1,6 +1,10 @@ # frozen_string_literal: false require 'yaml' -require 'dbm' + +begin + require 'dbm' +rescue LoadError +end module YAML @@ -16,7 +20,6 @@ module YAML # # See the documentation for ::DBM and ::YAML for more information. class DBM < ::DBM - VERSION = "0.1" # :nodoc: # :call-seq: # ydbm[key] -> value @@ -56,7 +59,13 @@ class DBM < ::DBM 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? @@ -102,7 +111,11 @@ class DBM < ::DBM 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 @@ -149,7 +162,7 @@ class DBM < ::DBM # # 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 @@ -158,7 +171,7 @@ class DBM < ::DBM # # 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: @@ -204,7 +217,9 @@ class DBM < ::DBM # 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 @@ -277,4 +292,4 @@ class DBM < ::DBM alias :each :each_pair end -end +end if defined?(DBM) diff --git a/lib/yaml/store.rb b/lib/yaml/store.rb index f8650b942f..27c823b9f9 100644 --- a/lib/yaml/store.rb +++ b/lib/yaml/store.rb @@ -3,7 +3,11 @@ # YAML::Store # require 'yaml' -require 'pstore' + +begin + require 'pstore' +rescue LoadError +end # YAML::Store provides the same functionality as PStore, except it uses YAML # to dump objects instead of Marshal. @@ -65,8 +69,16 @@ class YAML::Store < PStore end def load(content) - table = YAML.unsafe_load(content) - if table == false + table = if YAML.respond_to?(:safe_load) + if Psych::VERSION >= "3.1" + YAML.safe_load(content, permitted_classes: [Symbol]) + else + YAML.safe_load(content, [Symbol]) + end + else + YAML.load(content) + end + if table == false || table == nil {} else table @@ -83,4 +95,4 @@ class YAML::Store < PStore def empty_marshal_checksum CHECKSUM_ALGO.digest(empty_marshal_data) end -end +end if defined?(::PStore) diff --git a/lib/yaml/yaml.gemspec b/lib/yaml/yaml.gemspec index 80554d8a7a..17e1ce89e0 100644 --- a/lib/yaml/yaml.gemspec +++ b/lib/yaml/yaml.gemspec @@ -1,6 +1,13 @@ +name = File.basename(__FILE__, ".gemspec") +version = ["lib", Array.new(name.count("-")+1, "..").join("/")].find do |dir| + break File.foreach(File.join(__dir__, dir, "#{name.tr('-', '/')}.rb")) do |line| + /^\s*LOADER_VERSION\s*=\s*"(.*)"/ =~ line and break $1 + end rescue nil +end + Gem::Specification.new do |spec| - spec.name = "yaml" - spec.version = "0.2.1" + spec.name = name + spec.version = version spec.authors = ["Aaron Patterson", "SHIBATA Hiroshi"] spec.email = ["aaron@tenderlovemaking.com", "hsbt@ruby-lang.org"] @@ -15,7 +22,7 @@ Gem::Specification.new do |spec| # Specify which files should be added to the gem when it is released. # The `git ls-files -z` loads the files in the RubyGem that have been added into git. spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do - `git ls-files -z 2>/dev/null`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } + `git ls-files -z 2>#{IO::NULL}`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } end spec.bindir = "exe" spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } |
