Module: ProtocGenRails
- Defined in:
- lib/generator.rb
Defined Under Namespace
Classes: FileResult
Class Method Summary collapse
- .parse_erb(filename, data) ⇒ String
- .process(input) ⇒ String
- .process_service(service) ⇒ FileResult
- .require_directories(params) ⇒ Object
- .route_file(services) ⇒ FileResult
Class Method Details
.parse_erb(filename, data) ⇒ String
82 83 84 85 |
# File 'lib/generator.rb', line 82 def parse_erb(filename, data) erb = File.read("#{__dir__}/generator/#{filename}.rb.erb") ERB.new(erb, trim_mode: '-').result_with_hash(data.to_h) end |
.process(input) ⇒ String
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/generator.rb', line 34 def process(input) request = Google::Protobuf::Compiler::CodeGeneratorRequest.decode(input) files = [] require_directories(request.parameter) services = [] request.proto_file.each do |proto_file| next if request.file_to_generate.exclude?(proto_file.name) next if proto_file.service.none? proto_file.service.each do |service_descriptor| service = ProtocGenRails::Service.new(service_descriptor, proto_file.package) services.push(service) file = process_service(service) files.push(file) if file end end if files.any? route_output = route_file(services) files.push(route_output) if route_output end Output.response(files) end |
.process_service(service) ⇒ FileResult
61 62 63 64 65 66 67 |
# File 'lib/generator.rb', line 61 def process_service(service) content = parse_erb('controller', { service: service }) FileResult.new( content: content, name: "app/controllers/#{service.name.underscore}_controller.rb" ) end |
.require_directories(params) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/generator.rb', line 18 def require_directories(params) params.split(',').each do |param| key, value = param.split('=') next unless key == 'require' dirs = value.split(',') dirs.each do |v| $LOAD_PATH.unshift(v) dir = File.(v) Dir["#{dir}/**/*.rb"].each { |file| require file } end end end |
.route_file(services) ⇒ FileResult
71 72 73 74 75 76 77 |
# File 'lib/generator.rb', line 71 def route_file(services) content = parse_erb('grpc', { services: services }) FileResult.new( name: 'config/routes/grpc.rb', content: content ) end |