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

Returns a new instance of CreateMessageVisitor.



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

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.



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

def attach_proto
  @attach_proto
end

#contextObject

Returns the value of attribute context.



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

def context
  @context
end

#indentObject

Returns the value of attribute indent.



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

def indent
  @indent
end

#proto_fileObject

Returns the value of attribute proto_file.



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

def proto_file
  @proto_file
end

Instance Method Details

#attach_proto?Boolean

Returns:

  • (Boolean)


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

def attach_proto?
  @attach_proto
end

#close_rubyObject



70
71
72
73
74
75
# File 'lib/protobuf/compiler/visitors.rb', line 70

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

#commented_proto_contentsObject



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

def commented_proto_contents
  if proto_file
    proto_filepath = File.exist?(proto_file) ? 
      proto_file : "#{@proto_dir}/#{proto_file}"
    File.read(proto_filepath).gsub(/^/, '# ')
  end
end

#create_files(filename, out_dir, file_create) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/protobuf/compiler/visitors.rb', line 106

def create_files(filename, out_dir, file_create)
  eval to_s # check the message
  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



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

def decrement
  @indent -= 1
end

#in_context(klass, &block) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/protobuf/compiler/visitors.rb', line 85

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

#incrementObject



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

def increment
  @indent += 1
end

#required_message_from_proto(proto_file) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/protobuf/compiler/visitors.rb', line 98

def required_message_from_proto(proto_file)
  rb_path = proto_file.sub(/\.proto$/, '.pb.rb')
  unless File.exist?("#{@out_dir}/#{rb_path}")
    Compiler.compile proto_file, @proto_dir, @out_dir
  end
  rb_path.sub(/\.rb$/, '')
end

#rubyObject



77
78
79
# File 'lib/protobuf/compiler/visitors.rb', line 77

def ruby
  @ruby ||= []
end

#to_sObject



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

def to_s
  @ruby.join "\n"
end

#visit(node) ⇒ Object



93
94
95
96
# File 'lib/protobuf/compiler/visitors.rb', line 93

def visit(node)
  node.accept_message_visitor self 
  self
end

#write(str) ⇒ Object



58
59
60
# File 'lib/protobuf/compiler/visitors.rb', line 58

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