Class: Protod::Interpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/protod/interpreter.rb,
lib/protod/interpreter/rpc.rb,
lib/protod/interpreter/builtin.rb,
lib/protod/interpreter/active_record.rb

Defined Under Namespace

Modules: ProtoMessageCacheable, SkipNilAbility Classes: ActiveRecord, Base, Builtin, Rpc

Class Method Summary collapse

Class Method Details

.clear!Object



70
71
72
73
# File 'lib/protod/interpreter.rb', line 70

def clear!
  @map = nil
  @reverse_map = nil
end

.find_by(const_or_name, with_register_from_ancestor: false) ⇒ Object



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
# File 'lib/protod/interpreter.rb', line 28

def find_by(const_or_name, with_register_from_ancestor: false)
  const_name  = const_or_name.is_a?(::String) ? Protod::RubyIdent.absolute_of(const_or_name) : nil
  const       = const_or_name.is_a?(::String) ? const_or_name.safe_constantize : const_or_name
  entryables  = const.respond_to?(:ancestors) ? const.ancestors : [const]
  entry_const = entryables.compact.find { map.key?(_1) }
  entry       = entry_const && map.fetch(entry_const)

  return unless entry

  unless entry.is_a?(Base)
    entry = map[entry_const] = entry.fetch(:const).new(entry_const, **entry.except(:const)).tap do
      _1.extend SkipNilAbility
      _1.extend ProtoMessageCacheable
    end
  end

  return entry unless with_register_from_ancestor

  unless map[const].is_a?(Base)
    map[const] = entry.class.new(const, parent: entry.parent, path: entry.path).tap do
      _1.extend SkipNilAbility
      _1.extend ProtoMessageCacheable
    end
  end

  map.fetch(const)
end

.find_by_proto(full_ident) ⇒ Object

Raises:

  • (NotImplementedError)


56
57
58
59
60
# File 'lib/protod/interpreter.rb', line 56

def find_by_proto(full_ident)
  raise NotImplementedError, "You need to call Protod::Interpreter.setup_reverse_lookup! before" unless @reverse_map

  @reverse_map[full_ident]
end

.keysObject



62
63
64
# File 'lib/protod/interpreter.rb', line 62

def keys
  map.keys
end

.register_for(*const_names, with: nil, parent: nil, path: nil, force: true, ignore: false, &body) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/protod/interpreter.rb', line 6

def register_for(*const_names, with: nil, parent: nil, path: nil, force: true, ignore: false, &body)
  base = if with
           find_by(with)&.class or raise ArgumentError, "Not found the interpreter for #{with}"
         else
           Base
         end

  Class.new(base, &body).tap do |interpreter_const|
    const_names.each do
      c = Protod::RubyIdent.absolute_of(_1).safe_constantize

      raise ArgumentError, "Not found to constantize for #{_1}" unless c

      next if map.key?(c) && ignore

      raise ArgumentError, "Interpreter already regsitered for #{c.name}" if map.key?(c) && !force

      map[c] = { const: interpreter_const, parent: parent, path: path }
    end
  end
end

.reverse_keysObject



66
67
68
# File 'lib/protod/interpreter.rb', line 66

def reverse_keys
  @reverse_map&.keys
end

.setup_reverse_lookup!Object



75
76
77
# File 'lib/protod/interpreter.rb', line 75

def setup_reverse_lookup!
  @reverse_map = keys.map { find_by(_1) }.uniq.map { [_1.proto_full_ident, _1] }.to_h
end