Class: Protobuf::Visitor::CreateMessageVisitor

Inherits:
Base
  • Object
show all
Defined in:
lib/protobuf/compiler/visitors.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#silent

Instance Method Summary collapse

Methods inherited from Base

#create_file_with_backup, #log_writing

Constructor Details

#initialize(proto_file = nil, proto_dir = '.', out_dir = '.') ⇒ CreateMessageVisitor



39
40
41
42
43
44
45
# File 'lib/protobuf/compiler/visitors.rb', line 39

def initialize(proto_file=nil, proto_dir='.', out_dir='.')
  @proto_dir, @out_dir = proto_dir, out_dir
  @indent = 0
  @context = []
  @attach_proto = true
  @proto_file = proto_file
end

Instance Attribute Details

#attach_protoObject

Returns the value of attribute attach_proto



37
38
39
# File 'lib/protobuf/compiler/visitors.rb', line 37

def attach_proto
  @attach_proto
end

#contextObject

Returns the value of attribute context



37
38
39
# File 'lib/protobuf/compiler/visitors.rb', line 37

def context
  @context
end

#indentObject

Returns the value of attribute indent



37
38
39
# File 'lib/protobuf/compiler/visitors.rb', line 37

def indent
  @indent
end

#proto_fileObject

Returns the value of attribute proto_file



37
38
39
# File 'lib/protobuf/compiler/visitors.rb', line 37

def proto_file
  @proto_file
end

Instance Method Details

#attach_proto?Boolean



47
48
49
# File 'lib/protobuf/compiler/visitors.rb', line 47

def attach_proto?
  @attach_proto
end

#close_rubyObject



73
74
75
76
77
78
# File 'lib/protobuf/compiler/visitors.rb', line 73

def close_ruby
  while 0 < indent
    decrement
    write('end')
  end
end

#commented_proto_contentsObject



51
52
53
54
55
56
57
58
59
# File 'lib/protobuf/compiler/visitors.rb', line 51

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

#create_files(filename, out_dir, file_create) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/protobuf/compiler/visitors.rb', line 108

def create_files(filename, out_dir, file_create)
  begin
    eval(to_s, TOPLEVEL_BINDING)  # check the message
  rescue LoadError
    $stderr.puts "Warning, couldn't test load proto file because of imports"
  end
  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

#decrementObject



69
70
71
# File 'lib/protobuf/compiler/visitors.rb', line 69

def decrement
  @indent -= 1
end

#in_context(klass, &block) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/protobuf/compiler/visitors.rb', line 88

def in_context(klass, &block)
  increment
  context.push klass
  block.call
  context.pop
  decrement
end

#incrementObject



65
66
67
# File 'lib/protobuf/compiler/visitors.rb', line 65

def increment
  @indent += 1
end

#required_message_from_proto(proto_file) ⇒ Object



101
102
103
104
105
106
# File 'lib/protobuf/compiler/visitors.rb', line 101

def required_message_from_proto(proto_file)
  unless File.exist?(File.join(@out_dir, File.basename(proto_file, '.proto') + '.pb.rb'))
    Compiler.compile(proto_file, @proto_dir, @out_dir)
  end
  proto_file.sub(/\.proto\z/, '.pb')
end

#rubyObject



80
81
82
# File 'lib/protobuf/compiler/visitors.rb', line 80

def ruby
  @ruby ||= []
end

#to_sObject



84
85
86
# File 'lib/protobuf/compiler/visitors.rb', line 84

def to_s
  @ruby.join("\n")
end

#visit(node) ⇒ Object



96
97
98
99
# File 'lib/protobuf/compiler/visitors.rb', line 96

def visit(node)
  node.accept_message_visitor(self)
  self
end

#write(str) ⇒ Object



61
62
63
# File 'lib/protobuf/compiler/visitors.rb', line 61

def write(str)
  ruby << "#{'  ' * @indent}#{str}"
end