Permalink
Browse files

implementing some deprecated api methods

  • Loading branch information...
1 parent 6faec13 commit 774977e39b4a31b32f7ea607b2af223507e59f59 @tenderlove tenderlove committed Apr 9, 2010
Showing with 177 additions and 3 deletions.
  1. +1 −1 Rakefile
  2. +1 −0 ext/psych/parser.c
  3. +1 −0 lib/psych.rb
  4. +18 −0 lib/psych/deprecated.rb
  5. +8 −2 lib/psych/visitors/to_ruby.rb
  6. +13 −0 lib/psych/visitors/yaml_tree.rb
  7. +135 −0 test/psych/test_deprecated.rb
View
@@ -26,7 +26,7 @@ Hoe.spec 'psych' do
end
end
-Hoe.add_include_dirs('.')
+Hoe.add_include_dirs('.:lib/psych')
task :test => :compile
View
@@ -57,6 +57,7 @@ static VALUE parse(VALUE self, VALUE yaml)
if(rb_respond_to(yaml, id_read)) {
yaml_parser_set_input(&parser, io_reader, (void *)yaml);
} else {
+ Check_Type(yaml, T_STRING);
yaml_parser_set_input_string(
&parser,
(const unsigned char *)StringValuePtr(yaml),
View
@@ -8,6 +8,7 @@
require 'psych/set'
require 'psych/coder'
require 'psych/core_ext'
+require 'psych/deprecated'
###
# = Overview
@@ -0,0 +1,18 @@
+module Psych
+ module DeprecatedMethods # :nodoc:
+ attr_accessor :taguri
+ attr_accessor :to_yaml_style
+ end
+
+ def self.quick_emit thing, opts = {}, &block # :nodoc:
+ 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
@@ -219,10 +219,16 @@ def revive klass, node
end
def init_with o, h, node
+ c = Psych::Coder.new(node.tag)
+ c.map = h
+
if o.respond_to?(:init_with)
- c = Psych::Coder.new(node.tag)
- c.map = h
o.init_with c
+ elsif o.respond_to?(:yaml_initialize)
+ if $VERBOSE
+ "Implementing #{o.class}#yaml_initialize is deprecated, please implement \"init_with(coder)\""
+ end
+ o.yaml_initialize c.tag, c.map
else
h.each { |k,v| o.instance_variable_set(:"@#{k}", v) }
end
@@ -36,6 +36,19 @@ def accept target
return append Nodes::Alias.new target.object_id.to_s
end
+ if target.respond_to?(:to_yaml)
+ loc = target.method(:to_yaml).source_location.first
+ if loc !~ /(syck\/rubytypes.rb|psych\/core_ext.rb)/
+ unless target.respond_to?(:encode_with)
+ if $VERBOSE
+ warn "implementing to_yaml is deprecated, please implement \"encode_with\""
+ end
+
+ target.to_yaml(:nodump => true)
+ end
+ end
+ end
+
if target.respond_to?(:encode_with)
dump_coder target
else
@@ -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

0 comments on commit 774977e

Please sign in to comment.