mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* ext/psych/lib/psych/deprecated.rb: implementing Psych.quick_emit and
adding deprecation warnings. * ext/psych/lib/psych/visitors/to_ruby.rb: supporting deprecated yaml_initialize api. * ext/psych/lib/psych/visitors/yaml_tree.rb: supporting deprecated to_yaml api. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27279 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
052f204dcb
commit
186e575827
2 changed files with 154 additions and 0 deletions
19
ext/psych/lib/psych/deprecated.rb
Normal file
19
ext/psych/lib/psych/deprecated.rb
Normal file
|
@ -0,0 +1,19 @@
|
|||
module Psych
|
||||
module DeprecatedMethods # :nodoc:
|
||||
attr_accessor :taguri
|
||||
attr_accessor :to_yaml_style
|
||||
end
|
||||
|
||||
def self.quick_emit thing, opts = {}, &block # :nodoc:
|
||||
warn "#{caller[0]}: YAML.quick_emit is deprecated" if $VERBOSE && !caller[0].start_with?(File.dirname(__FILE__))
|
||||
target = eval 'self', block.binding
|
||||
target.extend DeprecatedMethods
|
||||
metaclass = class << target; self; end
|
||||
metaclass.send(:define_method, :encode_with) do |coder|
|
||||
target.taguri = coder.tag
|
||||
target.to_yaml_style = coder.style
|
||||
block.call coder
|
||||
end
|
||||
target.psych_to_yaml unless opts[:nodump]
|
||||
end
|
||||
end
|
135
test/psych/test_deprecated.rb
Normal file
135
test/psych/test_deprecated.rb
Normal file
|
@ -0,0 +1,135 @@
|
|||
require_relative 'helper'
|
||||
|
||||
module Psych
|
||||
class TestDeprecated < TestCase
|
||||
class QuickEmitter
|
||||
attr_reader :name
|
||||
attr_reader :value
|
||||
|
||||
def initialize
|
||||
@name = 'hello!!'
|
||||
@value = 'Friday!'
|
||||
end
|
||||
|
||||
def to_yaml opts = {}
|
||||
Psych.quick_emit object_id, opts do |out|
|
||||
out.map taguri, to_yaml_style do |map|
|
||||
map.add 'name', @name
|
||||
map.add 'value', nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def setup
|
||||
@qe = QuickEmitter.new
|
||||
end
|
||||
|
||||
def test_quick_emit
|
||||
qe2 = Psych.load @qe.to_yaml
|
||||
assert_equal @qe.name, qe2.name
|
||||
assert_instance_of QuickEmitter, qe2
|
||||
assert_nil qe2.value
|
||||
end
|
||||
|
||||
def test_recursive_quick_emit
|
||||
hash = { :qe => @qe }
|
||||
hash2 = Psych.load Psych.dump hash
|
||||
qe = hash2[:qe]
|
||||
|
||||
assert_equal @qe.name, qe.name
|
||||
assert_instance_of QuickEmitter, qe
|
||||
assert_nil qe.value
|
||||
end
|
||||
|
||||
class QuickEmitterEncodeWith
|
||||
attr_reader :name
|
||||
attr_reader :value
|
||||
|
||||
def initialize
|
||||
@name = 'hello!!'
|
||||
@value = 'Friday!'
|
||||
end
|
||||
|
||||
def encode_with coder
|
||||
coder.map do |map|
|
||||
map.add 'name', @name
|
||||
map.add 'value', nil
|
||||
end
|
||||
end
|
||||
|
||||
def to_yaml opts = {}
|
||||
raise
|
||||
end
|
||||
end
|
||||
|
||||
###
|
||||
# An object that defines both to_yaml and encode_with should only call
|
||||
# encode_with.
|
||||
def test_recursive_quick_emit_encode_with
|
||||
qeew = QuickEmitterEncodeWith.new
|
||||
hash = { :qe => qeew }
|
||||
hash2 = Psych.load Psych.dump hash
|
||||
qe = hash2[:qe]
|
||||
|
||||
assert_equal qeew.name, qe.name
|
||||
assert_instance_of QuickEmitterEncodeWith, qe
|
||||
assert_nil qe.value
|
||||
end
|
||||
|
||||
class YamlInit
|
||||
attr_reader :name
|
||||
attr_reader :value
|
||||
|
||||
def initialize
|
||||
@name = 'hello!!'
|
||||
@value = 'Friday!'
|
||||
end
|
||||
|
||||
def yaml_initialize tag, vals
|
||||
vals.each { |ivar, val| instance_variable_set "@#{ivar}", 'TGIF!' }
|
||||
end
|
||||
end
|
||||
|
||||
def test_yaml_initialize
|
||||
hash = { :yi => YamlInit.new }
|
||||
hash2 = Psych.load Psych.dump hash
|
||||
yi = hash2[:yi]
|
||||
|
||||
assert_equal 'TGIF!', yi.name
|
||||
assert_equal 'TGIF!', yi.value
|
||||
assert_instance_of YamlInit, yi
|
||||
end
|
||||
|
||||
class YamlInitAndInitWith
|
||||
attr_reader :name
|
||||
attr_reader :value
|
||||
|
||||
def initialize
|
||||
@name = 'shaners'
|
||||
@value = 'Friday!'
|
||||
end
|
||||
|
||||
def init_with coder
|
||||
coder.map.each { |ivar, val| instance_variable_set "@#{ivar}", 'TGIF!' }
|
||||
end
|
||||
|
||||
def yaml_initialize tag, vals
|
||||
raise
|
||||
end
|
||||
end
|
||||
|
||||
###
|
||||
# An object that implements both yaml_initialize and init_with should not
|
||||
# receive the yaml_initialize call.
|
||||
def test_yaml_initialize_and_init_with
|
||||
hash = { :yi => YamlInitAndInitWith.new }
|
||||
hash2 = Psych.load Psych.dump hash
|
||||
yi = hash2[:yi]
|
||||
|
||||
assert_equal 'TGIF!', yi.name
|
||||
assert_equal 'TGIF!', yi.value
|
||||
assert_instance_of YamlInitAndInitWith, yi
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue