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.



121
122
123
124
125
# File 'lib/protobuf/compiler/visitors.rb', line 121

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

Instance Attribute Details

#current_serviceObject

Returns the value of attribute current_service.



119
120
121
# File 'lib/protobuf/compiler/visitors.rb', line 119

def current_service
  @current_service
end

#file_contentsObject

Returns the value of attribute file_contents.



119
120
121
# File 'lib/protobuf/compiler/visitors.rb', line 119

def file_contents
  @file_contents
end

#packageObject

Returns the value of attribute package.



119
120
121
# File 'lib/protobuf/compiler/visitors.rb', line 119

def package
  @package
end

#servicesObject

Returns the value of attribute services.



119
120
121
# File 'lib/protobuf/compiler/visitors.rb', line 119

def services
  @services
end

Instance Method Details

#add_rpc(name, request, response) ⇒ Object



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

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



155
156
157
158
159
160
# File 'lib/protobuf/compiler/visitors.rb', line 155

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, message_module, required_file) ⇒ Object



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

def create_client(out_dir, underscored_name, default_port, name, request, response, 
  message_module, required_file)
  client_filename = "#{out_dir}/client_#{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



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/protobuf/compiler/visitors.rb', line 136

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 = message_file.sub(/^\.\//, '').sub(/\.rb$/, '')

    create_bin out_dir, underscored_name, message_module, service_name, default_port
    create_service message_file, out_dir, underscored_name, message_module, 
      service_name, default_port, rpcs, required_file
    rpcs.each do |name, request, response|
      create_client out_dir, underscored_name, default_port, name, request, response, 
        message_module, 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



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

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



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

def visit(node)
  node.accept_rpc_visitor self
  self
end