/tools/Ruby/lib/ruby/1.8/xsd/codegen/moduledef.rb

http://github.com/agross/netopenspace · Ruby · 191 lines · 154 code · 31 blank · 6 comment · 12 complexity · 6c64a282a7412a1fb269980dd91ce250 MD5 · raw file

  1. # XSD4R - Generating module definition code
  2. # Copyright (C) 2004 NAKAMURA, Hiroshi <nahi@ruby-lang.org>.
  3. # This program is copyrighted free software by NAKAMURA, Hiroshi. You can
  4. # redistribute it and/or modify it under the same terms of Ruby's license;
  5. # either the dual license version in 2003, or any later version.
  6. require 'xsd/codegen/gensupport'
  7. require 'xsd/codegen/methoddef'
  8. require 'xsd/codegen/commentdef'
  9. module XSD
  10. module CodeGen
  11. class ModuleDef
  12. include GenSupport
  13. include CommentDef
  14. def initialize(name)
  15. @name = name
  16. @comment = nil
  17. @const = []
  18. @code = []
  19. @requirepath = []
  20. @methoddef = []
  21. end
  22. def def_require(path)
  23. @requirepath << path
  24. end
  25. def def_const(const, value)
  26. unless safeconstname?(const)
  27. raise ArgumentError.new("#{const} seems to be unsafe")
  28. end
  29. @const << [const, value]
  30. end
  31. def def_code(code)
  32. @code << code
  33. end
  34. def def_method(name, *params)
  35. add_method(MethodDef.new(name, *params) { yield if block_given? }, :public)
  36. end
  37. alias def_publicmethod def_method
  38. def def_protectedmethod(name, *params)
  39. add_method(MethodDef.new(name, *params) { yield if block_given? },
  40. :protected)
  41. end
  42. def def_privatemethod(name, *params)
  43. add_method(MethodDef.new(name, *params) { yield if block_given? }, :private)
  44. end
  45. def add_method(m, visibility = :public)
  46. @methoddef << [visibility, m]
  47. end
  48. def dump
  49. buf = ""
  50. unless @requirepath.empty?
  51. buf << dump_requirepath
  52. end
  53. buf << dump_emptyline unless buf.empty?
  54. package = @name.split(/::/)[0..-2]
  55. buf << dump_package_def(package) unless package.empty?
  56. buf << dump_comment if @comment
  57. buf << dump_module_def
  58. spacer = false
  59. unless @const.empty?
  60. buf << dump_emptyline if spacer
  61. spacer = true
  62. buf << dump_const
  63. end
  64. unless @code.empty?
  65. buf << dump_emptyline if spacer
  66. spacer = true
  67. buf << dump_code
  68. end
  69. unless @methoddef.empty?
  70. buf << dump_emptyline if spacer
  71. spacer = true
  72. buf << dump_methods
  73. end
  74. buf << dump_module_def_end
  75. buf << dump_package_def_end(package) unless package.empty?
  76. buf.gsub(/^\s+$/, '')
  77. end
  78. private
  79. def dump_requirepath
  80. format(
  81. @requirepath.collect { |path|
  82. %Q(require '#{path}')
  83. }.join("\n")
  84. )
  85. end
  86. def dump_const
  87. dump_static(
  88. @const.sort.collect { |var, value|
  89. %Q(#{var} = #{dump_value(value)})
  90. }.join("\n")
  91. )
  92. end
  93. def dump_code
  94. dump_static(@code.join("\n"))
  95. end
  96. def dump_static(str)
  97. format(str, 2)
  98. end
  99. def dump_methods
  100. methods = {}
  101. @methoddef.each do |visibility, method|
  102. (methods[visibility] ||= []) << method
  103. end
  104. str = ""
  105. [:public, :protected, :private].each do |visibility|
  106. if methods[visibility]
  107. str << "\n" unless str.empty?
  108. str << visibility.to_s << "\n\n" unless visibility == :public
  109. str << methods[visibility].collect { |m| format(m.dump, 2) }.join("\n")
  110. end
  111. end
  112. str
  113. end
  114. def dump_value(value)
  115. if value.respond_to?(:to_src)
  116. value.to_src
  117. else
  118. value
  119. end
  120. end
  121. def dump_package_def(package)
  122. format(package.collect { |ele| "module #{ele}" }.join("; ")) + "\n\n"
  123. end
  124. def dump_package_def_end(package)
  125. "\n\n" + format(package.collect { |ele| "end" }.join("; "))
  126. end
  127. def dump_module_def
  128. name = @name.to_s.split(/::/)
  129. format("module #{name.last}")
  130. end
  131. def dump_module_def_end
  132. format("end")
  133. end
  134. end
  135. end
  136. end
  137. if __FILE__ == $0
  138. require 'xsd/codegen/moduledef'
  139. include XSD::CodeGen
  140. m = ModuleDef.new("Foo::Bar::HobbitName")
  141. m.def_require("foo/bar")
  142. m.def_require("baz")
  143. m.comment = <<-EOD
  144. foo
  145. bar
  146. baz
  147. EOD
  148. m.def_method("foo") do
  149. <<-EOD
  150. foo.bar = 1
  151. baz.each do |ele|
  152. ele + 1
  153. end
  154. EOD
  155. end
  156. m.def_method("baz", "qux")
  157. #m.def_protectedmethod("aaa")
  158. m.def_privatemethod("bbb")
  159. puts m.dump
  160. end