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

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



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

- (Object) attach_proto

Returns the value of attribute attach_proto



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

def attach_proto
  @attach_proto
end

- (Object) context

Returns the value of attribute context



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

def context
  @context
end

- (Object) indent

Returns the value of attribute indent



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

def indent
  @indent
end

- (Object) proto_file

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

- (Boolean) attach_proto?



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

def attach_proto?
  @attach_proto
end

- (Object) close_ruby



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

- (Object) commented_proto_contents



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

- (Object) create_files(filename, out_dir, file_create)



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

- (Object) decrement



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

def decrement
  @indent -= 1
end

- (Object) in_context(klass, &block)



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

- (Object) increment



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

def increment
  @indent += 1
end

- (Object) required_message_from_proto(proto_file)



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

- (Object) ruby



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

def ruby
  @ruby ||= []
end

- (Object) to_s



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

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

- (Object) visit(node)



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

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

- (Object) write(str)



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

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