rubysl-yaml 0.0.1 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +0 -1
- data/.travis.yml +7 -0
- data/README.md +2 -2
- data/Rakefile +0 -1
- data/lib/rubysl/yaml.rb +2 -0
- data/lib/rubysl/yaml/version.rb +5 -0
- data/lib/rubysl/yaml/yaml.rb +440 -0
- data/lib/yaml.rb +1 -0
- data/lib/yaml/baseemitter.rb +247 -0
- data/lib/yaml/basenode.rb +216 -0
- data/lib/yaml/constants.rb +45 -0
- data/lib/yaml/dbm.rb +111 -0
- data/lib/yaml/encoding.rb +33 -0
- data/lib/yaml/error.rb +34 -0
- data/lib/yaml/loader.rb +14 -0
- data/lib/yaml/rubytypes.rb +409 -0
- data/lib/yaml/store.rb +43 -0
- data/lib/yaml/stream.rb +40 -0
- data/lib/yaml/stringio.rb +83 -0
- data/lib/yaml/syck.rb +44 -0
- data/lib/yaml/tag.rb +91 -0
- data/lib/yaml/types.rb +192 -0
- data/lib/yaml/yamlnode.rb +54 -0
- data/lib/yaml/ypath.rb +52 -0
- data/rubysl-yaml.gemspec +19 -17
- data/spec/add_builtin_type_spec.rb +1 -0
- data/spec/add_domain_type_spec.rb +1 -0
- data/spec/add_private_type_spec.rb +1 -0
- data/spec/add_ruby_type_spec.rb +1 -0
- data/spec/detect_implicit_spec.rb +1 -0
- data/spec/dump_spec.rb +35 -0
- data/spec/dump_stream_spec.rb +13 -0
- data/spec/each_document_spec.rb +9 -0
- data/spec/each_node_spec.rb +1 -0
- data/spec/emitter_spec.rb +1 -0
- data/spec/fixtures/common.rb +10 -0
- data/spec/fixtures/example_class.rb +5 -0
- data/spec/fixtures/strings.rb +36 -0
- data/spec/fixtures/test_yaml.yml +2 -0
- data/spec/generic_parser_spec.rb +1 -0
- data/spec/load_documents_spec.rb +7 -0
- data/spec/load_file_spec.rb +12 -0
- data/spec/load_spec.rb +111 -0
- data/spec/load_stream_spec.rb +1 -0
- data/spec/object_maker_spec.rb +1 -0
- data/spec/parse_documents_spec.rb +1 -0
- data/spec/parse_file_spec.rb +9 -0
- data/spec/parse_spec.rb +21 -0
- data/spec/parser_spec.rb +1 -0
- data/spec/quick_emit_spec.rb +1 -0
- data/spec/read_type_class_spec.rb +1 -0
- data/spec/shared/each_document.rb +18 -0
- data/spec/tag_class_spec.rb +9 -0
- data/spec/tagged_classes_spec.rb +9 -0
- data/spec/tagurize_spec.rb +8 -0
- data/spec/to_yaml_spec.rb +97 -0
- data/spec/transfer_spec.rb +1 -0
- data/spec/try_implicit_spec.rb +1 -0
- metadata +163 -88
- data/lib/rubysl-yaml.rb +0 -7
- data/lib/rubysl-yaml/version.rb +0 -5
@@ -0,0 +1,45 @@
|
|
1
|
+
#
|
2
|
+
# Constants used throughout the library
|
3
|
+
#
|
4
|
+
module YAML
|
5
|
+
|
6
|
+
#
|
7
|
+
# Constants
|
8
|
+
#
|
9
|
+
VERSION = '0.60'
|
10
|
+
SUPPORTED_YAML_VERSIONS = ['1.0']
|
11
|
+
|
12
|
+
#
|
13
|
+
# Parser tokens
|
14
|
+
#
|
15
|
+
WORD_CHAR = 'A-Za-z0-9'
|
16
|
+
PRINTABLE_CHAR = '-_A-Za-z0-9!?/()$\'". '
|
17
|
+
NOT_PLAIN_CHAR = '\x7f\x0-\x1f\x80-\x9f'
|
18
|
+
ESCAPE_CHAR = '[\\x00-\\x09\\x0b-\\x1f]'
|
19
|
+
INDICATOR_CHAR = '*&!|\\\\^@%{}[]='
|
20
|
+
SPACE_INDICATORS = '-#:,?'
|
21
|
+
RESTRICTED_INDICATORS = '#:,}]'
|
22
|
+
DNS_COMP_RE = "\\w(?:[-\\w]*\\w)?"
|
23
|
+
DNS_NAME_RE = "(?:(?:#{DNS_COMP_RE}\\.)+#{DNS_COMP_RE}|#{DNS_COMP_RE})"
|
24
|
+
ESCAPES = %w{\x00 \x01 \x02 \x03 \x04 \x05 \x06 \a
|
25
|
+
\x08 \t \n \v \f \r \x0e \x0f
|
26
|
+
\x10 \x11 \x12 \x13 \x14 \x15 \x16 \x17
|
27
|
+
\x18 \x19 \x1a \e \x1c \x1d \x1e \x1f
|
28
|
+
}
|
29
|
+
UNESCAPES = {
|
30
|
+
'a' => "\x07", 'b' => "\x08", 't' => "\x09",
|
31
|
+
'n' => "\x0a", 'v' => "\x0b", 'f' => "\x0c",
|
32
|
+
'r' => "\x0d", 'e' => "\x1b", '\\' => '\\',
|
33
|
+
}
|
34
|
+
|
35
|
+
#
|
36
|
+
# Default settings
|
37
|
+
#
|
38
|
+
DEFAULTS = {
|
39
|
+
:Indent => 2, :UseHeader => false, :UseVersion => false, :Version => '1.0',
|
40
|
+
:SortKeys => false, :AnchorFormat => 'id%03d', :ExplicitTypes => false,
|
41
|
+
:WidthType => 'absolute', :BestWidth => 80,
|
42
|
+
:UseBlock => false, :UseFold => false, :Encoding => :None
|
43
|
+
}
|
44
|
+
|
45
|
+
end
|
data/lib/yaml/dbm.rb
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'dbm'
|
3
|
+
#
|
4
|
+
# YAML + DBM = YDBM
|
5
|
+
# - Same interface as DBM class
|
6
|
+
#
|
7
|
+
module YAML
|
8
|
+
|
9
|
+
class DBM < ::DBM
|
10
|
+
VERSION = "0.1"
|
11
|
+
def []( key )
|
12
|
+
fetch( key )
|
13
|
+
end
|
14
|
+
def []=( key, val )
|
15
|
+
store( key, val )
|
16
|
+
end
|
17
|
+
def fetch( keystr, ifnone = nil )
|
18
|
+
begin
|
19
|
+
val = super( keystr )
|
20
|
+
return YAML::load( val ) if String === val
|
21
|
+
rescue IndexError
|
22
|
+
end
|
23
|
+
if block_given?
|
24
|
+
yield keystr
|
25
|
+
else
|
26
|
+
ifnone
|
27
|
+
end
|
28
|
+
end
|
29
|
+
def index( keystr )
|
30
|
+
super( keystr.to_yaml )
|
31
|
+
end
|
32
|
+
def values_at( *keys )
|
33
|
+
keys.collect { |k| fetch( k ) }
|
34
|
+
end
|
35
|
+
def delete( key )
|
36
|
+
v = super( key )
|
37
|
+
if String === v
|
38
|
+
v = YAML::load( v )
|
39
|
+
end
|
40
|
+
v
|
41
|
+
end
|
42
|
+
def delete_if
|
43
|
+
del_keys = keys.dup
|
44
|
+
del_keys.delete_if { |k| yield( k, fetch( k ) ) == false }
|
45
|
+
del_keys.each { |k| delete( k ) }
|
46
|
+
self
|
47
|
+
end
|
48
|
+
def reject
|
49
|
+
hsh = self.to_hash
|
50
|
+
hsh.reject { |k,v| yield k, v }
|
51
|
+
end
|
52
|
+
def each_pair
|
53
|
+
keys.each { |k| yield k, fetch( k ) }
|
54
|
+
self
|
55
|
+
end
|
56
|
+
def each_value
|
57
|
+
super { |v| yield YAML::load( v ) }
|
58
|
+
self
|
59
|
+
end
|
60
|
+
def values
|
61
|
+
super.collect { |v| YAML::load( v ) }
|
62
|
+
end
|
63
|
+
def has_value?( val )
|
64
|
+
each_value { |v| return true if v == val }
|
65
|
+
return false
|
66
|
+
end
|
67
|
+
def invert
|
68
|
+
h = {}
|
69
|
+
keys.each { |k| h[ self.fetch( k ) ] = k }
|
70
|
+
h
|
71
|
+
end
|
72
|
+
def replace( hsh )
|
73
|
+
clear
|
74
|
+
update( hsh )
|
75
|
+
end
|
76
|
+
def shift
|
77
|
+
a = super
|
78
|
+
a[1] = YAML::load( a[1] ) if a
|
79
|
+
a
|
80
|
+
end
|
81
|
+
def select( *keys )
|
82
|
+
if block_given?
|
83
|
+
self.keys.collect { |k| v = self[k]; [k, v] if yield k, v }.compact
|
84
|
+
else
|
85
|
+
values_at( *keys )
|
86
|
+
end
|
87
|
+
end
|
88
|
+
def store( key, val )
|
89
|
+
super( key, val.to_yaml )
|
90
|
+
val
|
91
|
+
end
|
92
|
+
def update( hsh )
|
93
|
+
hsh.keys.each do |k|
|
94
|
+
self.store( k, hsh.fetch( k ) )
|
95
|
+
end
|
96
|
+
self
|
97
|
+
end
|
98
|
+
def to_a
|
99
|
+
a = []
|
100
|
+
keys.each { |k| a.push [ k, self.fetch( k ) ] }
|
101
|
+
a
|
102
|
+
end
|
103
|
+
def to_hash
|
104
|
+
h = {}
|
105
|
+
keys.each { |k| h[ k ] = self.fetch( k ) }
|
106
|
+
h
|
107
|
+
end
|
108
|
+
alias :each :each_pair
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
#
|
2
|
+
# Handle Unicode-to-Internal conversion
|
3
|
+
#
|
4
|
+
|
5
|
+
module YAML
|
6
|
+
|
7
|
+
#
|
8
|
+
# Escape the string, condensing common escapes
|
9
|
+
#
|
10
|
+
def YAML.escape( value, skip = "" )
|
11
|
+
value.gsub( /\\/, "\\\\\\" ).
|
12
|
+
gsub( /"/, "\\\"" ).
|
13
|
+
gsub( /([\x00-\x1f])/ ) do |x|
|
14
|
+
skip[x] || ESCAPES[ x.unpack("C")[0] ]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
#
|
19
|
+
# Unescape the condenses escapes
|
20
|
+
#
|
21
|
+
def YAML.unescape( value )
|
22
|
+
value.gsub( /\\(?:([nevfbart\\])|0?x([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/ ) { |x|
|
23
|
+
if $3
|
24
|
+
["#$3".hex ].pack('U*')
|
25
|
+
elsif $2
|
26
|
+
[$2].pack( "H2" )
|
27
|
+
else
|
28
|
+
UNESCAPES[$1]
|
29
|
+
end
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
data/lib/yaml/error.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#
|
2
|
+
# Error messages and exception class
|
3
|
+
#
|
4
|
+
|
5
|
+
module YAML
|
6
|
+
|
7
|
+
#
|
8
|
+
# Error messages
|
9
|
+
#
|
10
|
+
|
11
|
+
ERROR_NO_HEADER_NODE = "With UseHeader=false, the node Array or Hash must have elements"
|
12
|
+
ERROR_NEED_HEADER = "With UseHeader=false, the node must be an Array or Hash"
|
13
|
+
ERROR_BAD_EXPLICIT = "Unsupported explicit transfer: '%s'"
|
14
|
+
ERROR_MANY_EXPLICIT = "More than one explicit transfer"
|
15
|
+
ERROR_MANY_IMPLICIT = "More than one implicit request"
|
16
|
+
ERROR_NO_ANCHOR = "No anchor for alias '%s'"
|
17
|
+
ERROR_BAD_ANCHOR = "Invalid anchor: %s"
|
18
|
+
ERROR_MANY_ANCHOR = "More than one anchor"
|
19
|
+
ERROR_ANCHOR_ALIAS = "Can't define both an anchor and an alias"
|
20
|
+
ERROR_BAD_ALIAS = "Invalid alias: %s"
|
21
|
+
ERROR_MANY_ALIAS = "More than one alias"
|
22
|
+
ERROR_ZERO_INDENT = "Can't use zero as an indentation width"
|
23
|
+
ERROR_UNSUPPORTED_VERSION = "This release of YAML.rb does not support YAML version %s"
|
24
|
+
ERROR_UNSUPPORTED_ENCODING = "Attempt to use unsupported encoding: %s"
|
25
|
+
|
26
|
+
#
|
27
|
+
# YAML Error classes
|
28
|
+
#
|
29
|
+
|
30
|
+
class Error < StandardError; end
|
31
|
+
class ParseError < Error; end
|
32
|
+
class TypeError < StandardError; end
|
33
|
+
|
34
|
+
end
|
data/lib/yaml/loader.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#
|
2
|
+
# YAML::Loader class
|
3
|
+
# .. type handling ..
|
4
|
+
#
|
5
|
+
module YAML
|
6
|
+
class Loader
|
7
|
+
TRANSFER_DOMAINS = {
|
8
|
+
'yaml.org,2002' => {},
|
9
|
+
'ruby.yaml.org,2002' => {}
|
10
|
+
}
|
11
|
+
PRIVATE_TYPES = {}
|
12
|
+
IMPLICIT_TYPES = [ 'null', 'bool', 'time', 'int', 'float' ]
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,409 @@
|
|
1
|
+
# -*- mode: ruby; ruby-indent-level: 4; tab-width: 4 -*- vim: sw=4 ts=4
|
2
|
+
require 'date'
|
3
|
+
|
4
|
+
class Class
|
5
|
+
def to_yaml( opts = {} )
|
6
|
+
raise TypeError, "can't dump anonymous class %s" % self.class
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class Object
|
11
|
+
yaml_as "tag:ruby.yaml.org,2002:object"
|
12
|
+
def to_yaml_style; end
|
13
|
+
def to_yaml_properties; instance_variables.sort; end
|
14
|
+
def to_yaml( opts = {} )
|
15
|
+
YAML::quick_emit( self, opts ) do |out|
|
16
|
+
out.map( taguri, to_yaml_style ) do |map|
|
17
|
+
to_yaml_properties.each do |m|
|
18
|
+
map.add( m[1..-1], instance_variable_get( m ) )
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Hash
|
26
|
+
yaml_as "tag:ruby.yaml.org,2002:hash"
|
27
|
+
yaml_as "tag:yaml.org,2002:map"
|
28
|
+
def yaml_initialize( tag, val )
|
29
|
+
if Array === val
|
30
|
+
update Hash.[]( *val ) # Convert the map to a sequence
|
31
|
+
elsif Hash === val
|
32
|
+
update val
|
33
|
+
else
|
34
|
+
raise YAML::TypeError, "Invalid map explicitly tagged #{ tag }: " + val.inspect
|
35
|
+
end
|
36
|
+
end
|
37
|
+
def to_yaml( opts = {} )
|
38
|
+
YAML::quick_emit( self, opts ) do |out|
|
39
|
+
out.map( taguri, to_yaml_style ) do |map|
|
40
|
+
each do |k, v|
|
41
|
+
map.add( k, v )
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class Struct
|
49
|
+
yaml_as "tag:ruby.yaml.org,2002:struct"
|
50
|
+
def self.yaml_tag_class_name; self.name.gsub( "Struct::", "" ); end
|
51
|
+
def self.yaml_tag_read_class( name ); "Struct::#{ name }"; end
|
52
|
+
def self.yaml_new( klass, tag, val )
|
53
|
+
if Hash === val
|
54
|
+
struct_type = nil
|
55
|
+
|
56
|
+
#
|
57
|
+
# Use existing Struct if it exists
|
58
|
+
#
|
59
|
+
props = {}
|
60
|
+
val.delete_if { |k,v| props[k] = v if k =~ /^@/ }
|
61
|
+
begin
|
62
|
+
struct_name, struct_type = YAML.read_type_class( tag, Struct )
|
63
|
+
rescue NameError
|
64
|
+
end
|
65
|
+
if not struct_type
|
66
|
+
struct_def = [ tag.split( ':', 4 ).last ]
|
67
|
+
struct_type = Struct.new( *struct_def.concat( val.keys.collect { |k| k.intern } ) )
|
68
|
+
end
|
69
|
+
|
70
|
+
#
|
71
|
+
# Set the Struct properties
|
72
|
+
#
|
73
|
+
st = YAML::object_maker( struct_type, {} )
|
74
|
+
st.members.each do |m|
|
75
|
+
st.send( "#{m}=", val[m] )
|
76
|
+
end
|
77
|
+
props.each do |k,v|
|
78
|
+
st.instance_variable_set(k, v)
|
79
|
+
end
|
80
|
+
st
|
81
|
+
else
|
82
|
+
raise YAML::TypeError, "Invalid Ruby Struct: " + val.inspect
|
83
|
+
end
|
84
|
+
end
|
85
|
+
def to_yaml( opts = {} )
|
86
|
+
YAML::quick_emit( self, opts ) do |out|
|
87
|
+
#
|
88
|
+
# Basic struct is passed as a YAML map
|
89
|
+
#
|
90
|
+
out.map( taguri, to_yaml_style ) do |map|
|
91
|
+
self.members.each do |m|
|
92
|
+
map.add( m, self[m] )
|
93
|
+
end
|
94
|
+
self.to_yaml_properties.each do |m|
|
95
|
+
map.add( m, instance_variable_get( m ) )
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
class Array
|
103
|
+
yaml_as "tag:ruby.yaml.org,2002:array"
|
104
|
+
yaml_as "tag:yaml.org,2002:seq"
|
105
|
+
def yaml_initialize( tag, val ); concat( val.to_a ); end
|
106
|
+
def to_yaml( opts = {} )
|
107
|
+
YAML::quick_emit( self, opts ) do |out|
|
108
|
+
out.seq( taguri, to_yaml_style ) do |seq|
|
109
|
+
each do |x|
|
110
|
+
seq.add( x )
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
class Exception
|
118
|
+
yaml_as "tag:ruby.yaml.org,2002:exception"
|
119
|
+
def Exception.yaml_new( klass, tag, val )
|
120
|
+
o = YAML.object_maker( klass, { 'mesg' => val.delete( 'message' ) } )
|
121
|
+
val.each_pair do |k,v|
|
122
|
+
o.instance_variable_set("@#{k}", v)
|
123
|
+
end
|
124
|
+
o
|
125
|
+
end
|
126
|
+
def to_yaml( opts = {} )
|
127
|
+
YAML::quick_emit( self, opts ) do |out|
|
128
|
+
out.map( taguri, to_yaml_style ) do |map|
|
129
|
+
map.add( 'message', message )
|
130
|
+
to_yaml_properties.each do |m|
|
131
|
+
map.add( m[1..-1], instance_variable_get( m ) )
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
class String
|
139
|
+
yaml_as "tag:ruby.yaml.org,2002:string"
|
140
|
+
yaml_as "tag:yaml.org,2002:binary"
|
141
|
+
yaml_as "tag:yaml.org,2002:str"
|
142
|
+
def is_complex_yaml?
|
143
|
+
to_yaml_style or not to_yaml_properties.empty? or self =~ /\n.+/
|
144
|
+
end
|
145
|
+
def is_binary_data?
|
146
|
+
# It's binary if it's got a null.
|
147
|
+
index(0)
|
148
|
+
end
|
149
|
+
def String.yaml_new( klass, tag, val )
|
150
|
+
val = val.unpack("m")[0] if tag == "tag:yaml.org,2002:binary"
|
151
|
+
val = { 'str' => val } if String === val
|
152
|
+
if Hash === val
|
153
|
+
s = klass.allocate
|
154
|
+
# Thank you, NaHi
|
155
|
+
String.instance_method(:initialize).
|
156
|
+
bind(s).
|
157
|
+
call( val.delete( 'str' ) )
|
158
|
+
val.each { |k,v| s.instance_variable_set( k, v ) }
|
159
|
+
s
|
160
|
+
else
|
161
|
+
raise YAML::TypeError, "Invalid String: " + val.inspect
|
162
|
+
end
|
163
|
+
end
|
164
|
+
def to_yaml( opts = {} )
|
165
|
+
YAML::quick_emit( is_complex_yaml? ? self : nil, opts ) do |out|
|
166
|
+
if is_binary_data?
|
167
|
+
out.scalar( "tag:yaml.org,2002:binary", [self].pack("m"), :literal )
|
168
|
+
elsif to_yaml_properties.empty?
|
169
|
+
out.scalar( taguri, self, self =~ /^:/ ? :quote2 : to_yaml_style )
|
170
|
+
else
|
171
|
+
out.map( taguri, to_yaml_style ) do |map|
|
172
|
+
map.add( 'str', "#{self}" )
|
173
|
+
to_yaml_properties.each do |m|
|
174
|
+
map.add( m, instance_variable_get( m ) )
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
class Symbol
|
183
|
+
yaml_as "tag:ruby.yaml.org,2002:symbol"
|
184
|
+
yaml_as "tag:ruby.yaml.org,2002:sym"
|
185
|
+
def Symbol.yaml_new( klass, tag, val )
|
186
|
+
if String === val
|
187
|
+
val = YAML::load( val ) if val =~ /\A(["']).*\1\z/
|
188
|
+
val.intern
|
189
|
+
else
|
190
|
+
raise YAML::TypeError, "Invalid Symbol: " + val.inspect
|
191
|
+
end
|
192
|
+
end
|
193
|
+
def to_yaml( opts = {} )
|
194
|
+
YAML::quick_emit( nil, opts ) do |out|
|
195
|
+
out.scalar( "tag:yaml.org,2002:str", self.inspect, :plain )
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
class Range
|
201
|
+
yaml_as "tag:ruby.yaml.org,2002:range"
|
202
|
+
def Range.yaml_new( klass, tag, val )
|
203
|
+
inr = %r'(\w+|[+-]?\d+(?:\.\d+)?(?:e[+-]\d+)?|"(?:[^\\"]|\\.)*")'
|
204
|
+
opts = {}
|
205
|
+
if String === val and val =~ /^#{inr}(\.{2,3})#{inr}$/o
|
206
|
+
r1, rdots, r2 = $1, $2, $3
|
207
|
+
opts = {
|
208
|
+
'begin' => YAML.load( "--- #{r1}" ),
|
209
|
+
'end' => YAML.load( "--- #{r2}" ),
|
210
|
+
'excl' => rdots.length == 3
|
211
|
+
}
|
212
|
+
val = {}
|
213
|
+
elsif Hash === val
|
214
|
+
opts['begin'] = val.delete('begin')
|
215
|
+
opts['end'] = val.delete('end')
|
216
|
+
opts['excl'] = val.delete('excl')
|
217
|
+
end
|
218
|
+
if Hash === opts
|
219
|
+
r = YAML::object_maker( klass, {} )
|
220
|
+
# Thank you, NaHi
|
221
|
+
Range.instance_method(:initialize).
|
222
|
+
bind(r).
|
223
|
+
call( opts['begin'], opts['end'], opts['excl'] )
|
224
|
+
val.each { |k,v| r.instance_variable_set( k, v ) }
|
225
|
+
r
|
226
|
+
else
|
227
|
+
raise YAML::TypeError, "Invalid Range: " + val.inspect
|
228
|
+
end
|
229
|
+
end
|
230
|
+
def to_yaml( opts = {} )
|
231
|
+
YAML::quick_emit( self, opts ) do |out|
|
232
|
+
# if self.begin.is_complex_yaml? or self.begin.respond_to? :to_str or
|
233
|
+
# self.end.is_complex_yaml? or self.end.respond_to? :to_str or
|
234
|
+
# not to_yaml_properties.empty?
|
235
|
+
out.map( taguri, to_yaml_style ) do |map|
|
236
|
+
map.add( 'begin', self.begin )
|
237
|
+
map.add( 'end', self.end )
|
238
|
+
map.add( 'excl', self.exclude_end? )
|
239
|
+
to_yaml_properties.each do |m|
|
240
|
+
map.add( m, instance_variable_get( m ) )
|
241
|
+
end
|
242
|
+
end
|
243
|
+
# else
|
244
|
+
# out.scalar( taguri ) do |sc|
|
245
|
+
# sc.embed( self.begin )
|
246
|
+
# sc.concat( self.exclude_end? ? "..." : ".." )
|
247
|
+
# sc.embed( self.end )
|
248
|
+
# end
|
249
|
+
# end
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
class Regexp
|
255
|
+
yaml_as "tag:ruby.yaml.org,2002:regexp"
|
256
|
+
def Regexp.yaml_new( klass, tag, val )
|
257
|
+
if String === val and val =~ /^\/(.*)\/([mix]*)$/
|
258
|
+
val = { 'regexp' => $1, 'mods' => $2 }
|
259
|
+
end
|
260
|
+
if Hash === val
|
261
|
+
mods = nil
|
262
|
+
unless val['mods'].to_s.empty?
|
263
|
+
mods = 0x00
|
264
|
+
mods |= Regexp::EXTENDED if val['mods'].include?( 'x' )
|
265
|
+
mods |= Regexp::IGNORECASE if val['mods'].include?( 'i' )
|
266
|
+
mods |= Regexp::MULTILINE if val['mods'].include?( 'm' )
|
267
|
+
end
|
268
|
+
val.delete( 'mods' )
|
269
|
+
r = YAML::object_maker( klass, {} )
|
270
|
+
Regexp.instance_method(:initialize).
|
271
|
+
bind(r).
|
272
|
+
call( val.delete( 'regexp' ), mods )
|
273
|
+
val.each { |k,v| r.instance_variable_set( k, v ) }
|
274
|
+
r
|
275
|
+
else
|
276
|
+
raise YAML::TypeError, "Invalid Regular expression: " + val.inspect
|
277
|
+
end
|
278
|
+
end
|
279
|
+
def to_yaml( opts = {} )
|
280
|
+
YAML::quick_emit( nil, opts ) do |out|
|
281
|
+
if to_yaml_properties.empty?
|
282
|
+
out.scalar( taguri, self.inspect, :plain )
|
283
|
+
else
|
284
|
+
out.map( taguri, to_yaml_style ) do |map|
|
285
|
+
src = self.inspect
|
286
|
+
if src =~ /\A\/(.*)\/([a-z]*)\Z/
|
287
|
+
map.add( 'regexp', $1 )
|
288
|
+
map.add( 'mods', $2 )
|
289
|
+
else
|
290
|
+
raise YAML::TypeError, "Invalid Regular expression: " + src
|
291
|
+
end
|
292
|
+
to_yaml_properties.each do |m|
|
293
|
+
map.add( m, instance_variable_get( m ) )
|
294
|
+
end
|
295
|
+
end
|
296
|
+
end
|
297
|
+
end
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
class Time
|
302
|
+
yaml_as "tag:ruby.yaml.org,2002:time"
|
303
|
+
yaml_as "tag:yaml.org,2002:timestamp"
|
304
|
+
def Time.yaml_new( klass, tag, val )
|
305
|
+
if Hash === val
|
306
|
+
t = val.delete( 'at' )
|
307
|
+
val.each { |k,v| t.instance_variable_set( k, v ) }
|
308
|
+
t
|
309
|
+
else
|
310
|
+
raise YAML::TypeError, "Invalid Time: " + val.inspect
|
311
|
+
end
|
312
|
+
end
|
313
|
+
def to_yaml( opts = {} )
|
314
|
+
YAML::quick_emit( self, opts ) do |out|
|
315
|
+
tz = "Z"
|
316
|
+
# from the tidy Tobias Peters <t-peters@gmx.de> Thanks!
|
317
|
+
unless self.utc?
|
318
|
+
utc_same_instant = self.dup.utc
|
319
|
+
utc_same_writing = Time.utc(year,month,day,hour,min,sec,usec)
|
320
|
+
difference_to_utc = utc_same_writing - utc_same_instant
|
321
|
+
if (difference_to_utc < 0)
|
322
|
+
difference_sign = '-'
|
323
|
+
absolute_difference = -difference_to_utc
|
324
|
+
else
|
325
|
+
difference_sign = '+'
|
326
|
+
absolute_difference = difference_to_utc
|
327
|
+
end
|
328
|
+
difference_minutes = (absolute_difference/60).round
|
329
|
+
tz = "%s%02d:%02d" % [ difference_sign, difference_minutes / 60, difference_minutes % 60]
|
330
|
+
end
|
331
|
+
standard = self.strftime( "%Y-%m-%d %H:%M:%S" )
|
332
|
+
standard += ".%06d" % [usec] if usec.nonzero?
|
333
|
+
standard += " %s" % [tz]
|
334
|
+
if to_yaml_properties.empty?
|
335
|
+
out.scalar( taguri, standard, :plain )
|
336
|
+
else
|
337
|
+
out.map( taguri, to_yaml_style ) do |map|
|
338
|
+
map.add( 'at', standard )
|
339
|
+
to_yaml_properties.each do |m|
|
340
|
+
map.add( m, instance_variable_get( m ) )
|
341
|
+
end
|
342
|
+
end
|
343
|
+
end
|
344
|
+
end
|
345
|
+
end
|
346
|
+
end
|
347
|
+
|
348
|
+
class Date
|
349
|
+
yaml_as "tag:yaml.org,2002:timestamp#ymd"
|
350
|
+
def to_yaml( opts = {} )
|
351
|
+
YAML::quick_emit( self, opts ) do |out|
|
352
|
+
out.scalar( "tag:yaml.org,2002:timestamp", self.to_s, :plain )
|
353
|
+
end
|
354
|
+
end
|
355
|
+
end
|
356
|
+
|
357
|
+
class Integer
|
358
|
+
yaml_as "tag:yaml.org,2002:int"
|
359
|
+
def to_yaml( opts = {} )
|
360
|
+
YAML::quick_emit( nil, opts ) do |out|
|
361
|
+
out.scalar( "tag:yaml.org,2002:int", self.to_s, :plain )
|
362
|
+
end
|
363
|
+
end
|
364
|
+
end
|
365
|
+
|
366
|
+
class Float
|
367
|
+
yaml_as "tag:yaml.org,2002:float"
|
368
|
+
def to_yaml( opts = {} )
|
369
|
+
YAML::quick_emit( nil, opts ) do |out|
|
370
|
+
str = self.to_s
|
371
|
+
if str == "Infinity"
|
372
|
+
str = ".Inf"
|
373
|
+
elsif str == "-Infinity"
|
374
|
+
str = "-.Inf"
|
375
|
+
elsif str == "NaN"
|
376
|
+
str = ".NaN"
|
377
|
+
end
|
378
|
+
out.scalar( "tag:yaml.org,2002:float", str, :plain )
|
379
|
+
end
|
380
|
+
end
|
381
|
+
end
|
382
|
+
|
383
|
+
class TrueClass
|
384
|
+
yaml_as "tag:yaml.org,2002:bool#yes"
|
385
|
+
def to_yaml( opts = {} )
|
386
|
+
YAML::quick_emit( nil, opts ) do |out|
|
387
|
+
out.scalar( taguri, "true", :plain )
|
388
|
+
end
|
389
|
+
end
|
390
|
+
end
|
391
|
+
|
392
|
+
class FalseClass
|
393
|
+
yaml_as "tag:yaml.org,2002:bool#no"
|
394
|
+
def to_yaml( opts = {} )
|
395
|
+
YAML::quick_emit( nil, opts ) do |out|
|
396
|
+
out.scalar( taguri, "false", :plain )
|
397
|
+
end
|
398
|
+
end
|
399
|
+
end
|
400
|
+
|
401
|
+
class NilClass
|
402
|
+
yaml_as "tag:yaml.org,2002:null"
|
403
|
+
def to_yaml( opts = {} )
|
404
|
+
YAML::quick_emit( nil, opts ) do |out|
|
405
|
+
out.scalar( taguri, "", :plain )
|
406
|
+
end
|
407
|
+
end
|
408
|
+
end
|
409
|
+
|