* ext/psych/lib/psych.rb: supporting deprecated syck method
add_ruby_type * ext/psych/lib/psych/visitors/to_ruby.rb: ditto * test/psych/test_deprecated.rb: ditto * test/psych/test_psych.rb: ditto git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27469 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
655b2ade49
commit
7309f9cc00
@ -207,11 +207,22 @@ module Psych
|
|||||||
# :stopdoc:
|
# :stopdoc:
|
||||||
@domain_types = {}
|
@domain_types = {}
|
||||||
def self.add_domain_type domain, type_tag, &block
|
def self.add_domain_type domain, type_tag, &block
|
||||||
@domain_types[type_tag] = ["http://#{domain}", block]
|
key = ['tag', domain, type_tag].join ':'
|
||||||
|
@domain_types[key] = [key, block]
|
||||||
|
@domain_types["tag:#{type_tag}"] = [key, block]
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.add_builtin_type type_tag, &block
|
def self.add_builtin_type type_tag, &block
|
||||||
@domain_types[type_tag] = ['yaml.org', block]
|
domain = 'yaml.org,2002'
|
||||||
|
key = ['tag', domain, type_tag].join ':'
|
||||||
|
@domain_types[key] = [key, block]
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.add_ruby_type type_tag, &block
|
||||||
|
warn "#{caller[0]}: add_ruby_type is deprecated, use add_domain_type" if $VERBOSE && !caller[0].start_with?(File.dirname(__FILE__))
|
||||||
|
domain = 'ruby.yaml.org,2002'
|
||||||
|
key = ['tag', domain, type_tag].join ':'
|
||||||
|
@domain_types[key] = [key, block]
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.remove_type type_tag
|
def self.remove_type type_tag
|
||||||
|
@ -16,10 +16,12 @@ module Psych
|
|||||||
result = super
|
result = super
|
||||||
return result if @domain_types.empty? || !target.tag
|
return result if @domain_types.empty? || !target.tag
|
||||||
|
|
||||||
short_name = target.tag.sub(/^!/, '').split('/', 2).last
|
key = target.tag.sub(/^[!\/]*/, '').sub(/(,\d+)\//, '\1:')
|
||||||
if Psych.domain_types.key? short_name
|
key = "tag:#{key}" unless key.start_with?('tag:')
|
||||||
url, block = Psych.domain_types[short_name]
|
|
||||||
return block.call "#{url}:#{short_name}", result
|
if @domain_types.key? key
|
||||||
|
value, block = @domain_types[key]
|
||||||
|
return block.call value, result
|
||||||
end
|
end
|
||||||
|
|
||||||
result
|
result
|
||||||
|
@ -2,6 +2,10 @@ require_relative 'helper'
|
|||||||
|
|
||||||
module Psych
|
module Psych
|
||||||
class TestDeprecated < TestCase
|
class TestDeprecated < TestCase
|
||||||
|
def teardown
|
||||||
|
Psych.domain_types.clear
|
||||||
|
end
|
||||||
|
|
||||||
class QuickEmitter
|
class QuickEmitter
|
||||||
attr_reader :name
|
attr_reader :name
|
||||||
attr_reader :value
|
attr_reader :value
|
||||||
@ -147,5 +151,17 @@ module Psych
|
|||||||
def test_yaml_as
|
def test_yaml_as
|
||||||
assert_match(/helloworld/, Psych.dump(YamlAs.new))
|
assert_match(/helloworld/, Psych.dump(YamlAs.new))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_ruby_type
|
||||||
|
types = []
|
||||||
|
appender = lambda { |*args| types << args }
|
||||||
|
|
||||||
|
Psych.add_ruby_type('foo', &appender)
|
||||||
|
Psych.load <<-eoyml
|
||||||
|
- !ruby.yaml.org,2002/foo bar
|
||||||
|
eoyml
|
||||||
|
|
||||||
|
assert_equal [["tag:ruby.yaml.org,2002:foo", "bar"]], types
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -4,6 +4,10 @@ require 'stringio'
|
|||||||
require 'tempfile'
|
require 'tempfile'
|
||||||
|
|
||||||
class TestPsych < Psych::TestCase
|
class TestPsych < Psych::TestCase
|
||||||
|
def teardown
|
||||||
|
Psych.domain_types.clear
|
||||||
|
end
|
||||||
|
|
||||||
def test_load_argument_error
|
def test_load_argument_error
|
||||||
assert_raises(TypeError) do
|
assert_raises(TypeError) do
|
||||||
Psych.load nil
|
Psych.load nil
|
||||||
@ -56,7 +60,7 @@ class TestPsych < Psych::TestCase
|
|||||||
Psych.add_builtin_type 'omap', do |type, val|
|
Psych.add_builtin_type 'omap', do |type, val|
|
||||||
got = val
|
got = val
|
||||||
end
|
end
|
||||||
Psych.load('--- !omap hello')
|
Psych.load('--- !!omap hello')
|
||||||
assert_equal 'hello', got
|
assert_equal 'hello', got
|
||||||
ensure
|
ensure
|
||||||
Psych.remove_type 'omap'
|
Psych.remove_type 'omap'
|
||||||
@ -98,4 +102,21 @@ class TestPsych < Psych::TestCase
|
|||||||
assert_equal false, Psych.load('')
|
assert_equal false, Psych.load('')
|
||||||
assert_equal false, Psych.parse('')
|
assert_equal false, Psych.parse('')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_callbacks
|
||||||
|
types = []
|
||||||
|
appender = lambda { |*args| types << args }
|
||||||
|
|
||||||
|
Psych.add_builtin_type('foo', &appender)
|
||||||
|
Psych.add_domain_type('example.com,2002', 'foo', &appender)
|
||||||
|
Psych.load <<-eoyml
|
||||||
|
- !tag:yaml.org,2002:foo bar
|
||||||
|
- !tag:example.com,2002:foo bar
|
||||||
|
eoyml
|
||||||
|
|
||||||
|
assert_equal [
|
||||||
|
["tag:yaml.org,2002:foo", "bar"],
|
||||||
|
["tag:example.com,2002:foo", "bar"]
|
||||||
|
], types
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user