summaryrefslogtreecommitdiff
path: root/lib/yaml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/yaml')
-rw-r--r--lib/yaml/baseemitter.rb247
-rw-r--r--lib/yaml/basenode.rb216
-rw-r--r--lib/yaml/constants.rb45
-rw-r--r--lib/yaml/dbm.rb220
-rw-r--r--lib/yaml/encoding.rb33
-rw-r--r--lib/yaml/error.rb34
-rw-r--r--lib/yaml/loader.rb14
-rw-r--r--lib/yaml/rubytypes.rb408
-rw-r--r--lib/yaml/store.rb89
-rw-r--r--lib/yaml/stream.rb40
-rw-r--r--lib/yaml/stringio.rb83
-rw-r--r--lib/yaml/syck.rb19
-rw-r--r--lib/yaml/tag.rb91
-rw-r--r--lib/yaml/types.rb194
-rw-r--r--lib/yaml/yaml.gemspec30
-rw-r--r--lib/yaml/yamlnode.rb54
-rw-r--r--lib/yaml/ypath.rb52
17 files changed, 311 insertions, 1558 deletions
diff --git a/lib/yaml/baseemitter.rb b/lib/yaml/baseemitter.rb
deleted file mode 100644
index 1aef152749..0000000000
--- a/lib/yaml/baseemitter.rb
+++ /dev/null
@@ -1,247 +0,0 @@
-#
-# BaseEmitter
-#
-
-require 'yaml/constants'
-require 'yaml/encoding'
-require 'yaml/error'
-
-module YAML
-
- module BaseEmitter
-
- def options( opt = nil )
- if opt
- @options[opt] || YAML::DEFAULTS[opt]
- else
- @options
- end
- end
-
- def options=( opt )
- @options = opt
- end
-
- #
- # Emit binary data
- #
- def binary_base64( value )
- self << "!binary "
- self.node_text( [value].pack("m"), '|' )
- end
-
- #
- # Emit plain, normal flowing text
- #
- def node_text( value, block = nil )
- @seq_map = false
- valx = value.dup
- unless block
- block =
- if options(:UseBlock)
- '|'
- elsif not options(:UseFold) and valx =~ /\n[ \t]/ and not valx =~ /#{YAML::ESCAPE_CHAR}/
- '|'
- else
- '>'
- end
-
- indt = $&.to_i if block =~ /\d+/
- if valx =~ /(\A\n*[ \t#]|^---\s+)/
- indt = options(:Indent) unless indt.to_i > 0
- block += indt.to_s
- end
-
- block +=
- if valx =~ /\n\Z\n/
- "+"
- elsif valx =~ /\Z\n/
- ""
- else
- "-"
- end
- end
- block += "\n"
- if block[0] == ?"
- esc_skip = ( "\t\n" unless valx =~ /^[ \t]/ ) || ""
- valx = fold( YAML::escape( valx, esc_skip ) + "\"" ).chomp
- self << '"' + indent_text( valx, indt, false )
- else
- if block[0] == ?>
- valx = fold( valx )
- end
- #p [block, indt]
- self << block + indent_text( valx, indt )
- end
- end
-
- #
- # Emit a simple, unqouted string
- #
- def simple( value )
- @seq_map = false
- self << value.to_s
- end
-
- #
- # Emit double-quoted string
- #
- def double( value )
- "\"#{YAML.escape( value )}\""
- end
-
- #
- # Emit single-quoted string
- #
- def single( value )
- "'#{value}'"
- end
-
- #
- # Write a text block with the current indent
- #
- def indent_text( text, mod, first_line = true )
- return "" if text.to_s.empty?
- spacing = indent( mod )
- text = text.gsub( /\A([^\n])/, "#{ spacing }\\1" ) if first_line
- return text.gsub( /\n^([^\n])/, "\n#{spacing}\\1" )
- end
-
- #
- # Write a current indent
- #
- def indent( mod = nil )
- #p [ self.id, level, mod, :INDENT ]
- if level <= 0
- mod ||= 0
- else
- mod ||= options(:Indent)
- mod += ( level - 1 ) * options(:Indent)
- end
- return " " * mod
- end
-
- #
- # Add indent to the buffer
- #
- def indent!
- self << indent
- end
-
- #
- # Folding paragraphs within a column
- #
- def fold( value )
- value.gsub( /(^[ \t]+.*$)|(\S.{0,#{options(:BestWidth) - 1}})(?:[ \t]+|(\n+(?=[ \t]|\Z))|$)/ ) do |s|
- $1 || $2 + ( $3 || "\n" )
- end
- end
-
- #
- # Quick mapping
- #
- def map( type, &e )
- val = Mapping.new
- e.call( val )
- self << "#{type} " if type.length.nonzero?
-
- #
- # Empty hashes
- #
- if val.length.zero?
- self << "{}"
- @seq_map = false
- else
- # FIXME
- # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero?
- # @headless = 1
- # end
-
- defkey = @options.delete( :DefaultKey )
- if defkey
- seq_map_shortcut
- self << "= : "
- defkey.to_yaml( :Emitter => self )
- end
-
- #
- # Emit the key and value
- #
- val.each { |v|
- seq_map_shortcut
- if v[0].is_complex_yaml?
- self << "? "
- end
- v[0].to_yaml( :Emitter => self )
- if v[0].is_complex_yaml?
- self << "\n"
- indent!
- end
- self << ": "
- v[1].to_yaml( :Emitter => self )
- }
- end
- end
-
- def seq_map_shortcut
- # FIXME: seq_map needs to work with the new anchoring system
- # if @seq_map
- # @anchor_extras[@buffer.length - 1] = "\n" + indent
- # @seq_map = false
- # else
- self << "\n"
- indent!
- # end
- end
-
- #
- # Quick sequence
- #
- def seq( type, &e )
- @seq_map = false
- val = Sequence.new
- e.call( val )
- self << "#{type} " if type.length.nonzero?
-
- #
- # Empty arrays
- #
- if val.length.zero?
- self << "[]"
- else
- # FIXME
- # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero?
- # @headless = 1
- # end
-
- #
- # Emit the key and value
- #
- val.each { |v|
- self << "\n"
- indent!
- self << "- "
- @seq_map = true if v.class == Hash
- v.to_yaml( :Emitter => self )
- }
- end
- end
-
- end
-
- #
- # Emitter helper classes
- #
- class Mapping < Array
- def add( k, v )
- push [k, v]
- end
- end
-
- class Sequence < Array
- def add( v )
- push v
- end
- end
-
-end
diff --git a/lib/yaml/basenode.rb b/lib/yaml/basenode.rb
deleted file mode 100644
index d24f6172e9..0000000000
--- a/lib/yaml/basenode.rb
+++ /dev/null
@@ -1,216 +0,0 @@
-#
-# YAML::BaseNode class
-#
-require 'yaml/ypath'
-
-module YAML
-
- #
- # YAML Generic Model container
- #
- module BaseNode
-
- #
- # Search for YPath entry and return
- # qualified nodes.
- #
- def select( ypath_str )
- matches = match_path( ypath_str )
-
- #
- # Create a new generic view of the elements selected
- #
- if matches
- result = []
- matches.each { |m|
- result.push m.last
- }
- YAML.transfer( 'seq', result )
- end
- end
-
- #
- # Search for YPath entry and return
- # transformed nodes.
- #
- def select!( ypath_str )
- matches = match_path( ypath_str )
-
- #
- # Create a new generic view of the elements selected
- #
- if matches
- result = []
- matches.each { |m|
- result.push m.last.transform
- }
- result
- end
- end
-
- #
- # Search for YPath entry and return a list of
- # qualified paths.
- #
- def search( ypath_str )
- matches = match_path( ypath_str )
-
- if matches
- matches.collect { |m|
- path = []
- m.each_index { |i|
- path.push m[i] if ( i % 2 ).zero?
- }
- "/" + path.compact.join( "/" )
- }
- end
- end
-
- def at( seg )
- if Hash === @value
- self[seg]
- elsif Array === @value and seg =~ /\A\d+\Z/ and @value[seg.to_i]
- @value[seg.to_i]
- end
- end
-
- #
- # YPath search returning a complete depth array
- #
- def match_path( ypath_str )
- depth = 0
- matches = []
- YPath.each_path( ypath_str ) do |ypath|
- seg = match_segment( ypath, 0 )
- matches += seg if seg
- end
- matches.uniq
- end
-
- #
- # Search a node for a single YPath segment
- #
- def match_segment( ypath, depth )
- deep_nodes = []
- seg = ypath.segments[ depth ]
- if seg == "/"
- unless String === @value
- idx = -1
- @value.collect { |v|
- idx += 1
- if Hash === @value
- match_init = [v[0].transform, v[1]]
- match_deep = v[1].match_segment( ypath, depth )
- else
- match_init = [idx, v]
- match_deep = v.match_segment( ypath, depth )
- end
- if match_deep
- match_deep.each { |m|
- deep_nodes.push( match_init + m )
- }
- end
- }
- end
- depth += 1
- seg = ypath.segments[ depth ]
- end
- match_nodes =
- case seg
- when "."
- [[nil, self]]
- when ".."
- [["..", nil]]
- when "*"
- if @value.is_a? Enumerable
- idx = -1
- @value.collect { |h|
- idx += 1
- if Hash === @value
- [h[0].transform, h[1]]
- else
- [idx, h]
- end
- }
- end
- else
- if seg =~ /^"(.*)"$/
- seg = $1
- elsif seg =~ /^'(.*)'$/
- seg = $1
- end
- if ( v = at( seg ) )
- [[ seg, v ]]
- end
- end
- return deep_nodes unless match_nodes
- pred = ypath.predicates[ depth ]
- if pred
- case pred
- when /^\.=/
- pred = $' # '
- match_nodes.reject! { |n|
- n.last.value != pred
- }
- else
- match_nodes.reject! { |n|
- n.last.at( pred ).nil?
- }
- end
- end
- return match_nodes + deep_nodes unless ypath.segments.length > depth + 1
-
- #puts "DEPTH: #{depth + 1}"
- deep_nodes = []
- match_nodes.each { |n|
- if n[1].is_a? BaseNode
- match_deep = n[1].match_segment( ypath, depth + 1 )
- if match_deep
- match_deep.each { |m|
- deep_nodes.push( n + m )
- }
- end
- else
- deep_nodes = []
- end
- }
- deep_nodes = nil if deep_nodes.length == 0
- deep_nodes
- end
-
- #
- # We want the node to act like as Hash
- # if it is.
- #
- def []( *key )
- if Hash === @value
- v = @value.detect { |k,v| k.transform == key.first }
- v[1] if v
- elsif Array === @value
- @value.[]( *key )
- end
- end
-
- def children
- if Hash === @value
- @value.values.collect { |c| c[1] }
- elsif Array === @value
- @value
- end
- end
-
- def children_with_index
- if Hash === @value
- @value.keys.collect { |i| [self[i], i] }
- elsif Array === @value
- i = -1; @value.collect { |v| i += 1; [v, i] }
- end
- end
-
- def emit
- transform.to_yaml
- end
- end
-
-end
-
diff --git a/lib/yaml/constants.rb b/lib/yaml/constants.rb
deleted file mode 100644
index fb833d3077..0000000000
--- a/lib/yaml/constants.rb
+++ /dev/null
@@ -1,45 +0,0 @@
-#
-# Constants used throughout the library
-#
-module YAML
-
- #
- # Constants
- #
- VERSION = '0.60'
- SUPPORTED_YAML_VERSIONS = ['1.0']
-
- #
- # Parser tokens
- #
- WORD_CHAR = 'A-Za-z0-9'
- PRINTABLE_CHAR = '-_A-Za-z0-9!?/()$\'". '
- NOT_PLAIN_CHAR = '\x7f\x0-\x1f\x80-\x9f'
- ESCAPE_CHAR = '[\\x00-\\x09\\x0b-\\x1f]'
- INDICATOR_CHAR = '*&!|\\\\^@%{}[]='
- SPACE_INDICATORS = '-#:,?'
- RESTRICTED_INDICATORS = '#:,}]'
- DNS_COMP_RE = "\\w(?:[-\\w]*\\w)?"
- DNS_NAME_RE = "(?:(?:#{DNS_COMP_RE}\\.)+#{DNS_COMP_RE}|#{DNS_COMP_RE})"
- ESCAPES = %w{\x00 \x01 \x02 \x03 \x04 \x05 \x06 \a
- \x08 \t \n \v \f \r \x0e \x0f
- \x10 \x11 \x12 \x13 \x14 \x15 \x16 \x17
- \x18 \x19 \x1a \e \x1c \x1d \x1e \x1f
- }
- UNESCAPES = {
- 'a' => "\x07", 'b' => "\x08", 't' => "\x09",
- 'n' => "\x0a", 'v' => "\x0b", 'f' => "\x0c",
- 'r' => "\x0d", 'e' => "\x1b", '\\' => '\\',
- }
-
- #
- # Default settings
- #
- DEFAULTS = {
- :Indent => 2, :UseHeader => false, :UseVersion => false, :Version => '1.0',
- :SortKeys => false, :AnchorFormat => 'id%03d', :ExplicitTypes => false,
- :WidthType => 'absolute', :BestWidth => 80,
- :UseBlock => false, :UseFold => false, :Encoding => :None
- }
-
-end
diff --git a/lib/yaml/dbm.rb b/lib/yaml/dbm.rb
index 87d6009250..a3cbaeccf6 100644
--- a/lib/yaml/dbm.rb
+++ b/lib/yaml/dbm.rb
@@ -1,23 +1,71 @@
+# frozen_string_literal: false
require 'yaml'
-require 'dbm'
-#
-# YAML + DBM = YDBM
-# - Same interface as DBM class
-#
+
+begin
+ require 'dbm'
+rescue LoadError
+end
+
module YAML
+# YAML + DBM = YDBM
+#
+# 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.
class DBM < ::DBM
- VERSION = "0.1"
+
+ # :call-seq:
+ # ydbm[key] -> value
+ #
+ # Return value associated with +key+ from database.
+ #
+ # Returns +nil+ if there is no such +key+.
+ #
+ # See #fetch for more information.
def []( key )
fetch( key )
end
+
+ # :call-seq:
+ # ydbm[key] = value
+ #
+ # Set +key+ to +value+ in database.
+ #
+ # +value+ will be converted to YAML before storage.
+ #
+ # See #store for more information.
def []=( key, val )
store( key, val )
end
+
+ # :call-seq:
+ # ydbm.fetch( key, ifnone = nil )
+ # ydbm.fetch( key ) { |key| ... }
+ #
+ # 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+.
+ #
+ # 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?
@@ -26,58 +74,163 @@ class DBM < ::DBM
ifnone
end
end
+
+ # Deprecated, used YAML::DBM#key instead.
+ # ----
+ # 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.
+ #
def index( keystr )
super( keystr.to_yaml )
end
+
+ # :call-seq:
+ # ydbm.key(value) -> string
+ #
+ # Returns the key for the specified value.
+ def key( keystr )
+ invert[keystr]
+ end
+
+ # :call-seq:
+ # ydbm.values_at(*keys)
+ #
+ # Returns an array containing the values associated with the given keys.
def values_at( *keys )
keys.collect { |k| fetch( k ) }
end
+
+ # :call-seq:
+ # ydbm.delete(key)
+ #
+ # Deletes value from database associated with +key+.
+ #
+ # 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
- def delete_if
+
+ # :call-seq:
+ # ydbm.delete_if { |key, value| ... }
+ #
+ # 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]
del_keys = keys.dup
del_keys.delete_if { |k| yield( k, fetch( k ) ) == false }
- del_keys.each { |k| delete( k ) }
+ del_keys.each { |k| delete( k ) }
self
end
+
+ # :call-seq:
+ # ydbm.reject { |key, value| ... }
+ #
+ # Converts the contents of the database to an in-memory Hash, then calls
+ # Hash#reject with the specified code block, returning a new Hash.
def reject
hsh = self.to_hash
hsh.reject { |k,v| yield k, v }
end
- def each_pair
+
+ # :call-seq:
+ # ydbm.each_pair { |key, value| ... }
+ #
+ # Calls the given block once for each +key+, +value+ pair in the database.
+ #
+ # Returns +self+.
+ def each_pair # :yields: [key, value]
keys.each { |k| yield k, fetch( k ) }
self
end
- def each_value
- super { |v| yield YAML::load( v ) }
+
+ # :call-seq:
+ # ydbm.each_value { |value| ... }
+ #
+ # Calls the given block for each value in database.
+ #
+ # Returns +self+.
+ def each_value # :yields: value
+ 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)
+ #
+ # Returns true if specified +value+ is found in the database.
def has_value?( val )
each_value { |v| return true if v == val }
return false
end
+
+ # :call-seq:
+ # ydbm.invert -> hash
+ #
+ # 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.
def invert
h = {}
keys.each { |k| h[ self.fetch( k ) ] = k }
h
end
+
+ # :call-seq:
+ # ydbm.replace(hash) -> ydbm
+ #
+ # 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.
def replace( hsh )
clear
update( hsh )
end
+
+ # :call-seq:
+ # ydbm.shift -> [key, value]
+ #
+ # 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.
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| ... }
+ # ydbm.select(*keys)
+ #
+ # If a block is provided, returns a new array containing [key, value] pairs
+ # for which the block returns true.
+ #
+ # Otherwise, same as #values_at
def select( *keys )
if block_given?
self.keys.collect { |k| v = self[k]; [k, v] if yield k, v }.compact
@@ -85,27 +238,58 @@ class DBM < ::DBM
values_at( *keys )
end
end
+
+ # :call-seq:
+ # ydbm.store(key, value) -> value
+ #
+ # Stores +value+ in database with +key+ as the index. +value+ is converted
+ # to YAML before being stored.
+ #
+ # Returns +value+
def store( key, val )
super( key, val.to_yaml )
val
end
+
+ # :call-seq:
+ # ydbm.update(hash) -> ydbm
+ #
+ # 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+.
def update( hsh )
- hsh.keys.each do |k|
- self.store( k, hsh.fetch( k ) )
+ hsh.each_pair do |k,v|
+ self.store( k, v )
end
self
end
+
+ # :call-seq:
+ # ydbm.to_a -> array
+ #
+ # Converts the contents of the database to an array of [key, value] arrays,
+ # and returns it.
def to_a
a = []
keys.each { |k| a.push [ k, self.fetch( k ) ] }
a
end
+
+
+ # :call-seq:
+ # ydbm.to_hash -> hash
+ #
+ # Converts the contents of the database to an in-memory Hash object, and
+ # returns it.
def to_hash
h = {}
keys.each { |k| h[ k ] = self.fetch( k ) }
h
end
+
alias :each :each_pair
end
-end
+end if defined?(DBM)
diff --git a/lib/yaml/encoding.rb b/lib/yaml/encoding.rb
deleted file mode 100644
index 37f5cfda64..0000000000
--- a/lib/yaml/encoding.rb
+++ /dev/null
@@ -1,33 +0,0 @@
-#
-# Handle Unicode-to-Internal conversion
-#
-
-module YAML
-
- #
- # Escape the string, condensing common escapes
- #
- def YAML.escape( value, skip = "" )
- value.gsub( /\\/, "\\\\\\" ).
- gsub( /"/, "\\\"" ).
- gsub( /([\x00-\x1f])/ ) do |x|
- skip[x] || ESCAPES[ x.unpack("C")[0] ]
- end
- end
-
- #
- # Unescape the condenses escapes
- #
- def YAML.unescape( value )
- value.gsub( /\\(?:([nevfbart\\])|0?x([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/ ) { |x|
- if $3
- ["#$3".hex ].pack('U*')
- elsif $2
- [$2].pack( "H2" )
- else
- UNESCAPES[$1]
- end
- }
- end
-
-end
diff --git a/lib/yaml/error.rb b/lib/yaml/error.rb
deleted file mode 100644
index 15865a9aa9..0000000000
--- a/lib/yaml/error.rb
+++ /dev/null
@@ -1,34 +0,0 @@
-#
-# Error messages and exception class
-#
-
-module YAML
-
- #
- # Error messages
- #
-
- ERROR_NO_HEADER_NODE = "With UseHeader=false, the node Array or Hash must have elements"
- ERROR_NEED_HEADER = "With UseHeader=false, the node must be an Array or Hash"
- ERROR_BAD_EXPLICIT = "Unsupported explicit transfer: '%s'"
- ERROR_MANY_EXPLICIT = "More than one explicit transfer"
- ERROR_MANY_IMPLICIT = "More than one implicit request"
- ERROR_NO_ANCHOR = "No anchor for alias '%s'"
- ERROR_BAD_ANCHOR = "Invalid anchor: %s"
- ERROR_MANY_ANCHOR = "More than one anchor"
- ERROR_ANCHOR_ALIAS = "Can't define both an anchor and an alias"
- ERROR_BAD_ALIAS = "Invalid alias: %s"
- ERROR_MANY_ALIAS = "More than one alias"
- ERROR_ZERO_INDENT = "Can't use zero as an indentation width"
- ERROR_UNSUPPORTED_VERSION = "This release of YAML.rb does not support YAML version %s"
- ERROR_UNSUPPORTED_ENCODING = "Attempt to use unsupported encoding: %s"
-
- #
- # YAML Error classes
- #
-
- class Error < StandardError; end
- class ParseError < Error; end
- class TypeError < StandardError; end
-
-end
diff --git a/lib/yaml/loader.rb b/lib/yaml/loader.rb
deleted file mode 100644
index eb0709e103..0000000000
--- a/lib/yaml/loader.rb
+++ /dev/null
@@ -1,14 +0,0 @@
-#
-# YAML::Loader class
-# .. type handling ..
-#
-module YAML
- class Loader
- TRANSFER_DOMAINS = {
- 'yaml.org,2002' => {},
- 'ruby.yaml.org,2002' => {}
- }
- PRIVATE_TYPES = {}
- IMPLICIT_TYPES = [ 'null', 'bool', 'time', 'int', 'float' ]
- end
-end
diff --git a/lib/yaml/rubytypes.rb b/lib/yaml/rubytypes.rb
deleted file mode 100644
index eebf027135..0000000000
--- a/lib/yaml/rubytypes.rb
+++ /dev/null
@@ -1,408 +0,0 @@
-# -*- mode: ruby; ruby-indent-level: 4; tab-width: 4 -*- vim: sw=4 ts=4
-require 'date'
-
-class Class
- def to_yaml( opts = {} )
- raise TypeError, "can't dump anonymous class %s" % self.class
- end
-end
-
-class Object
- yaml_as "tag:ruby.yaml.org,2002:object"
- def to_yaml_style; end
- def to_yaml_properties; instance_variables.sort; end
- def to_yaml( opts = {} )
- YAML::quick_emit( object_id, opts ) do |out|
- out.map( taguri, to_yaml_style ) do |map|
- to_yaml_properties.each do |m|
- map.add( m[1..-1], instance_variable_get( m ) )
- end
- end
- end
- end
-end
-
-class Hash
- yaml_as "tag:ruby.yaml.org,2002:hash"
- yaml_as "tag:yaml.org,2002:map"
- def yaml_initialize( tag, val )
- if Array === val
- update Hash.[]( *val ) # Convert the map to a sequence
- elsif Hash === val
- update val
- else
- raise YAML::TypeError, "Invalid map explicitly tagged #{ tag }: " + val.inspect
- end
- end
- def to_yaml( opts = {} )
- YAML::quick_emit( object_id, opts ) do |out|
- out.map( taguri, to_yaml_style ) do |map|
- each do |k, v|
- map.add( k, v )
- end
- end
- end
- end
-end
-
-class Struct
- yaml_as "tag:ruby.yaml.org,2002:struct"
- def self.yaml_tag_class_name; self.name.gsub( "Struct::", "" ); end
- def self.yaml_tag_read_class( name ); "Struct::#{ name }"; end
- def self.yaml_new( klass, tag, val )
- if Hash === val
- struct_type = nil
-
- #
- # Use existing Struct if it exists
- #
- props = {}
- val.delete_if { |k,v| props[k] = v if k =~ /^@/ }
- begin
- struct_name, struct_type = YAML.read_type_class( tag, Struct )
- rescue NameError
- end
- if not struct_type
- struct_def = [ tag.split( ':', 4 ).last ]
- struct_type = Struct.new( *struct_def.concat( val.keys.collect { |k| k.intern } ) )
- end
-
- #
- # Set the Struct properties
- #
- st = YAML::object_maker( struct_type, {} )
- st.members.each do |m|
- st.send( "#{m}=", val[m] )
- end
- props.each do |k,v|
- st.instance_variable_set(k, v)
- end
- st
- else
- raise YAML::TypeError, "Invalid Ruby Struct: " + val.inspect
- end
- end
- def to_yaml( opts = {} )
- YAML::quick_emit( object_id, opts ) do |out|
- #
- # Basic struct is passed as a YAML map
- #
- out.map( taguri, to_yaml_style ) do |map|
- self.members.each do |m|
- map.add( m, self[m] )
- end
- self.to_yaml_properties.each do |m|
- map.add( m, instance_variable_get( m ) )
- end
- end
- end
- end
-end
-
-class Array
- yaml_as "tag:ruby.yaml.org,2002:array"
- yaml_as "tag:yaml.org,2002:seq"
- def yaml_initialize( tag, val ); concat( val.to_a ); end
- def to_yaml( opts = {} )
- YAML::quick_emit( object_id, opts ) do |out|
- out.seq( taguri, to_yaml_style ) do |seq|
- each do |x|
- seq.add( x )
- end
- end
- end
- end
-end
-
-class Exception
- yaml_as "tag:ruby.yaml.org,2002:exception"
- def Exception.yaml_new( klass, tag, val )
- o = YAML.object_maker( klass, { 'mesg' => val.delete( 'message' ) } )
- val.each_pair do |k,v|
- o.instance_variable_set("@#{k}", v)
- end
- o
- end
- def to_yaml( opts = {} )
- YAML::quick_emit( object_id, opts ) do |out|
- out.map( taguri, to_yaml_style ) do |map|
- map.add( 'message', message )
- to_yaml_properties.each do |m|
- map.add( m[1..-1], instance_variable_get( m ) )
- end
- end
- end
- end
-end
-
-class String
- yaml_as "tag:ruby.yaml.org,2002:string"
- yaml_as "tag:yaml.org,2002:binary"
- yaml_as "tag:yaml.org,2002:str"
- def is_complex_yaml?
- to_yaml_style or not to_yaml_properties.empty? or self =~ /\n.+/
- end
- def is_binary_data?
- ( self.count( "^ -~", "^\r\n" ) / self.size > 0.3 || self.count( "\x00" ) > 0 ) unless empty?
- end
- def String.yaml_new( klass, tag, val )
- val = val.unpack("m")[0] if tag == "tag:yaml.org,2002:binary"
- val = { 'str' => val } if String === val
- if Hash === val
- s = klass.allocate
- # Thank you, NaHi
- String.instance_method(:initialize).
- bind(s).
- call( val.delete( 'str' ) )
- val.each { |k,v| s.instance_variable_set( k, v ) }
- s
- else
- raise YAML::TypeError, "Invalid String: " + val.inspect
- end
- end
- def to_yaml( opts = {} )
- YAML::quick_emit( is_complex_yaml? ? object_id : nil, opts ) do |out|
- if is_binary_data?
- out.scalar( "tag:yaml.org,2002:binary", [self].pack("m"), :literal )
- elsif to_yaml_properties.empty?
- out.scalar( taguri, self, self =~ /^:/ ? :quote2 : to_yaml_style )
- else
- out.map( taguri, to_yaml_style ) do |map|
- map.add( 'str', "#{self}" )
- to_yaml_properties.each do |m|
- map.add( m, instance_variable_get( m ) )
- end
- end
- end
- end
- end
-end
-
-class Symbol
- yaml_as "tag:ruby.yaml.org,2002:symbol"
- yaml_as "tag:ruby.yaml.org,2002:sym"
- def Symbol.yaml_new( klass, tag, val )
- if String === val
- val = YAML::load( val ) if val =~ /\A(["']).*\1\z/
- val.intern
- else
- raise YAML::TypeError, "Invalid Symbol: " + val.inspect
- end
- end
- def to_yaml( opts = {} )
- YAML::quick_emit( nil, opts ) do |out|
- out.scalar( "tag:yaml.org,2002:str", self.inspect, :plain )
- end
- end
-end
-
-class Range
- yaml_as "tag:ruby.yaml.org,2002:range"
- def Range.yaml_new( klass, tag, val )
- inr = %r'(\w+|[+-]?\d+(?:\.\d+)?(?:e[+-]\d+)?|"(?:[^\\"]|\\.)*")'
- opts = {}
- if String === val and val =~ /^#{inr}(\.{2,3})#{inr}$/o
- r1, rdots, r2 = $1, $2, $3
- opts = {
- 'begin' => YAML.load( "--- #{r1}" ),
- 'end' => YAML.load( "--- #{r2}" ),
- 'excl' => rdots.length == 3
- }
- val = {}
- elsif Hash === val
- opts['begin'] = val.delete('begin')
- opts['end'] = val.delete('end')
- opts['excl'] = val.delete('excl')
- end
- if Hash === opts
- r = YAML::object_maker( klass, {} )
- # Thank you, NaHi
- Range.instance_method(:initialize).
- bind(r).
- call( opts['begin'], opts['end'], opts['excl'] )
- val.each { |k,v| r.instance_variable_set( k, v ) }
- r
- else
- raise YAML::TypeError, "Invalid Range: " + val.inspect
- end
- end
- def to_yaml( opts = {} )
- YAML::quick_emit( object_id, opts ) do |out|
- # if self.begin.is_complex_yaml? or self.begin.respond_to? :to_str or
- # self.end.is_complex_yaml? or self.end.respond_to? :to_str or
- # not to_yaml_properties.empty?
- out.map( taguri, to_yaml_style ) do |map|
- map.add( 'begin', self.begin )
- map.add( 'end', self.end )
- map.add( 'excl', self.exclude_end? )
- to_yaml_properties.each do |m|
- map.add( m, instance_variable_get( m ) )
- end
- end
- # else
- # out.scalar( taguri ) do |sc|
- # sc.embed( self.begin )
- # sc.concat( self.exclude_end? ? "..." : ".." )
- # sc.embed( self.end )
- # end
- # end
- end
- end
-end
-
-class Regexp
- yaml_as "tag:ruby.yaml.org,2002:regexp"
- def Regexp.yaml_new( klass, tag, val )
- if String === val and val =~ /^\/(.*)\/([mix]*)$/
- val = { 'regexp' => $1, 'mods' => $2 }
- end
- if Hash === val
- mods = nil
- unless val['mods'].to_s.empty?
- mods = 0x00
- mods |= Regexp::EXTENDED if val['mods'].include?( 'x' )
- mods |= Regexp::IGNORECASE if val['mods'].include?( 'i' )
- mods |= Regexp::MULTILINE if val['mods'].include?( 'm' )
- end
- val.delete( 'mods' )
- r = YAML::object_maker( klass, {} )
- Regexp.instance_method(:initialize).
- bind(r).
- call( val.delete( 'regexp' ), mods )
- val.each { |k,v| r.instance_variable_set( k, v ) }
- r
- else
- raise YAML::TypeError, "Invalid Regular expression: " + val.inspect
- end
- end
- def to_yaml( opts = {} )
- YAML::quick_emit( nil, opts ) do |out|
- if to_yaml_properties.empty?
- out.scalar( taguri, self.inspect, :plain )
- else
- out.map( taguri, to_yaml_style ) do |map|
- src = self.inspect
- if src =~ /\A\/(.*)\/([a-z]*)\Z/
- map.add( 'regexp', $1 )
- map.add( 'mods', $2 )
- else
- raise YAML::TypeError, "Invalid Regular expression: " + src
- end
- to_yaml_properties.each do |m|
- map.add( m, instance_variable_get( m ) )
- end
- end
- end
- end
- end
-end
-
-class Time
- yaml_as "tag:ruby.yaml.org,2002:time"
- yaml_as "tag:yaml.org,2002:timestamp"
- def Time.yaml_new( klass, tag, val )
- if Hash === val
- t = val.delete( 'at' )
- val.each { |k,v| t.instance_variable_set( k, v ) }
- t
- else
- raise YAML::TypeError, "Invalid Time: " + val.inspect
- end
- end
- def to_yaml( opts = {} )
- YAML::quick_emit( object_id, opts ) do |out|
- tz = "Z"
- # from the tidy Tobias Peters <t-peters@gmx.de> Thanks!
- unless self.utc?
- utc_same_instant = self.dup.utc
- utc_same_writing = Time.utc(year,month,day,hour,min,sec,usec)
- difference_to_utc = utc_same_writing - utc_same_instant
- if (difference_to_utc < 0)
- difference_sign = '-'
- absolute_difference = -difference_to_utc
- else
- difference_sign = '+'
- absolute_difference = difference_to_utc
- end
- difference_minutes = (absolute_difference/60).round
- tz = "%s%02d:%02d" % [ difference_sign, difference_minutes / 60, difference_minutes % 60]
- end
- standard = self.strftime( "%Y-%m-%d %H:%M:%S" )
- standard += ".%06d" % [usec] if usec.nonzero?
- standard += " %s" % [tz]
- if to_yaml_properties.empty?
- out.scalar( taguri, standard, :plain )
- else
- out.map( taguri, to_yaml_style ) do |map|
- map.add( 'at', standard )
- to_yaml_properties.each do |m|
- map.add( m, instance_variable_get( m ) )
- end
- end
- end
- end
- end
-end
-
-class Date
- yaml_as "tag:yaml.org,2002:timestamp#ymd"
- def to_yaml( opts = {} )
- YAML::quick_emit( object_id, opts ) do |out|
- out.scalar( "tag:yaml.org,2002:timestamp", self.to_s, :plain )
- end
- end
-end
-
-class Integer
- yaml_as "tag:yaml.org,2002:int"
- def to_yaml( opts = {} )
- YAML::quick_emit( nil, opts ) do |out|
- out.scalar( "tag:yaml.org,2002:int", self.to_s, :plain )
- end
- end
-end
-
-class Float
- yaml_as "tag:yaml.org,2002:float"
- def to_yaml( opts = {} )
- YAML::quick_emit( nil, opts ) do |out|
- str = self.to_s
- if str == "Infinity"
- str = ".Inf"
- elsif str == "-Infinity"
- str = "-.Inf"
- elsif str == "NaN"
- str = ".NaN"
- end
- out.scalar( "tag:yaml.org,2002:float", str, :plain )
- end
- end
-end
-
-class TrueClass
- yaml_as "tag:yaml.org,2002:bool#yes"
- def to_yaml( opts = {} )
- YAML::quick_emit( nil, opts ) do |out|
- out.scalar( taguri, "true", :plain )
- end
- end
-end
-
-class FalseClass
- yaml_as "tag:yaml.org,2002:bool#no"
- def to_yaml( opts = {} )
- YAML::quick_emit( nil, opts ) do |out|
- out.scalar( taguri, "false", :plain )
- end
- end
-end
-
-class NilClass
- yaml_as "tag:yaml.org,2002:null"
- def to_yaml( opts = {} )
- YAML::quick_emit( nil, opts ) do |out|
- out.scalar( taguri, "", :plain )
- end
- end
-end
-
diff --git a/lib/yaml/store.rb b/lib/yaml/store.rb
index 2ffa9554b9..27c823b9f9 100644
--- a/lib/yaml/store.rb
+++ b/lib/yaml/store.rb
@@ -1,29 +1,98 @@
+# 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.
+#
+# == Example
+#
+# require 'yaml/store'
+#
+# Person = Struct.new :first_name, :last_name
+#
+# people = [Person.new("Bob", "Smith"), Person.new("Mary", "Johnson")]
+#
+# store = YAML::Store.new "test.store"
+#
+# store.transaction do
+# store["people"] = people
+# store["greeting"] = { "hello" => "world" }
+# end
+#
+# After running the above code, the contents of "test.store" will be:
+#
+# ---
+# people:
+# - !ruby/struct:Person
+# first_name: Bob
+# last_name: Smith
+# - !ruby/struct:Person
+# first_name: Mary
+# last_name: Johnson
+# greeting:
+# hello: world
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( *o )
- @opt = YAML::DEFAULTS.dup
- if String === o.first
- super(o.shift)
- end
+ @opt = {}
if o.last.is_a? Hash
@opt.update(o.pop)
end
+ super(*o)
end
+ # :stopdoc:
+
def dump(table)
- @table.to_yaml(@opt)
+ table.to_yaml(@opt)
end
def load(content)
- YAML::load(content)
+ 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
+ end
end
- def load_file(file)
- YAML::load(file)
+ def marshal_dump_supports_canonical_option?
+ false
end
-end
+
+ def empty_marshal_data
+ {}.to_yaml(@opt)
+ end
+ def empty_marshal_checksum
+ CHECKSUM_ALGO.digest(empty_marshal_data)
+ end
+end if defined?(::PStore)
diff --git a/lib/yaml/stream.rb b/lib/yaml/stream.rb
deleted file mode 100644
index 060fbc4200..0000000000
--- a/lib/yaml/stream.rb
+++ /dev/null
@@ -1,40 +0,0 @@
-module YAML
-
- #
- # YAML::Stream -- for emitting many documents
- #
- class Stream
-
- attr_accessor :documents, :options
-
- def initialize( opts = {} )
- @options = opts
- @documents = []
- end
-
- def []( i )
- @documents[ i ]
- end
-
- def add( doc )
- @documents << doc
- end
-
- def edit( doc_num, doc )
- @documents[ doc_num ] = doc
- end
-
- def emit( io = nil )
- # opts = @options.dup
- # opts[:UseHeader] = true if @documents.length > 1
- out = YAML.emitter
- out.reset( io || io2 = StringIO.new )
- @documents.each { |v|
- v.to_yaml( out )
- }
- io || ( io2.rewind; io2.read )
- end
-
- end
-
-end
diff --git a/lib/yaml/stringio.rb b/lib/yaml/stringio.rb
deleted file mode 100644
index 8ad949fa2b..0000000000
--- a/lib/yaml/stringio.rb
+++ /dev/null
@@ -1,83 +0,0 @@
-#
-# Limited StringIO if no core lib is available
-#
-begin
-require 'stringio'
-rescue LoadError
- # StringIO based on code by MoonWolf
- class StringIO
- def initialize(string="")
- @string=string
- @pos=0
- @eof=(string.size==0)
- end
- def pos
- @pos
- end
- def eof
- @eof
- end
- alias eof? eof
- def readline(rs=$/)
- if @eof
- raise EOFError
- else
- if p = @string[@pos..-1]=~rs
- line = @string[@pos,p+1]
- else
- line = @string[@pos..-1]
- end
- @pos+=line.size
- @eof =true if @pos==@string.size
- $_ = line
- end
- end
- def rewind
- seek(0,0)
- end
- def seek(offset,whence)
- case whence
- when 0
- @pos=offset
- when 1
- @pos+=offset
- when 2
- @pos=@string.size+offset
- end
- @eof=(@pos>=@string.size)
- 0
- end
- end
-
- #
- # Class method for creating streams
- #
- def YAML.make_stream( io )
- if String === io
- io = StringIO.new( io )
- elsif not IO === io
- raise YAML::Error, "YAML stream must be an IO or String object."
- end
- if YAML::unicode
- def io.readline
- YAML.utf_to_internal( readline( @ln_sep ), @utf_encoding )
- end
- def io.check_unicode
- @utf_encoding = YAML.sniff_encoding( read( 4 ) )
- @ln_sep = YAML.enc_separator( @utf_encoding )
- seek( -4, IO::SEEK_CUR )
- end
- def io.utf_encoding
- @utf_encoding
- end
- io.check_unicode
- else
- def io.utf_encoding
- :None
- end
- end
- io
- end
-
-end
-
diff --git a/lib/yaml/syck.rb b/lib/yaml/syck.rb
deleted file mode 100644
index faf57e8036..0000000000
--- a/lib/yaml/syck.rb
+++ /dev/null
@@ -1,19 +0,0 @@
-#
-# YAML::Syck module
-# .. glues syck and yaml.rb together ..
-#
-require 'syck'
-require 'yaml/basenode'
-
-module YAML
- module Syck
-
- #
- # Mixin BaseNode functionality
- #
- class Node
- include YAML::BaseNode
- end
-
- end
-end
diff --git a/lib/yaml/tag.rb b/lib/yaml/tag.rb
deleted file mode 100644
index 0fb6bef9a0..0000000000
--- a/lib/yaml/tag.rb
+++ /dev/null
@@ -1,91 +0,0 @@
-# -*- mode: ruby; ruby-indent-level: 4; tab-width: 4 -*- vim: sw=4 ts=4
-# $Id$
-#
-# = yaml/tag.rb: methods for associating a taguri to a class.
-#
-# Author:: why the lucky stiff
-#
-module YAML
- # A dictionary of taguris which map to
- # Ruby classes.
- @@tagged_classes = {}
-
- #
- # Associates a taguri _tag_ with a Ruby class _cls_. The taguri is used to give types
- # to classes when loading YAML. Taguris are of the form:
- #
- # tag:authorityName,date:specific
- #
- # The +authorityName+ is a domain name or email address. The +date+ is the date the type
- # was issued in YYYY or YYYY-MM or YYYY-MM-DD format. The +specific+ is a name for
- # the type being added.
- #
- # For example, built-in YAML types have 'yaml.org' as the +authorityName+ and '2002' as the
- # +date+. The +specific+ is simply the name of the type:
- #
- # tag:yaml.org,2002:int
- # tag:yaml.org,2002:float
- # tag:yaml.org,2002:timestamp
- #
- # The domain must be owned by you on the +date+ declared. If you don't own any domains on the
- # date you declare the type, you can simply use an e-mail address.
- #
- # tag:why@ruby-lang.org,2004:notes/personal
- #
- def YAML.tag_class( tag, cls )
- if @@tagged_classes.has_key? tag
- warn "class #{ @@tagged_classes[tag] } held ownership of the #{ tag } tag"
- end
- @@tagged_classes[tag] = cls
- end
-
- # Returns the complete dictionary of taguris, paired with classes. The key for
- # the dictionary is the full taguri. The value for each key is the class constant
- # associated to that taguri.
- #
- # YAML.tagged_classes["tag:yaml.org,2002:int"] => Integer
- #
- def YAML.tagged_classes
- @@tagged_classes
- end
-end
-
-class Module
- # :stopdoc:
-
- # Adds a taguri _tag_ to a class, used when dumping or loading the class
- # in YAML. See YAML::tag_class for detailed information on typing and
- # taguris.
- def yaml_as( tag, sc = true )
- verbose, $VERBOSE = $VERBOSE, nil
- class_eval <<-"end;", __FILE__, __LINE__+1
- attr_writer :taguri
- def taguri
- if respond_to? :to_yaml_type
- YAML::tagurize( to_yaml_type[1..-1] )
- else
- return @taguri if defined?(@taguri) and @taguri
- tag = #{ tag.dump }
- if self.class.yaml_tag_subclasses? and self.class != YAML::tagged_classes[tag]
- tag = "\#{ tag }:\#{ self.class.yaml_tag_class_name }"
- end
- tag
- end
- end
- def self.yaml_tag_subclasses?; #{ sc ? 'true' : 'false' }; end
- end;
- YAML::tag_class tag, self
- ensure
- $VERBOSE = verbose
- end
- # Transforms the subclass name into a name suitable for display
- # in a subclassed tag.
- def yaml_tag_class_name
- self.name
- end
- # Transforms the subclass name found in the tag into a Ruby
- # constant name.
- def yaml_tag_read_class( name )
- name
- end
-end
diff --git a/lib/yaml/types.rb b/lib/yaml/types.rb
deleted file mode 100644
index 05113f216d..0000000000
--- a/lib/yaml/types.rb
+++ /dev/null
@@ -1,194 +0,0 @@
-# -*- mode: ruby; ruby-indent-level: 4 -*- vim: sw=4
-#
-# Classes required by the full core typeset
-#
-
-module YAML
-
- #
- # Default private type
- #
- class PrivateType
- def self.tag_subclasses?; false; end
- attr_accessor :type_id, :value
- verbose, $VERBOSE = $VERBOSE, nil
- def initialize( type, val )
- @type_id = type; @value = val
- @value.taguri = "x-private:#{ @type_id }"
- end
- def to_yaml( opts = {} )
- @value.to_yaml( opts )
- end
- ensure
- $VERBOSE = verbose
- end
-
- #
- # Default domain type
- #
- class DomainType
- def self.tag_subclasses?; false; end
- attr_accessor :domain, :type_id, :value
- verbose, $VERBOSE = $VERBOSE, nil
- def initialize( domain, type, val )
- @domain = domain; @type_id = type; @value = val
- @value.taguri = "tag:#{ @domain }:#{ @type_id }"
- end
- def to_yaml( opts = {} )
- @value.to_yaml( opts )
- end
- ensure
- $VERBOSE = verbose
- end
-
- #
- # Unresolved objects
- #
- class Object
- def self.tag_subclasses?; false; end
- def to_yaml( opts = {} )
- YAML::quick_emit( object_id, opts ) do |out|
- out.map( "tag:ruby.yaml.org,2002:object:#{ @class }", to_yaml_style ) do |map|
- @ivars.each do |k,v|
- map.add( k, v )
- end
- end
- end
- end
- end
-
- #
- # YAML Hash class to support comments and defaults
- #
- class SpecialHash < ::Hash
- attr_accessor :default
- def inspect
- self.default.to_s
- end
- def to_s
- self.default.to_s
- end
- def update( h )
- if YAML::SpecialHash === h
- @default = h.default if h.default
- end
- super( h )
- end
- def to_yaml( opts = {} )
- opts[:DefaultKey] = self.default
- super( opts )
- end
- end
-
- #
- # Builtin collection: !omap
- #
- class Omap < ::Array
- yaml_as "tag:yaml.org,2002:omap"
- def yaml_initialize( tag, val )
- if Array === val
- val.each do |v|
- if Hash === v
- concat( v.to_a ) # Convert the map to a sequence
- else
- raise YAML::Error, "Invalid !omap entry: " + val.inspect
- end
- end
- else
- raise YAML::Error, "Invalid !omap: " + val.inspect
- end
- self
- end
- def self.[]( *vals )
- o = Omap.new
- 0.step( vals.length - 1, 2 ) do |i|
- o[vals[i]] = vals[i+1]
- end
- o
- end
- def []( k )
- self.assoc( k ).to_a[1]
- end
- def []=( k, *rest )
- val, set = rest.reverse
- if ( tmp = self.assoc( k ) ) and not set
- tmp[1] = val
- else
- self << [ k, val ]
- end
- val
- end
- def has_key?( k )
- self.assoc( k ) ? true : false
- end
- def is_complex_yaml?
- true
- end
- def to_yaml( opts = {} )
- YAML::quick_emit( self.object_id, opts ) do |out|
- out.seq( taguri, to_yaml_style ) do |seq|
- self.each do |v|
- seq.add( Hash[ *v ] )
- end
- end
- end
- end
- end
-
- #
- # Builtin collection: !pairs
- #
- class Pairs < ::Array
- yaml_as "tag:yaml.org,2002:pairs"
- def yaml_initialize( tag, val )
- if Array === val
- val.each do |v|
- if Hash === v
- concat( v.to_a ) # Convert the map to a sequence
- else
- raise YAML::Error, "Invalid !pairs entry: " + val.inspect
- end
- end
- else
- raise YAML::Error, "Invalid !pairs: " + val.inspect
- end
- self
- end
- def self.[]( *vals )
- p = Pairs.new
- 0.step( vals.length - 1, 2 ) { |i|
- p[vals[i]] = vals[i+1]
- }
- p
- end
- def []( k )
- self.assoc( k ).to_a
- end
- def []=( k, val )
- self << [ k, val ]
- val
- end
- def has_key?( k )
- self.assoc( k ) ? true : false
- end
- def is_complex_yaml?
- true
- end
- def to_yaml( opts = {} )
- YAML::quick_emit( self.object_id, opts ) do |out|
- out.seq( taguri, to_yaml_style ) do |seq|
- self.each do |v|
- seq.add( Hash[ *v ] )
- end
- end
- end
- end
- end
-
- #
- # Builtin collection: !set
- #
- class Set < ::Hash
- yaml_as "tag:yaml.org,2002:set"
- end
-end
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
diff --git a/lib/yaml/yamlnode.rb b/lib/yaml/yamlnode.rb
deleted file mode 100644
index e36a18e694..0000000000
--- a/lib/yaml/yamlnode.rb
+++ /dev/null
@@ -1,54 +0,0 @@
-#
-# YAML::YamlNode class
-#
-require 'yaml/basenode'
-
-module YAML
-
- #
- # YAML Generic Model container
- #
- class YamlNode
- include BaseNode
- attr_accessor :kind, :type_id, :value, :anchor
- def initialize( t, v )
- @type_id = t
- if Hash === v
- @kind = 'map'
- @value = {}
- v.each { |k,v|
- @value[ k.transform ] = [ k, v ]
- }
- elsif Array === v
- @kind = 'seq'
- @value = v
- elsif String === v
- @kind = 'scalar'
- @value = v
- end
- end
-
- #
- # Transform this node fully into a native type
- #
- def transform
- t = nil
- if @value.is_a? Hash
- t = {}
- @value.each { |k,v|
- t[ k ] = v[1].transform
- }
- elsif @value.is_a? Array
- t = []
- @value.each { |v|
- t.push v.transform
- }
- else
- t = @value
- end
- YAML.transfer_method( @type_id, t )
- end
-
- end
-
-end
diff --git a/lib/yaml/ypath.rb b/lib/yaml/ypath.rb
deleted file mode 100644
index 81348ca043..0000000000
--- a/lib/yaml/ypath.rb
+++ /dev/null
@@ -1,52 +0,0 @@
-#
-# YAML::YPath
-#
-
-module YAML
-
- class YPath
- attr_accessor :segments, :predicates, :flags
- def initialize( str )
- @segments = []
- @predicates = []
- @flags = nil
- while str =~ /^\/?(\/|[^\/\[]+)(?:\[([^\]]+)\])?/
- @segments.push $1
- @predicates.push $2
- str = $'
- end
- unless str.to_s.empty?
- @segments += str.split( "/" )
- end
- if @segments.length == 0
- @segments.push "."
- end
- end
- def YPath.each_path( str )
- #
- # Find choices
- #
- paths = []
- str = "(#{ str })"
- while str.sub!( /\(([^()]+)\)/, "\n#{ paths.length }\n" )
- paths.push $1.split( '|' )
- end
-
- #
- # Construct all possible paths
- #
- all = [ str ]
- ( paths.length - 1 ).downto( 0 ) do |i|
- all = all.collect do |a|
- paths[i].collect do |p|
- a.gsub( /\n#{ i }\n/, p )
- end
- end.flatten.uniq
- end
- all.collect do |path|
- yield YPath.new( path )
- end
- end
- end
-
-end