Class: Protod::Proto::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/protod/proto/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(root_package) ⇒ Builder

Returns a new instance of Builder.



6
7
8
9
# File 'lib/protod/proto/builder.rb', line 6

def initialize(root_package)
  @root_package = root_package
  @receivers    = {}
end

Instance Method Details

#buildObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/protod/proto/builder.rb', line 19

def build
  return if @root_package.built?

  handler = Protod::Rpc::Handler.build_in(@root_package)

  @receivers.keys.each do |receiver|
    package = Protod.find_or_register_package("#{@root_package.full_ident}.#{receiver.underscore.gsub('/', '.')}")

    request_field  = Protod::Proto::Field.build_from(Protod::Rpc::Request.find_by(receiver), ident: receiver)
    response_field = Protod::Proto::Field.build_from(Protod::Rpc::Response.find_by(receiver), ident: receiver)

    handler.register_receiver(request_field, response_field)

    request_message  = package.bind(request_field.interpreter)
    response_message = package.bind(response_field.interpreter)

    bindable_interpreters_under(request_message, response_message).each do |i|
      root_message_name = i.const.ruby_ident.singleton ? 'Singleton' : 'Instance'

      package
        .find_or_push(root_message_name, by: :ident, into: :messages)
        .find_or_push(i.const.ruby_ident.method_name, by: :ident, into: :messages)
        .bind(i)
    end
  end

  models_package = Protod.find_or_register_package("#{@root_package.full_ident}.models")

  while (interpreters = bindable_interpreters_under(*@root_package.all_packages)).present?
    interpreters.each { models_package.bind(_1) }
  end

  # For supporting an Array/Hash instance at the fields whiches type is google.protobuf.Any,
  # make the proto definition for Array emerge even if it's not requried.
  any_interpreter = Protod::Interpreter.find_by('RBS::Types::Bases::Any')
  if @root_package.all_packages.flat_map(&:collect_fields).any? { _1.interpreter == any_interpreter }
    i = Protod::Interpreter.find_by('Array')

    models_package.bind(i) if i.bindable?

    models_package.imports.push(Protod::Interpreter.find_by('Hash').proto_path)
  end
end

#push_receiver(ruby_ident) ⇒ Object



11
12
13
# File 'lib/protod/proto/builder.rb', line 11

def push_receiver(ruby_ident)
  @receivers[ruby_ident.to_s] = true
end

#receiver_pushed?(ruby_ident) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/protod/proto/builder.rb', line 15

def receiver_pushed?(ruby_ident)
  @receivers.key?(ruby_ident.to_s)
end