PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

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

#
Ruby | 192 lines | 159 code | 8 blank | 25 comment | 7 complexity | 434f5489311d8fe2284d7da30d3b3fd1 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. # -*- mode: ruby; ruby-indent-level: 4 -*- vim: sw=4
  2. #
  3. # Classes required by the full core typeset
  4. #
  5. module YAML
  6. #
  7. # Default private type
  8. #
  9. class PrivateType
  10. def self.tag_subclasses?; false; end
  11. verbose, $VERBOSE = $VERBOSE, nil
  12. def initialize( type, val )
  13. @type_id = type; @value = val
  14. @value.taguri = "x-private:#{ @type_id }"
  15. end
  16. def to_yaml( opts = {} )
  17. @value.to_yaml( opts )
  18. end
  19. ensure
  20. $VERBOSE = verbose
  21. end
  22. #
  23. # Default domain type
  24. #
  25. class DomainType
  26. def self.tag_subclasses?; false; end
  27. verbose, $VERBOSE = $VERBOSE, nil
  28. def initialize( domain, type, val )
  29. @domain = domain; @type_id = type; @value = val
  30. @value.taguri = "tag:#{ @domain }:#{ @type_id }"
  31. end
  32. def to_yaml( opts = {} )
  33. @value.to_yaml( opts )
  34. end
  35. ensure
  36. $VERBOSE = verbose
  37. end
  38. #
  39. # Unresolved objects
  40. #
  41. class Object
  42. def self.tag_subclasses?; false; end
  43. def to_yaml( opts = {} )
  44. YAML::quick_emit( self, opts ) do |out|
  45. out.map( "tag:ruby.yaml.org,2002:object:#{ @class }", to_yaml_style ) do |map|
  46. @ivars.each do |k,v|
  47. map.add( k, v )
  48. end
  49. end
  50. end
  51. end
  52. end
  53. #
  54. # YAML Hash class to support comments and defaults
  55. #
  56. class SpecialHash < ::Hash
  57. attr_accessor :default
  58. def inspect
  59. self.default.to_s
  60. end
  61. def to_s
  62. self.default.to_s
  63. end
  64. def update( h )
  65. if YAML::SpecialHash === h
  66. @default = h.default if h.default
  67. end
  68. super( h )
  69. end
  70. def to_yaml( opts = {} )
  71. opts[:DefaultKey] = self.default
  72. super( opts )
  73. end
  74. end
  75. #
  76. # Builtin collection: !omap
  77. #
  78. class Omap < ::Array
  79. yaml_as "tag:yaml.org,2002:omap"
  80. def yaml_initialize( tag, val )
  81. if Array === val
  82. val.each do |v|
  83. if Hash === v
  84. concat( v.to_a ) # Convert the map to a sequence
  85. else
  86. raise YAML::Error, "Invalid !omap entry: " + val.inspect
  87. end
  88. end
  89. else
  90. raise YAML::Error, "Invalid !omap: " + val.inspect
  91. end
  92. self
  93. end
  94. def self.[]( *vals )
  95. o = Omap.new
  96. 0.step( vals.length - 1, 2 ) do |i|
  97. o[vals[i]] = vals[i+1]
  98. end
  99. o
  100. end
  101. def []( k )
  102. self.assoc( k ).to_a[1]
  103. end
  104. def []=( k, *rest )
  105. val, set = rest.reverse
  106. if ( tmp = self.assoc( k ) ) and not set
  107. tmp[1] = val
  108. else
  109. self << [ k, val ]
  110. end
  111. val
  112. end
  113. def has_key?( k )
  114. self.assoc( k ) ? true : false
  115. end
  116. def is_complex_yaml?
  117. true
  118. end
  119. def to_yaml( opts = {} )
  120. YAML::quick_emit( self, opts ) do |out|
  121. out.seq( taguri, to_yaml_style ) do |seq|
  122. self.each do |v|
  123. seq.add( Hash[ *v ] )
  124. end
  125. end
  126. end
  127. end
  128. end
  129. #
  130. # Builtin collection: !pairs
  131. #
  132. class Pairs < ::Array
  133. yaml_as "tag:yaml.org,2002:pairs"
  134. def yaml_initialize( tag, val )
  135. if Array === val
  136. val.each do |v|
  137. if Hash === v
  138. concat( v.to_a ) # Convert the map to a sequence
  139. else
  140. raise YAML::Error, "Invalid !pairs entry: " + val.inspect
  141. end
  142. end
  143. else
  144. raise YAML::Error, "Invalid !pairs: " + val.inspect
  145. end
  146. self
  147. end
  148. def self.[]( *vals )
  149. p = Pairs.new
  150. 0.step( vals.length - 1, 2 ) { |i|
  151. p[vals[i]] = vals[i+1]
  152. }
  153. p
  154. end
  155. def []( k )
  156. self.assoc( k ).to_a
  157. end
  158. def []=( k, val )
  159. self << [ k, val ]
  160. val
  161. end
  162. def has_key?( k )
  163. self.assoc( k ) ? true : false
  164. end
  165. def is_complex_yaml?
  166. true
  167. end
  168. def to_yaml( opts = {} )
  169. YAML::quick_emit( self, opts ) do |out|
  170. out.seq( taguri, to_yaml_style ) do |seq|
  171. self.each do |v|
  172. seq.add( Hash[ *v ] )
  173. end
  174. end
  175. end
  176. end
  177. end
  178. #
  179. # Builtin collection: !set
  180. #
  181. class Set < ::Hash
  182. yaml_as "tag:yaml.org,2002:set"
  183. end
  184. end