PageRenderTime 46ms CodeModel.GetById 20ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Languages/Ruby/libs/yaml/types.rb

#
Ruby | 192 lines | 159 code | 8 blank | 25 comment | 7 complexity | 434f5489311d8fe2284d7da30d3b3fd1 MD5 | raw file
  1# -*- mode: ruby; ruby-indent-level: 4 -*- vim: sw=4
  2#
  3# Classes required by the full core typeset
  4#
  5
  6module 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
192end