diff options
Diffstat (limited to 'lib/yaml')
| -rw-r--r-- | lib/yaml/dbm.rb | 32 | ||||
| -rw-r--r-- | lib/yaml/store.rb | 41 | ||||
| -rw-r--r-- | lib/yaml/yaml.gemspec | 30 |
3 files changed, 83 insertions, 20 deletions
diff --git a/lib/yaml/dbm.rb b/lib/yaml/dbm.rb index 24a68bfa71..a3cbaeccf6 100644 --- a/lib/yaml/dbm.rb +++ b/lib/yaml/dbm.rb @@ -1,5 +1,10 @@ +# frozen_string_literal: false require 'yaml' -require 'dbm' + +begin + require 'dbm' +rescue LoadError +end module YAML @@ -15,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 @@ -55,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? @@ -101,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 @@ -148,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 @@ -157,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: @@ -203,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 @@ -276,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 b0b580ba1a..27c823b9f9 100644 --- a/lib/yaml/store.rb +++ b/lib/yaml/store.rb @@ -1,8 +1,13 @@ +# frozen_string_literal: false # # 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. @@ -39,27 +44,41 @@ class YAML::Store < PStore # :call-seq: # initialize( file_name, yaml_opts = {} ) + # initialize( file_name, thread_safe = false, yaml_opts = {} ) # # Creates a new YAML::Store object, which will store data in +file_name+. # If the file does not already exist, it will be created. # + # YAML::Store objects are always reentrant. But if _thread_safe_ is set to true, + # then it will become thread-safe at the cost of a minor performance hit. # # Options passed in through +yaml_opts+ will be used when converting the # store to YAML via Hash#to_yaml(). - def initialize file_name, yaml_opts = {} - @opt = yaml_opts - super + def initialize( *o ) + @opt = {} + if o.last.is_a? Hash + @opt.update(o.pop) + end + super(*o) end # :stopdoc: def dump(table) - YAML.dump @table + table.to_yaml(@opt) end def load(content) - table = YAML.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 @@ -70,12 +89,10 @@ class YAML::Store < PStore false end - EMPTY_MARSHAL_DATA = YAML.dump({}) - EMPTY_MARSHAL_CHECKSUM = Digest::MD5.digest(EMPTY_MARSHAL_DATA) def empty_marshal_data - EMPTY_MARSHAL_DATA + {}.to_yaml(@opt) end def empty_marshal_checksum - 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 new file mode 100644 index 0000000000..17e1ce89e0 --- /dev/null +++ b/lib/yaml/yaml.gemspec @@ -0,0 +1,30 @@ +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 = name + spec.version = version + spec.authors = ["Aaron Patterson", "SHIBATA Hiroshi"] + spec.email = ["aaron@tenderlovemaking.com", "hsbt@ruby-lang.org"] + + spec.summary = "YAML Ain't Markup Language" + spec.description = spec.summary + spec.homepage = "https://github.com/ruby/yaml" + spec.licenses = ["Ruby", "BSD-2-Clause"] + + spec.metadata["homepage_uri"] = spec.homepage + spec.metadata["source_code_uri"] = spec.homepage + + # 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>#{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) } + spec.require_paths = ["lib"] +end |
