Imported File#ftype spec from rubyspecs.
[rbx.git] / lib / rbyaml / types.rb
blobfb953f491472bb219b26c3edb2dddd1bca432144
1 # -*- mode: ruby; ruby-indent-level: 4 -*- vim: sw=4
3 # Classes required by the full core typeset
5 module RbYAML
6     #
7     # Default private type
8     #
9     class PrivateType
10         def self.tag_subclasses?; false; end
11         attr_accessor :type_id, :value
12         verbose, $VERBOSE = $VERBOSE, nil
13         def initialize( type, val )
14             @type_id = type; @value = val
15             @value.taguri = "x-private:#{ @type_id }"
16         end
17         def to_yaml_node( repr )
18             @value.to_yaml_node( repr )
19         end
20     ensure
21         $VERBOSE = verbose
22     end
24     #
25     # Default domain type
26     #
27     class DomainType
28         def self.tag_subclasses?; false; end
29         attr_accessor :domain, :type_id, :value
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_node( repr )
36             @value.to_yaml_node( repr )
37         end
38     ensure
39         $VERBOSE = verbose
40     end
42     #
43     # Builtin collection: !omap
44     #
45     class Omap < ::Array
46         yaml_as "tag:yaml.org,2002:omap"
47         def yaml_initialize( tag, val )
48             if Array === val
49                 val.each do |v|
50                     if Hash === v
51                         concat( v.to_a )                # Convert the map to a sequence
52                     else
53                         raise YAML::Error, "Invalid !omap entry: " + val.inspect
54                     end
55                 end
56             else
57                 raise YAML::Error, "Invalid !omap: " + val.inspect
58             end
59             self
60         end
61         def self.[]( *vals )
63             o = Omap.new
64             0.step( vals.length - 1, 2 ) do |i|
65                 o[vals[i]] = vals[i+1]
66             end
67             o
68         end
69         def []( k )
70             self.assoc( k ).to_a[1]
71         end
72         def []=( k, *rest )
73             val, set = rest.reverse
74             if ( tmp = self.assoc( k ) ) and not set
75                 tmp[1] = val
76             else
77                 self << [ k, val ] 
78             end
79             val
80         end
81         def has_key?( k )
82             self.assoc( k ) ? true : false
83         end
84         def is_complex_yaml?
85             true
86         end
87         def to_yaml_node( repr )
88             RbYAML::quick_emit_node( self.object_id, repr ) do |out|
89                 out.seq( taguri, self.collect {|v| Hash[*v]}, to_yaml_style )
90             end
91         end
92     end
94     #
95     # Builtin collection: !pairs
96     #
97     class Pairs < ::Array
98         yaml_as "tag:yaml.org,2002:pairs"
99         def yaml_initialize( tag, val )
100             if Array === val
101                 val.each do |v|
102                     if Hash === v
103                         concat( v.to_a )                # Convert the map to a sequence
104                     else
105                         raise YAML::Error, "Invalid !pairs entry: " + val.inspect
106                     end
107                 end
108             else
109                 raise YAML::Error, "Invalid !pairs: " + val.inspect
110             end
111             self
112         end
113         def self.[]( *vals )
114             p = Pairs.new
115             0.step( vals.length - 1, 2 ) { |i|
116                 p[vals[i]] = vals[i+1]
117             }
118             p
119         end
120         def []( k )
121             self.assoc( k ).to_a
122         end
123         def []=( k, val )
124             self << [ k, val ] 
125             val
126         end
127         def has_key?( k )
128             self.assoc( k ) ? true : false
129         end
130         def is_complex_yaml?
131             true
132         end
133         def to_yaml_node( repr )
134             RbYAML::quick_emit_node( self.object_id, opts ) do |out|
135                 out.seq( taguri, self.collect {|v| Hash[*v]}, to_yaml_style )
136             end
137         end
138     end
140     #
141     # Builtin collection: !set
142     #
143     class Set < ::Hash
144         yaml_as "tag:yaml.org,2002:set"
145     end