Class: Protobuf::Visitor::CreateRpcVisitor

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 ⇒ CreateRpcVisitor

Returns a new instance of CreateRpcVisitor.



127
128
129
130
131
# File 'lib/protobuf/compiler/visitors.rb', line 127

def initialize
  @services = {}
  @create_file = true
  @file_contents = {}
end

Instance Attribute Details

#current_service ⇒ Object

Returns the value of attribute current_service.



125
126
127
# File 'lib/protobuf/compiler/visitors.rb', line 125

def current_service
  @current_service
end

#file_contents ⇒ Object

Returns the value of attribute file_contents.



125
126
127
# File 'lib/protobuf/compiler/visitors.rb', line 125

def file_contents
  @file_contents
end

#package ⇒ Object

Returns the value of attribute package.



125
126
127
# File 'lib/protobuf/compiler/visitors.rb', line 125

def package
  @package
end

#services ⇒ Object

Returns the value of attribute services.



125
126
127
# File 'lib/protobuf/compiler/visitors.rb', line 125

def services
  @services
end

Instance Method Details

#add_rpc(name, request, response) ⇒ Object



138
139
140
# File 'lib/protobuf/compiler/visitors.rb', line 138

def add_rpc(name, request, response)
  (@services[@current_service] ||= []) << [name, request.first, response.first]
end

#create_bin(out_dir, underscored_name, module_name, service_name, default_port) ⇒ Object



161
162
163
164
165
166
# File 'lib/protobuf/compiler/visitors.rb', line 161

def create_bin(out_dir, underscored_name, module_name, service_name, default_port)
  bin_filename = "#{out_dir}/start_#{underscored_name}"
  bin_contents = template_erb('rpc_bin').result(binding)
  create_file_with_backup(bin_filename, bin_contents, true) if @create_file
  @file_contents[bin_filename] = bin_contents
end

#create_client(out_dir, underscored_name, default_port, name, request, response, module_name, required_file) ⇒ Object



175
176
177
178
179
180
# File 'lib/protobuf/compiler/visitors.rb', line 175

def create_client(out_dir, underscored_name, default_port, name, request, response, module_name, required_file)
  client_filename = "#{out_dir}/client_#{Util.underscore(name)}.rb"
  client_contents = template_erb('rpc_client').result binding
  create_file_with_backup(client_filename, client_contents, true) if @create_file
  @file_contents[client_filename] = client_contents
end

#create_files(message_file, out_dir, create_file = true) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/protobuf/compiler/visitors.rb', line 142

def create_files(message_file, out_dir, create_file=true)
  @create_file = create_file
  default_port = 9999
  @services.each do |service_name, rpcs|
    underscored_name = Util.underscore(service_name.to_s)
    module_name = package.map{|p| Util.camelize(p.to_s)}.join('::') if package
    required_file = message_file.sub(/^\.\//, '').sub(/\.rb$/, '')

    create_bin(out_dir, underscored_name, module_name, service_name, default_port)
    create_service(message_file, out_dir, underscored_name, module_name,
      service_name, default_port, rpcs, required_file)
    rpcs.each do |name, request, response|
      create_client(out_dir, underscored_name, default_port, name, request, response,
        module_name, required_file)
    end
  end
  @file_contents
end

#create_service(message_file, out_dir, underscored_name, module_name, service_name, default_port, rpcs, required_file) ⇒ Object



168
169
170
171
172
173
# File 'lib/protobuf/compiler/visitors.rb', line 168

def create_service(message_file, out_dir, underscored_name, module_name, service_name, default_port, rpcs, required_file)
  service_filename = "#{out_dir}/#{underscored_name}.rb"
  service_contents = template_erb('rpc_service').result(binding)
  create_file_with_backup(service_filename, service_contents) if @create_file
  @file_contents[service_filename] = service_contents
end

#visit(node) ⇒ Object



133
134
135
136
# File 'lib/protobuf/compiler/visitors.rb', line 133

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