rubysl-yaml 2.0.0 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,41 @@
1
+ module Syck
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
+ warn "#{caller[0]}: edit is deprecated" if $VERBOSE
25
+ @documents[ doc_num ] = doc
26
+ end
27
+
28
+ def emit( io = nil )
29
+ # opts = @options.dup
30
+ # opts[:UseHeader] = true if @documents.length > 1
31
+ out = Syck.emitter
32
+ out.reset( io || io2 = StringIO.new )
33
+ @documents.each { |v|
34
+ v.to_yaml( out )
35
+ }
36
+ io || ( io2.rewind; io2.read )
37
+ end
38
+
39
+ end
40
+
41
+ end
data/lib/syck/tag.rb ADDED
@@ -0,0 +1,95 @@
1
+ # -*- mode: ruby; ruby-indent-level: 4; tab-width: 4 -*- vim: sw=4 ts=4
2
+ # $Id: tag.rb 27365 2010-04-16 20:31:59Z tenderlove $
3
+ #
4
+ # = yaml/tag.rb: methods for associating a taguri to a class.
5
+ #
6
+ # Author:: why the lucky stiff
7
+ #
8
+ module Syck
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 self.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 self.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 syck_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
+ Syck.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 != Syck.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
+ Syck.tag_class tag, self
78
+ ensure
79
+ $VERBOSE = verbose
80
+ end
81
+ remove_method :yaml_as rescue nil
82
+ alias :yaml_as :syck_yaml_as
83
+
84
+ # Transforms the subclass name into a name suitable for display
85
+ # in a subclassed tag.
86
+ def yaml_tag_class_name
87
+ self.name
88
+ end
89
+ # Transforms the subclass name found in the tag into a Ruby
90
+ # constant name.
91
+ def yaml_tag_read_class( name )
92
+ name
93
+ end
94
+ # :startdoc:
95
+ end
data/lib/syck/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 Syck
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
+ Syck.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 Syck::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 Syck::Error, "Invalid !omap entry: " + val.inspect
93
+ end
94
+ end
95
+ else
96
+ raise Syck::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
+ Syck.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 Syck::Error, "Invalid !pairs entry: " + val.inspect
148
+ end
149
+ end
150
+ else
151
+ raise Syck::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
+ Syck.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
@@ -0,0 +1,54 @@
1
+ #
2
+ # YAML::YamlNode class
3
+ #
4
+ require 'syck/basenode'
5
+
6
+ module Syck
7
+
8
+ #
9
+ # YAML Generic Model container
10
+ #
11
+ class YamlNode
12
+ include BaseNode
13
+ attr_accessor :kind, :type_id, :value, :anchor
14
+ def initialize(t, v)
15
+ @type_id = t
16
+ if Hash === v
17
+ @kind = 'map'
18
+ @value = {}
19
+ v.each {|key,val|
20
+ @value[key.transform] = [key, val]
21
+ }
22
+ elsif Array === v
23
+ @kind = 'seq'
24
+ @value = v
25
+ elsif String === v
26
+ @kind = 'scalar'
27
+ @value = v
28
+ end
29
+ end
30
+
31
+ #
32
+ # Transform this node fully into a native type
33
+ #
34
+ def transform
35
+ t = nil
36
+ if @value.is_a? Hash
37
+ t = {}
38
+ @value.each { |k,v|
39
+ t[ k ] = v[1].transform
40
+ }
41
+ elsif @value.is_a? Array
42
+ t = []
43
+ @value.each { |v|
44
+ t.push v.transform
45
+ }
46
+ else
47
+ t = @value
48
+ end
49
+ Syck.transfer_method( @type_id, t )
50
+ end
51
+
52
+ end
53
+
54
+ end
data/lib/syck/ypath.rb ADDED
@@ -0,0 +1,54 @@
1
+ #
2
+ # YAML::YPath
3
+ #
4
+
5
+ warn "#{caller[0]}: YAML::YPath is deprecated" if $VERBOSE
6
+
7
+ module Syck
8
+
9
+ class YPath
10
+ attr_accessor :segments, :predicates, :flags
11
+ def initialize( str )
12
+ @segments = []
13
+ @predicates = []
14
+ @flags = nil
15
+ while str =~ /^\/?(\/|[^\/\[]+)(?:\[([^\]]+)\])?/
16
+ @segments.push $1
17
+ @predicates.push $2
18
+ str = $'
19
+ end
20
+ unless str.to_s.empty?
21
+ @segments += str.split( "/" )
22
+ end
23
+ if @segments.length == 0
24
+ @segments.push "."
25
+ end
26
+ end
27
+ def self.each_path( str )
28
+ #
29
+ # Find choices
30
+ #
31
+ paths = []
32
+ str = "(#{ str })"
33
+ while str.sub!( /\(([^()]+)\)/, "\n#{ paths.length }\n" )
34
+ paths.push $1.split( '|' )
35
+ end
36
+
37
+ #
38
+ # Construct all possible paths
39
+ #
40
+ all = [ str ]
41
+ ( paths.length - 1 ).downto( 0 ) do |i|
42
+ all = all.collect do |a|
43
+ paths[i].collect do |p|
44
+ a.gsub( /\n#{ i }\n/, p )
45
+ end
46
+ end.flatten.uniq
47
+ end
48
+ all.collect do |path|
49
+ yield YPath.new( path )
50
+ end
51
+ end
52
+ end
53
+
54
+ end
data/lib/yaml/dbm.rb CHANGED
@@ -15,34 +15,43 @@ module YAML
15
15
  #
16
16
  # See the documentation for ::DBM and ::YAML for more information.
17
17
  class DBM < ::DBM
18
- VERSION = "0.1"
18
+ VERSION = "0.1" # :nodoc:
19
19
 
20
+ # :call-seq:
21
+ # ydbm[key] -> value
22
+ #
20
23
  # Return value associated with +key+ from database.
21
24
  #
22
25
  # Returns +nil+ if there is no such +key+.
26
+ #
27
+ # See #fetch for more information.
23
28
  def []( key )
24
29
  fetch( key )
25
30
  end
26
31
 
27
32
  # :call-seq:
28
- # []=( key, value )
33
+ # ydbm[key] = value
29
34
  #
30
35
  # Set +key+ to +value+ in database.
31
36
  #
32
37
  # +value+ will be converted to YAML before storage.
38
+ #
39
+ # See #store for more information.
33
40
  def []=( key, val )
34
41
  store( key, val )
35
42
  end
36
43
 
37
44
  # :call-seq:
38
- # fetch( key, ifnone = nil )
39
- # fetch( key, &block )
45
+ # ydbm.fetch( key, ifnone = nil )
46
+ # ydbm.fetch( key ) { |key| ... }
40
47
  #
41
48
  # Return value associated with +key+.
42
49
  #
43
50
  # If there is no value for +key+ and no block is given, returns +ifnone+.
44
51
  #
45
52
  # Otherwise, calls block passing in the given +key+.
53
+ #
54
+ # See ::DBM#fetch for more information.
46
55
  def fetch( keystr, ifnone = nil )
47
56
  begin
48
57
  val = super( keystr )
@@ -57,15 +66,35 @@ class DBM < ::DBM
57
66
  end
58
67
 
59
68
  # Deprecated, used YAML::DBM#key instead.
69
+ # ----
70
+ # Note:
71
+ # YAML::DBM#index makes warning from internal of ::DBM#index.
72
+ # It says 'DBM#index is deprecated; use DBM#key', but DBM#key
73
+ # behaves not same as DBM#index.
74
+ #
60
75
  def index( keystr )
61
76
  super( keystr.to_yaml )
62
77
  end
63
78
 
79
+ # :call-seq:
80
+ # ydbm.key(value) -> string
81
+ #
82
+ # Returns the key for the specified value.
83
+ def key( keystr )
84
+ invert[keystr]
85
+ end
86
+
87
+ # :call-seq:
88
+ # ydbm.values_at(*keys)
89
+ #
64
90
  # Returns an array containing the values associated with the given keys.
65
91
  def values_at( *keys )
66
92
  keys.collect { |k| fetch( k ) }
67
93
  end
68
94
 
95
+ # :call-seq:
96
+ # ydbm.delete(key)
97
+ #
69
98
  # Deletes value from database associated with +key+.
70
99
  #
71
100
  # Returns value or +nil+.
@@ -77,6 +106,9 @@ class DBM < ::DBM
77
106
  v
78
107
  end
79
108
 
109
+ # :call-seq:
110
+ # ydbm.delete_if { |key, value| ... }
111
+ #
80
112
  # Calls the given block once for each +key+, +value+ pair in the database.
81
113
  # Deletes all entries for which the block returns true.
82
114
  #
@@ -88,6 +120,9 @@ class DBM < ::DBM
88
120
  self
89
121
  end
90
122
 
123
+ # :call-seq:
124
+ # ydbm.reject { |key, value| ... }
125
+ #
91
126
  # Converts the contents of the database to an in-memory Hash, then calls
92
127
  # Hash#reject with the specified code block, returning a new Hash.
93
128
  def reject
@@ -95,6 +130,9 @@ class DBM < ::DBM
95
130
  hsh.reject { |k,v| yield k, v }
96
131
  end
97
132
 
133
+ # :call-seq:
134
+ # ydbm.each_pair { |key, value| ... }
135
+ #
98
136
  # Calls the given block once for each +key+, +value+ pair in the database.
99
137
  #
100
138
  # Returns +self+.
@@ -103,6 +141,9 @@ class DBM < ::DBM
103
141
  self
104
142
  end
105
143
 
144
+ # :call-seq:
145
+ # ydbm.each_value { |value| ... }
146
+ #
106
147
  # Calls the given block for each value in database.
107
148
  #
108
149
  # Returns +self+.
@@ -111,17 +152,26 @@ class DBM < ::DBM
111
152
  self
112
153
  end
113
154
 
155
+ # :call-seq:
156
+ # ydbm.values
157
+ #
114
158
  # Returns an array of values from the database.
115
159
  def values
116
160
  super.collect { |v| YAML.load( v ) }
117
161
  end
118
162
 
119
- # Returns true if specified value is found in the database.
163
+ # :call-seq:
164
+ # ydbm.has_value?(value)
165
+ #
166
+ # Returns true if specified +value+ is found in the database.
120
167
  def has_value?( val )
121
168
  each_value { |v| return true if v == val }
122
169
  return false
123
170
  end
124
171
 
172
+ # :call-seq:
173
+ # ydbm.invert -> hash
174
+ #
125
175
  # Returns a Hash (not a DBM database) created by using each value in the
126
176
  # database as a key, with the corresponding key as its value.
127
177
  #
@@ -133,6 +183,9 @@ class DBM < ::DBM
133
183
  h
134
184
  end
135
185
 
186
+ # :call-seq:
187
+ # ydbm.replace(hash) -> ydbm
188
+ #
136
189
  # Replaces the contents of the database with the contents of the specified
137
190
  # object. Takes any object which implements the each_pair method, including
138
191
  # Hash and DBM objects.
@@ -141,6 +194,9 @@ class DBM < ::DBM
141
194
  update( hsh )
142
195
  end
143
196
 
197
+ # :call-seq:
198
+ # ydbm.shift -> [key, value]
199
+ #
144
200
  # Removes a [key, value] pair from the database, and returns it.
145
201
  # If the database is empty, returns +nil+.
146
202
  #
@@ -152,8 +208,8 @@ class DBM < ::DBM
152
208
  end
153
209
 
154
210
  # :call-seq:
155
- # select( &block )
156
- # select( *keys )
211
+ # ydbm.select { |key, value| ... }
212
+ # ydbm.select(*keys)
157
213
  #
158
214
  # If a block is provided, returns a new array containing [key, value] pairs
159
215
  # for which the block returns true.
@@ -168,29 +224,35 @@ class DBM < ::DBM
168
224
  end
169
225
 
170
226
  # :call-seq:
171
- # store( key, value )
227
+ # ydbm.store(key, value) -> value
172
228
  #
173
- #Stores +value+ in database with +key+ as the index. +value+ is converted
174
- #to YAML before being stored.
229
+ # Stores +value+ in database with +key+ as the index. +value+ is converted
230
+ # to YAML before being stored.
175
231
  #
176
- #Returns +value+
232
+ # Returns +value+
177
233
  def store( key, val )
178
234
  super( key, val.to_yaml )
179
235
  val
180
236
  end
181
237
 
238
+ # :call-seq:
239
+ # ydbm.update(hash) -> ydbm
240
+ #
182
241
  # Updates the database with multiple values from the specified object.
183
242
  # Takes any object which implements the each_pair method, including
184
243
  # Hash and DBM objects.
185
244
  #
186
245
  # Returns +self+.
187
246
  def update( hsh )
188
- hsh.keys.each do |k|
189
- self.store( k, hsh.fetch( k ) )
247
+ hsh.each_pair do |k,v|
248
+ self.store( k, v )
190
249
  end
191
250
  self
192
251
  end
193
252
 
253
+ # :call-seq:
254
+ # ydbm.to_a -> array
255
+ #
194
256
  # Converts the contents of the database to an array of [key, value] arrays,
195
257
  # and returns it.
196
258
  def to_a
@@ -200,6 +262,9 @@ class DBM < ::DBM
200
262
  end
201
263
 
202
264
 
265
+ # :call-seq:
266
+ # ydbm.to_hash -> hash
267
+ #
203
268
  # Converts the contents of the database to an in-memory Hash object, and
204
269
  # returns it.
205
270
  def to_hash