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

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