Skip to content

Commit

Permalink
porting all constants to use Syck
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Sep 24, 2012
1 parent a201054 commit 0fe4b21
Show file tree
Hide file tree
Showing 20 changed files with 127 additions and 498 deletions.
4 changes: 2 additions & 2 deletions lib/syck.rb
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,9 @@ module Kernel
def y( object, *objects )
objects.unshift object
puts( if objects.length == 1
YAML.dump( *objects )
Syck.dump( *objects )
else
YAML.dump_stream( *objects )
Syck.dump_stream( *objects )
end )
end
private :y
Expand Down
83 changes: 33 additions & 50 deletions lib/syck/rubytypes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def to_yaml_style; end
undef to_yaml_properties rescue nil
def to_yaml_properties; instance_variables.sort; end
def to_yaml( opts = {} )
YAML::quick_emit( self, opts ) do |out|
Syck::quick_emit( self, 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 ) )
Expand All @@ -33,12 +33,11 @@ def yaml_initialize( tag, val )
elsif Hash === val
update val
else
raise YAML::TypeError, "Invalid map explicitly tagged #{ tag }: " + val.inspect
raise Syck::TypeError, "Invalid map explicitly tagged #{ tag }: " + val.inspect
end
end
def to_yaml( opts = {} )
return super unless YAML::ENGINE.syck?
YAML::quick_emit( self, opts ) do |out|
Syck::quick_emit( self, opts ) do |out|
out.map( taguri, to_yaml_style ) do |map|
each do |k, v|
map.add( k, v )
Expand All @@ -62,7 +61,7 @@ def self.yaml_new( klass, tag, val )
props = {}
val.delete_if { |k,v| props[k] = v if k =~ /^@/ }
begin
struct_type = YAML.read_type_class( tag, Struct ).last
struct_type = Syck.read_type_class( tag, Struct ).last
rescue NameError
end
if not struct_type
Expand All @@ -73,7 +72,7 @@ def self.yaml_new( klass, tag, val )
#
# Set the Struct properties
#
st = YAML::object_maker( struct_type, {} )
st = Syck::object_maker( struct_type, {} )
st.members.each do |m|
st.send( "#{m}=", val[m.to_s] )
end
Expand All @@ -82,12 +81,11 @@ def self.yaml_new( klass, tag, val )
end
st
else
raise YAML::TypeError, "Invalid Ruby Struct: " + val.inspect
raise Syck::TypeError, "Invalid Ruby Struct: " + val.inspect
end
end
def to_yaml( opts = {} )
return super unless YAML::ENGINE.syck?
YAML::quick_emit( self, opts ) do |out|
Syck::quick_emit( self, opts ) do |out|
#
# Basic struct is passed as a YAML map
#
Expand All @@ -108,8 +106,7 @@ class Array
yaml_as "tag:yaml.org,2002:seq"
def yaml_initialize( tag, val ); concat( val.to_a ); end
def to_yaml( opts = {} )
return super unless YAML::ENGINE.syck?
YAML::quick_emit( self, opts ) do |out|
Syck::quick_emit( self, opts ) do |out|
out.seq( taguri, to_yaml_style ) do |seq|
each do |x|
seq.add( x )
Expand All @@ -130,8 +127,7 @@ def Exception.yaml_new( klass, tag, val )
o
end
def to_yaml( opts = {} )
return super unless YAML::ENGINE.syck?
YAML::quick_emit( self, opts ) do |out|
Syck::quick_emit( self, opts ) do |out|
out.map( taguri, to_yaml_style ) do |map|
map.add( 'message', message )
to_yaml_properties.each do |m|
Expand Down Expand Up @@ -164,12 +160,11 @@ def String.yaml_new( klass, tag, val )
val.each { |k,v| s.instance_variable_set( k, v ) }
s
else
raise YAML::TypeError, "Invalid String: " + val.inspect
raise Syck::TypeError, "Invalid String: " + val.inspect
end
end
def to_yaml( opts = {} )
return super unless YAML::ENGINE.syck?
YAML::quick_emit( is_complex_yaml? ? self : nil, opts ) do |out|
Syck::quick_emit( is_complex_yaml? ? self : nil, opts ) do |out|
if is_binary_data?
out.scalar( "tag:yaml.org,2002:binary", [self].pack("m"), :literal )
elsif to_yaml_properties.empty?
Expand All @@ -191,15 +186,14 @@ class 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 = Syck::load( val ) if val =~ /\A(["']).*\1\z/
val.intern
else
raise YAML::TypeError, "Invalid Symbol: " + val.inspect
raise Syck::TypeError, "Invalid Symbol: " + val.inspect
end
end
def to_yaml( opts = {} )
return super unless YAML::ENGINE.syck?
YAML::quick_emit( nil, opts ) do |out|
Syck::quick_emit( nil, opts ) do |out|
out.scalar( "tag:yaml.org,2002:str", self.inspect, :plain )
end
end
Expand All @@ -213,8 +207,8 @@ def Range.yaml_new( klass, tag, val )
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}" ),
'begin' => Syck.load( "--- #{r1}" ),
'end' => Syck.load( "--- #{r2}" ),
'excl' => rdots.length == 3
}
val = {}
Expand All @@ -224,20 +218,19 @@ def Range.yaml_new( klass, tag, val )
opts['excl'] = val.delete('excl')
end
if Hash === opts
r = YAML::object_maker( klass, {} )
r = Syck::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
raise Syck::TypeError, "Invalid Range: " + val.inspect
end
end
def to_yaml( opts = {} )
return super unless YAML::ENGINE.syck?
YAML::quick_emit( self, opts ) do |out|
Syck::quick_emit( self, 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?
Expand Down Expand Up @@ -276,19 +269,18 @@ def Regexp.yaml_new( klass, tag, val )
mods |= Regexp::NOENCODING if val['mods'].include?( 'n' )
end
val.delete( 'mods' )
r = YAML::object_maker( klass, {} )
r = Syck::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
raise Syck::TypeError, "Invalid Regular expression: " + val.inspect
end
end
def to_yaml( opts = {} )
return super unless YAML::ENGINE.syck?
YAML::quick_emit( nil, opts ) do |out|
Syck::quick_emit( nil, opts ) do |out|
if to_yaml_properties.empty?
out.scalar( taguri, self.inspect, :plain )
else
Expand All @@ -298,7 +290,7 @@ def to_yaml( opts = {} )
map.add( 'regexp', $1 )
map.add( 'mods', $2 )
else
raise YAML::TypeError, "Invalid Regular expression: " + src
raise Syck::TypeError, "Invalid Regular expression: " + src
end
to_yaml_properties.each do |m|
map.add( m, instance_variable_get( m ) )
Expand All @@ -318,12 +310,11 @@ def Time.yaml_new( klass, tag, val )
val.each { |k,v| t.instance_variable_set( k, v ) }
t
else
raise YAML::TypeError, "Invalid Time: " + val.inspect
raise Syck::TypeError, "Invalid Time: " + val.inspect
end
end
def to_yaml( opts = {} )
return super unless YAML::ENGINE.syck?
YAML::quick_emit( self, opts ) do |out|
Syck::quick_emit( self, opts ) do |out|
tz = "Z"
# from the tidy Tobias Peters <t-peters@gmx.de> Thanks!
unless self.utc?
Expand Down Expand Up @@ -360,8 +351,7 @@ def to_yaml( opts = {} )
class Date
yaml_as "tag:yaml.org,2002:timestamp#ymd"
def to_yaml( opts = {} )
return super unless YAML::ENGINE.syck?
YAML::quick_emit( self, opts ) do |out|
Syck::quick_emit( self, opts ) do |out|
out.scalar( "tag:yaml.org,2002:timestamp", self.to_s, :plain )
end
end
Expand All @@ -370,8 +360,7 @@ def to_yaml( opts = {} )
class Integer
yaml_as "tag:yaml.org,2002:int"
def to_yaml( opts = {} )
return super unless YAML::ENGINE.syck?
YAML::quick_emit( nil, opts ) do |out|
Syck::quick_emit( nil, opts ) do |out|
out.scalar( "tag:yaml.org,2002:int", self.to_s, :plain )
end
end
Expand All @@ -380,8 +369,7 @@ def to_yaml( opts = {} )
class Float
yaml_as "tag:yaml.org,2002:float"
def to_yaml( opts = {} )
return super unless YAML::ENGINE.syck?
YAML::quick_emit( nil, opts ) do |out|
Syck::quick_emit( nil, opts ) do |out|
str = self.to_s
if str == "Infinity"
str = ".Inf"
Expand All @@ -405,8 +393,7 @@ def Rational.yaml_new( klass, tag, val )
end
end
def to_yaml( opts = {} )
return super unless YAML::ENGINE.syck?
YAML::quick_emit( self, opts ) do |out|
Syck::quick_emit( self, opts ) do |out|
out.map( taguri, nil ) do |map|
map.add( 'denominator', denominator )
map.add( 'numerator', numerator )
Expand All @@ -425,8 +412,7 @@ def Complex.yaml_new( klass, tag, val )
end
end
def to_yaml( opts = {} )
return super unless YAML::ENGINE.syck?
YAML::quick_emit( self, opts ) do |out|
Syck::quick_emit( self, opts ) do |out|
out.map( taguri, nil ) do |map|
map.add( 'image', imaginary )
map.add( 'real', real )
Expand All @@ -438,8 +424,7 @@ def to_yaml( opts = {} )
class TrueClass
yaml_as "tag:yaml.org,2002:bool#yes"
def to_yaml( opts = {} )
return super unless YAML::ENGINE.syck?
YAML::quick_emit( nil, opts ) do |out|
Syck::quick_emit( nil, opts ) do |out|
out.scalar( taguri, "true", :plain )
end
end
Expand All @@ -448,8 +433,7 @@ def to_yaml( opts = {} )
class FalseClass
yaml_as "tag:yaml.org,2002:bool#no"
def to_yaml( opts = {} )
return super unless YAML::ENGINE.syck?
YAML::quick_emit( nil, opts ) do |out|
Syck::quick_emit( nil, opts ) do |out|
out.scalar( taguri, "false", :plain )
end
end
Expand All @@ -458,8 +442,7 @@ def to_yaml( opts = {} )
class NilClass
yaml_as "tag:yaml.org,2002:null"
def to_yaml( opts = {} )
return super unless YAML::ENGINE.syck?
YAML::quick_emit( nil, opts ) do |out|
Syck::quick_emit( nil, opts ) do |out|
out.scalar( taguri, "", :plain )
end
end
Expand Down
15 changes: 2 additions & 13 deletions test/test_array.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
require 'test/unit'
require 'yaml'
require 'helper'

module Syck
class TestArray < Test::Unit::TestCase
def setup
@current_engine = YAML::ENGINE.yamler
YAML::ENGINE.yamler = 'syck'
@list = [{ :a => 'b' }, 'foo']
end

def teardown
YAML::ENGINE.yamler = @current_engine
end

def test_to_yaml
assert_equal @list, YAML.load(@list.to_yaml)
end

def test_dump
assert_equal @list, YAML.load(YAML.dump(@list))
assert_equal @list, Syck.load(Syck.dump(@list))
end
end
end
15 changes: 7 additions & 8 deletions test/test_boolean.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'test/unit'
require 'yaml'
require 'helper'

module Syck
###
Expand All @@ -8,30 +7,30 @@ module Syck
class TestBoolean < Test::Unit::TestCase
%w{ yes Yes YES true True TRUE on On ON }.each do |truth|
define_method(:"test_#{truth}") do
assert_equal true, YAML.load("--- #{truth}")
assert_equal true, Syck.load("--- #{truth}")
end
end

%w{ no No NO false False FALSE off Off OFF }.each do |truth|
define_method(:"test_#{truth}") do
assert_equal false, YAML.load("--- #{truth}")
assert_equal false, Syck.load("--- #{truth}")
end
end

###
# YAML spec says "y" and "Y" may be used as true, but Syck treats them
# as literal strings
def test_y
assert_equal "y", YAML.load("--- y")
assert_equal "Y", YAML.load("--- Y")
assert_equal "y", Syck.load("--- y")
assert_equal "Y", Syck.load("--- Y")
end

###
# YAML spec says "n" and "N" may be used as false, but Syck treats them
# as literal strings
def test_n
assert_equal "n", YAML.load("--- n")
assert_equal "N", YAML.load("--- N")
assert_equal "n", Syck.load("--- n")
assert_equal "N", Syck.load("--- N")
end
end
end
20 changes: 2 additions & 18 deletions test/test_class.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
require 'test/unit'
require 'yaml'
require 'helper'

module Syck
class TestClass < Test::Unit::TestCase
def setup
@engine = YAML::ENGINE.yamler
YAML::ENGINE.yamler = 'syck'
end

def teardown
YAML::ENGINE.yamler = @engine
end

def test_to_yaml
assert_raises(::TypeError) do
TestClass.to_yaml
end
end

def test_dump
assert_raises(::TypeError) do
YAML.dump TestClass
Syck.dump TestClass
end
end
end
Expand Down
3 changes: 0 additions & 3 deletions test/test_engine_manager.rb

This file was deleted.

Loading

0 comments on commit 0fe4b21

Please sign in to comment.