class Protobuf::Visitor::CreateMessageVisitor
def attach_proto?
def attach_proto? @attach_proto end
def close_ruby
def close_ruby while 0 < indent decrement write('end') end end
def commented_proto_contents
def commented_proto_contents if proto_file proto_filepath = if File.exist?(proto_file) then proto_file else "#{@proto_dir}/#{proto_file}" end File.read(proto_filepath).gsub(/^/, '# ') end end
def create_files(filename, out_dir, file_create)
def create_files(filename, out_dir, file_create) $: << File.expand_path(out_dir) Class.new.class_eval(to_s) # check the message $:.delete File.expand_path(out_dir) rescue LoadError puts "Error creating file #{filename}" puts $!.message exit 1 else file = File.basename(filename) message_module = Util.module_to_path(package.map{|p| p.to_s.capitalize}.join('::')) filename = "#{out_dir}/#{message_module}/#{file}" if file_create log_writing(filename) FileUtils.mkpath(File.dirname(filename)) File.open(filename, 'w') {|file| file.write(to_s) } else to_s end end
def decrement
def decrement @indent -= 1 end
def in_context(klass, &block)
def in_context(klass, &block) increment context.push klass block.call context.pop decrement end
def increment
def increment @indent += 1 end
def initialize(proto_file=nil, proto_dir='.', out_dir='.')
def initialize(proto_file=nil, proto_dir='.', out_dir='.') @proto_dir, @out_dir = proto_dir, out_dir @indent = 0 @context = [] @attach_proto = false @proto_file = proto_file end
def required_message_from_proto(proto_file)
def required_message_from_proto(proto_file) rb_path = [ proto_file.sub(/\.proto\z/, '.pb.rb') ].join('/').gsub(/\/{2,}/, '/') unless File.exist?(rb_path) Compiler.compile(proto_file, @proto_dir, @out_dir) end rb_path.sub(/\.rb$/, '') end
def ruby
def ruby @ruby ||= [] end
def to_s
def to_s @ruby.join("\n") end
def visit(node)
def visit(node) node.accept_message_visitor(self) self end
def write(str)
def write(str) ruby << "#{' ' * @indent}#{str}" end