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

#initializeCreateRpcVisitor

Returns a new instance of CreateRpcVisitor.



139
140
141
142
143
# File 'lib/protobuf/compiler/visitors.rb', line 139

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

Instance Attribute Details

#current_serviceObject

Returns the value of attribute current_service.



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

def current_service
  @current_service
end

#file_contentsObject

Returns the value of attribute file_contents.



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

def file_contents
  @file_contents
end

#packageObject

Returns the value of attribute package.



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

def package
  @package
end

#servicesObject

Returns the value of attribute services.



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

def services
  @services
end

Instance Method Details

#add_rpc(name, request, response) ⇒ Object



150
151
152
# File 'lib/protobuf/compiler/visitors.rb', line 150

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

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



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/protobuf/compiler/visitors.rb', line 154

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 = underscore service_name.to_s
    message_module = package.map{|p| p.to_s.capitalize}.join('::')
    required_file = [
      Util.module_to_path(message_module),
      File.basename(message_file.sub(/^\.\//, '').sub(/\.rb$/, ''))
    ].join('/').gsub(/\/{2,}/, '/')

    create_service(message_file, out_dir, underscored_name, message_module, service_name, default_port, rpcs, required_file)
  end
  @file_contents
end

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



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

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

#visit(node) ⇒ Object



145
146
147
148
# File 'lib/protobuf/compiler/visitors.rb', line 145

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