rubysl-yaml 0.0.1 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +0 -1
  3. data/.travis.yml +7 -0
  4. data/README.md +2 -2
  5. data/Rakefile +0 -1
  6. data/lib/rubysl/yaml.rb +2 -0
  7. data/lib/rubysl/yaml/version.rb +5 -0
  8. data/lib/rubysl/yaml/yaml.rb +440 -0
  9. data/lib/yaml.rb +1 -0
  10. data/lib/yaml/baseemitter.rb +247 -0
  11. data/lib/yaml/basenode.rb +216 -0
  12. data/lib/yaml/constants.rb +45 -0
  13. data/lib/yaml/dbm.rb +111 -0
  14. data/lib/yaml/encoding.rb +33 -0
  15. data/lib/yaml/error.rb +34 -0
  16. data/lib/yaml/loader.rb +14 -0
  17. data/lib/yaml/rubytypes.rb +409 -0
  18. data/lib/yaml/store.rb +43 -0
  19. data/lib/yaml/stream.rb +40 -0
  20. data/lib/yaml/stringio.rb +83 -0
  21. data/lib/yaml/syck.rb +44 -0
  22. data/lib/yaml/tag.rb +91 -0
  23. data/lib/yaml/types.rb +192 -0
  24. data/lib/yaml/yamlnode.rb +54 -0
  25. data/lib/yaml/ypath.rb +52 -0
  26. data/rubysl-yaml.gemspec +19 -17
  27. data/spec/add_builtin_type_spec.rb +1 -0
  28. data/spec/add_domain_type_spec.rb +1 -0
  29. data/spec/add_private_type_spec.rb +1 -0
  30. data/spec/add_ruby_type_spec.rb +1 -0
  31. data/spec/detect_implicit_spec.rb +1 -0
  32. data/spec/dump_spec.rb +35 -0
  33. data/spec/dump_stream_spec.rb +13 -0
  34. data/spec/each_document_spec.rb +9 -0
  35. data/spec/each_node_spec.rb +1 -0
  36. data/spec/emitter_spec.rb +1 -0
  37. data/spec/fixtures/common.rb +10 -0
  38. data/spec/fixtures/example_class.rb +5 -0
  39. data/spec/fixtures/strings.rb +36 -0
  40. data/spec/fixtures/test_yaml.yml +2 -0
  41. data/spec/generic_parser_spec.rb +1 -0
  42. data/spec/load_documents_spec.rb +7 -0
  43. data/spec/load_file_spec.rb +12 -0
  44. data/spec/load_spec.rb +111 -0
  45. data/spec/load_stream_spec.rb +1 -0
  46. data/spec/object_maker_spec.rb +1 -0
  47. data/spec/parse_documents_spec.rb +1 -0
  48. data/spec/parse_file_spec.rb +9 -0
  49. data/spec/parse_spec.rb +21 -0
  50. data/spec/parser_spec.rb +1 -0
  51. data/spec/quick_emit_spec.rb +1 -0
  52. data/spec/read_type_class_spec.rb +1 -0
  53. data/spec/shared/each_document.rb +18 -0
  54. data/spec/tag_class_spec.rb +9 -0
  55. data/spec/tagged_classes_spec.rb +9 -0
  56. data/spec/tagurize_spec.rb +8 -0
  57. data/spec/to_yaml_spec.rb +97 -0
  58. data/spec/transfer_spec.rb +1 -0
  59. data/spec/try_implicit_spec.rb +1 -0
  60. metadata +163 -88
  61. data/lib/rubysl-yaml.rb +0 -7
  62. data/lib/rubysl-yaml/version.rb +0 -5
data/lib/yaml/store.rb ADDED
@@ -0,0 +1,43 @@
1
+ #
2
+ # YAML::Store
3
+ #
4
+ require 'yaml'
5
+ require 'pstore'
6
+
7
+ class YAML::Store < PStore
8
+ def initialize( *o )
9
+ @opt = YAML::DEFAULTS.dup
10
+ if String === o.first
11
+ super(o.shift)
12
+ end
13
+ if o.last.is_a? Hash
14
+ @opt.update(o.pop)
15
+ end
16
+ end
17
+
18
+ def dump(table)
19
+ @table.to_yaml(@opt)
20
+ end
21
+
22
+ def load(content)
23
+ table = YAML::load(content)
24
+ if table == false
25
+ {}
26
+ else
27
+ table
28
+ end
29
+ end
30
+
31
+ def marshal_dump_supports_canonical_option?
32
+ false
33
+ end
34
+
35
+ EMPTY_MARSHAL_DATA = {}.to_yaml
36
+ EMPTY_MARSHAL_CHECKSUM = Digest::MD5.digest(EMPTY_MARSHAL_DATA)
37
+ def empty_marshal_data
38
+ EMPTY_MARSHAL_DATA
39
+ end
40
+ def empty_marshal_checksum
41
+ EMPTY_MARSHAL_CHECKSUM
42
+ end
43
+ end
@@ -0,0 +1,40 @@
1
+ module YAML
2
+
3
+ #
4
+ # YAML::Stream -- for emitting many documents
5
+ #
6
+ class Stream
7
+
8
+ attr_accessor :documents, :options
9
+
10
+ def initialize( opts = {} )
11
+ @options = opts
12
+ @documents = []
13
+ end
14
+
15
+ def []( i )
16
+ @documents[ i ]
17
+ end
18
+
19
+ def add( doc )
20
+ @documents << doc
21
+ end
22
+
23
+ def edit( doc_num, doc )
24
+ @documents[ doc_num ] = doc
25
+ end
26
+
27
+ def emit( io = nil )
28
+ # opts = @options.dup
29
+ # opts[:UseHeader] = true if @documents.length > 1
30
+ out = YAML.emitter
31
+ out.reset( io || io2 = StringIO.new )
32
+ @documents.each { |v|
33
+ v.to_yaml( out )
34
+ }
35
+ io || ( io2.rewind; io2.read )
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,83 @@
1
+ #
2
+ # Limited StringIO if no core lib is available
3
+ #
4
+ begin
5
+ require 'stringio'
6
+ rescue LoadError
7
+ # StringIO based on code by MoonWolf
8
+ class StringIO
9
+ def initialize(string="")
10
+ @string=string
11
+ @pos=0
12
+ @eof=(string.size==0)
13
+ end
14
+ def pos
15
+ @pos
16
+ end
17
+ def eof
18
+ @eof
19
+ end
20
+ alias eof? eof
21
+ def readline(rs=$/)
22
+ if @eof
23
+ raise EOFError
24
+ else
25
+ if p = @string[@pos..-1]=~rs
26
+ line = @string[@pos,p+1]
27
+ else
28
+ line = @string[@pos..-1]
29
+ end
30
+ @pos+=line.size
31
+ @eof =true if @pos==@string.size
32
+ $_ = line
33
+ end
34
+ end
35
+ def rewind
36
+ seek(0,0)
37
+ end
38
+ def seek(offset,whence)
39
+ case whence
40
+ when 0
41
+ @pos=offset
42
+ when 1
43
+ @pos+=offset
44
+ when 2
45
+ @pos=@string.size+offset
46
+ end
47
+ @eof=(@pos>=@string.size)
48
+ 0
49
+ end
50
+ end
51
+
52
+ #
53
+ # Class method for creating streams
54
+ #
55
+ def YAML.make_stream( io )
56
+ if String === io
57
+ io = StringIO.new( io )
58
+ elsif not IO === io
59
+ raise YAML::Error, "YAML stream must be an IO or String object."
60
+ end
61
+ if YAML::unicode
62
+ def io.readline
63
+ YAML.utf_to_internal( readline( @ln_sep ), @utf_encoding )
64
+ end
65
+ def io.check_unicode
66
+ @utf_encoding = YAML.sniff_encoding( read( 4 ) )
67
+ @ln_sep = YAML.enc_separator( @utf_encoding )
68
+ seek( -4, IO::SEEK_CUR )
69
+ end
70
+ def io.utf_encoding
71
+ @utf_encoding
72
+ end
73
+ io.check_unicode
74
+ else
75
+ def io.utf_encoding
76
+ :None
77
+ end
78
+ end
79
+ io
80
+ end
81
+
82
+ end
83
+
data/lib/yaml/syck.rb ADDED
@@ -0,0 +1,44 @@
1
+ #
2
+ # YAML::Syck module
3
+ # .. glues syck and yaml.rb together ..
4
+ #
5
+ require 'syck/syck'
6
+ require 'yaml/basenode'
7
+
8
+ module YAML
9
+ module Syck
10
+
11
+ #
12
+ # Mixin BaseNode functionality
13
+ #
14
+ class Node
15
+ include YAML::BaseNode
16
+ end
17
+
18
+ #--
19
+ # For Rubinius, replaces the rb_iterate call to syck_set_ivars.
20
+ #++
21
+ def self.set_ivars(hsh, obj)
22
+ hsh.each do |key, value|
23
+ obj.instance_variable_set :"@#{key}", value
24
+ end
25
+ end
26
+
27
+ #--
28
+ # For Rubinius, replaces the rb_iterate call to syck_merge_i.
29
+ #++
30
+ def self.merge_i(ary, hsh)
31
+ ary.each do |entry|
32
+ begin
33
+ entry = Rubinius::Type.coerce_to entry, Hash, :to_hash
34
+ hsh.update entry
35
+ rescue
36
+ # ignore coercion errors
37
+ end
38
+ end
39
+
40
+ nil
41
+ end
42
+
43
+ end
44
+ end
data/lib/yaml/tag.rb ADDED
@@ -0,0 +1,91 @@
1
+ # -*- mode: ruby; ruby-indent-level: 4; tab-width: 4 -*- vim: sw=4 ts=4
2
+ # $Id: tag.rb 11708 2007-02-12 23:01:19Z shyouhei $
3
+ #
4
+ # = yaml/tag.rb: methods for associating a taguri to a class.
5
+ #
6
+ # Author:: why the lucky stiff
7
+ #
8
+ module YAML
9
+ # A dictionary of taguris which map to
10
+ # Ruby classes.
11
+ @@tagged_classes = {}
12
+
13
+ #
14
+ # Associates a taguri _tag_ with a Ruby class _cls_. The taguri is used to give types
15
+ # to classes when loading YAML. Taguris are of the form:
16
+ #
17
+ # tag:authorityName,date:specific
18
+ #
19
+ # The +authorityName+ is a domain name or email address. The +date+ is the date the type
20
+ # was issued in YYYY or YYYY-MM or YYYY-MM-DD format. The +specific+ is a name for
21
+ # the type being added.
22
+ #
23
+ # For example, built-in YAML types have 'yaml.org' as the +authorityName+ and '2002' as the
24
+ # +date+. The +specific+ is simply the name of the type:
25
+ #
26
+ # tag:yaml.org,2002:int
27
+ # tag:yaml.org,2002:float
28
+ # tag:yaml.org,2002:timestamp
29
+ #
30
+ # The domain must be owned by you on the +date+ declared. If you don't own any domains on the
31
+ # date you declare the type, you can simply use an e-mail address.
32
+ #
33
+ # tag:why@ruby-lang.org,2004:notes/personal
34
+ #
35
+ def YAML.tag_class( tag, cls )
36
+ if @@tagged_classes.has_key? tag
37
+ warn "class #{ @@tagged_classes[tag] } held ownership of the #{ tag } tag"
38
+ end
39
+ @@tagged_classes[tag] = cls
40
+ end
41
+
42
+ # Returns the complete dictionary of taguris, paired with classes. The key for
43
+ # the dictionary is the full taguri. The value for each key is the class constant
44
+ # associated to that taguri.
45
+ #
46
+ # YAML.tagged_classes["tag:yaml.org,2002:int"] => Integer
47
+ #
48
+ def YAML.tagged_classes
49
+ @@tagged_classes
50
+ end
51
+ end
52
+
53
+ class Module
54
+ # :stopdoc:
55
+
56
+ # Adds a taguri _tag_ to a class, used when dumping or loading the class
57
+ # in YAML. See YAML::tag_class for detailed information on typing and
58
+ # taguris.
59
+ def yaml_as( tag, sc = true )
60
+ verbose, $VERBOSE = $VERBOSE, nil
61
+ class_eval <<-"end;", __FILE__, __LINE__+1
62
+ attr_writer :taguri
63
+ def taguri
64
+ if respond_to? :to_yaml_type
65
+ YAML::tagurize( to_yaml_type[1..-1] )
66
+ else
67
+ return @taguri if defined?(@taguri) and @taguri
68
+ tag = #{ tag.dump }
69
+ if self.class.yaml_tag_subclasses? and self.class != YAML::tagged_classes[tag]
70
+ tag = "\#{ tag }:\#{ self.class.yaml_tag_class_name }"
71
+ end
72
+ tag
73
+ end
74
+ end
75
+ def self.yaml_tag_subclasses?; #{ sc ? 'true' : 'false' }; end
76
+ end;
77
+ YAML::tag_class tag, self
78
+ ensure
79
+ $VERBOSE = verbose
80
+ end
81
+ # Transforms the subclass name into a name suitable for display
82
+ # in a subclassed tag.
83
+ def yaml_tag_class_name
84
+ self.name
85
+ end
86
+ # Transforms the subclass name found in the tag into a Ruby
87
+ # constant name.
88
+ def yaml_tag_read_class( name )
89
+ name
90
+ end
91
+ end
data/lib/yaml/types.rb ADDED
@@ -0,0 +1,192 @@
1
+ # -*- mode: ruby; ruby-indent-level: 4 -*- vim: sw=4
2
+ #
3
+ # Classes required by the full core typeset
4
+ #
5
+
6
+ module YAML
7
+
8
+ #
9
+ # Default private type
10
+ #
11
+ class PrivateType
12
+ def self.tag_subclasses?; false; end
13
+ verbose, $VERBOSE = $VERBOSE, nil
14
+ def initialize( type, val )
15
+ @type_id = type; @value = val
16
+ @value.taguri = "x-private:#{ @type_id }"
17
+ end
18
+ def to_yaml( opts = {} )
19
+ @value.to_yaml( opts )
20
+ end
21
+ ensure
22
+ $VERBOSE = verbose
23
+ end
24
+
25
+ #
26
+ # Default domain type
27
+ #
28
+ class DomainType
29
+ def self.tag_subclasses?; false; end
30
+ verbose, $VERBOSE = $VERBOSE, nil
31
+ def initialize( domain, type, val )
32
+ @domain = domain; @type_id = type; @value = val
33
+ @value.taguri = "tag:#{ @domain }:#{ @type_id }"
34
+ end
35
+ def to_yaml( opts = {} )
36
+ @value.to_yaml( opts )
37
+ end
38
+ ensure
39
+ $VERBOSE = verbose
40
+ end
41
+
42
+ #
43
+ # Unresolved objects
44
+ #
45
+ class Object
46
+ def self.tag_subclasses?; false; end
47
+ def to_yaml( opts = {} )
48
+ YAML::quick_emit( self, opts ) do |out|
49
+ out.map( "tag:ruby.yaml.org,2002:object:#{ @class }", to_yaml_style ) do |map|
50
+ @ivars.each do |k,v|
51
+ map.add( k, v )
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ #
59
+ # YAML Hash class to support comments and defaults
60
+ #
61
+ class SpecialHash < ::Hash
62
+ attr_accessor :default
63
+ def inspect
64
+ self.default.to_s
65
+ end
66
+ def to_s
67
+ self.default.to_s
68
+ end
69
+ def update( h )
70
+ if YAML::SpecialHash === h
71
+ @default = h.default if h.default
72
+ end
73
+ super( h )
74
+ end
75
+ def to_yaml( opts = {} )
76
+ opts[:DefaultKey] = self.default
77
+ super( opts )
78
+ end
79
+ end
80
+
81
+ #
82
+ # Builtin collection: !omap
83
+ #
84
+ class Omap < ::Array
85
+ yaml_as "tag:yaml.org,2002:omap"
86
+ def yaml_initialize( tag, val )
87
+ if Array === val
88
+ val.each do |v|
89
+ if Hash === v
90
+ concat( v.to_a ) # Convert the map to a sequence
91
+ else
92
+ raise YAML::Error, "Invalid !omap entry: " + val.inspect
93
+ end
94
+ end
95
+ else
96
+ raise YAML::Error, "Invalid !omap: " + val.inspect
97
+ end
98
+ self
99
+ end
100
+ def self.[]( *vals )
101
+ o = Omap.new
102
+ 0.step( vals.length - 1, 2 ) do |i|
103
+ o[vals[i]] = vals[i+1]
104
+ end
105
+ o
106
+ end
107
+ def []( k )
108
+ self.assoc( k ).to_a[1]
109
+ end
110
+ def []=( k, *rest )
111
+ val, set = rest.reverse
112
+ if ( tmp = self.assoc( k ) ) and not set
113
+ tmp[1] = val
114
+ else
115
+ self << [ k, val ]
116
+ end
117
+ val
118
+ end
119
+ def has_key?( k )
120
+ self.assoc( k ) ? true : false
121
+ end
122
+ def is_complex_yaml?
123
+ true
124
+ end
125
+ def to_yaml( opts = {} )
126
+ YAML::quick_emit( self, opts ) do |out|
127
+ out.seq( taguri, to_yaml_style ) do |seq|
128
+ self.each do |v|
129
+ seq.add( Hash[ *v ] )
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
135
+
136
+ #
137
+ # Builtin collection: !pairs
138
+ #
139
+ class Pairs < ::Array
140
+ yaml_as "tag:yaml.org,2002:pairs"
141
+ def yaml_initialize( tag, val )
142
+ if Array === val
143
+ val.each do |v|
144
+ if Hash === v
145
+ concat( v.to_a ) # Convert the map to a sequence
146
+ else
147
+ raise YAML::Error, "Invalid !pairs entry: " + val.inspect
148
+ end
149
+ end
150
+ else
151
+ raise YAML::Error, "Invalid !pairs: " + val.inspect
152
+ end
153
+ self
154
+ end
155
+ def self.[]( *vals )
156
+ p = Pairs.new
157
+ 0.step( vals.length - 1, 2 ) { |i|
158
+ p[vals[i]] = vals[i+1]
159
+ }
160
+ p
161
+ end
162
+ def []( k )
163
+ self.assoc( k ).to_a
164
+ end
165
+ def []=( k, val )
166
+ self << [ k, val ]
167
+ val
168
+ end
169
+ def has_key?( k )
170
+ self.assoc( k ) ? true : false
171
+ end
172
+ def is_complex_yaml?
173
+ true
174
+ end
175
+ def to_yaml( opts = {} )
176
+ YAML::quick_emit( self, opts ) do |out|
177
+ out.seq( taguri, to_yaml_style ) do |seq|
178
+ self.each do |v|
179
+ seq.add( Hash[ *v ] )
180
+ end
181
+ end
182
+ end
183
+ end
184
+ end
185
+
186
+ #
187
+ # Builtin collection: !set
188
+ #
189
+ class Set < ::Hash
190
+ yaml_as "tag:yaml.org,2002:set"
191
+ end
192
+ end