Module: KRPC::Gen

Defined in:
lib/krpc/gen.rb

Defined Under Namespace

Modules: RPCMethodGenerator Classes: ClassBase

Class Method Summary collapse

Class Method Details

.add_rpc_method(cls, method_name, service_name, proc, *options) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/krpc/gen.rb', line 30

def add_rpc_method(cls, method_name, service_name, proc, *options)
  is_static = options.include? :static
  prepend_self_to_args = options.include? :prepend_self_to_args
  param_names, param_types, param_default, return_type = parse_procedure(proc)
  method_name = method_name.underscore
  args = [cls, method_name, param_default, param_names, param_types, prepend_self_to_args, proc, return_type, service_name]

  define_rpc_method(*args)
  define_static_rpc_method(*args) if is_static
  add_stream_constructing_proc(*args) unless options.include? :no_stream
  Doc.add_docstring_info(is_static, cls, method_name, service_name, proc.name, param_names, param_types, param_default, return_type: return_type, xmldoc: proc.documentation)
end

.generate_class(service_name, class_name) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/krpc/gen.rb', line 13

def generate_class(service_name, class_name)
  mod = service_gen_module(service_name)
  mod.const_get_or_create(class_name) do
    Class.new(ClassBase) do
      @service_name = service_name
      class << self; attr_reader :service_name end
    end
  end
end

.generate_enum(service_name, enum_name, values) ⇒ Object



23
24
25
26
27
28
# File 'lib/krpc/gen.rb', line 23

def generate_enum(service_name, enum_name, values)
  mod = service_gen_module(service_name)
  mod.const_get_or_create(enum_name) do
    values.map{|ev| [ev.name.underscore.to_sym, ev.value]}.to_h
  end
end

.service_gen_module(service_name) ⇒ Object



9
10
11
# File 'lib/krpc/gen.rb', line 9

def service_gen_module(service_name) 
  const_get_or_create(service_name, Module.new)
end

.transform_exceptions(method_owner, method_name, prepend_self_to_args, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/krpc/gen.rb', line 43

def transform_exceptions(method_owner, method_name, prepend_self_to_args, &block)
  begin
    block.call
  rescue ArgumentsNumberErrorSig => err
    err = err.with_signature(Doc.docstring_for_method(method_owner, method_name, false))
    if prepend_self_to_args then raise err.with_arguments_count_incremented_by(-1)
    elsif method_owner.is_a?(Class) then raise err.with_arguments_count_incremented_by(1)
    else raise err end
  rescue ArgumentErrorSig => err
    raise err.with_signature(Doc.docstring_for_method(method_owner, method_name, false))
  end
end