Module: RailsGrpc::Dependencies

Defined in:
lib/rails_grpc/dependencies.rb

Constant Summary collapse

@@proto_lib_dir =
"grpc/lib"
@@grpc_model_dir =
"grpc/models"
@@grpc_service_dir =
"grpc/services"
@@loaded =
false

Class Method Summary collapse

Class Method Details

.cache_classes?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/rails_grpc/dependencies.rb', line 42

def cache_classes?
  ::Rails.application.config.cache_classes
end

.clear_dependencies!Object



72
73
74
# File 'lib/rails_grpc/dependencies.rb', line 72

def clear_dependencies!
  @@loaded = false
end

.find_grpc_model_filesObject



34
35
36
# File 'lib/rails_grpc/dependencies.rb', line 34

def find_grpc_model_files
  Dir.glob(File.join(rails_root, grpc_model_dir, "**/*.rb"))
end

.find_grpc_service_filesObject



38
39
40
# File 'lib/rails_grpc/dependencies.rb', line 38

def find_grpc_service_files
  Dir.glob(File.join(rails_root, grpc_service_dir, "**/*.rb"))
end

.find_proto_lib_filesObject



17
18
19
# File 'lib/rails_grpc/dependencies.rb', line 17

def find_proto_lib_files
  Dir.glob(File.join(rails_root, proto_lib_dir, "**/*.rb"))
end

.find_proto_message_definitionsObject



21
22
23
24
25
26
# File 'lib/rails_grpc/dependencies.rb', line 21

def find_proto_message_definitions
  messages = find_proto_lib_files.reject do |t|
    File.basename(t).include?("_services_pb.rb")
  end
  messages.sort_by { |m| m.include?("_model_pb.rb") ? 0 : 1 } # FIXME: temporary patch to load model first
end

.find_proto_service_definitionsObject



28
29
30
31
32
# File 'lib/rails_grpc/dependencies.rb', line 28

def find_proto_service_definitions
  find_proto_lib_files.select do |t|
    File.basename(t).include?("_services_pb.rb")
  end
end

.load_and_watch(file_path, const_name) ⇒ Object



76
77
78
79
80
# File 'lib/rails_grpc/dependencies.rb', line 76

def load_and_watch(file_path, const_name)
  watch_file_and_dir(file_path) unless self.cache_classes?

  ActiveSupport::Dependencies.require_or_load(file_path, const_name.to_sym)
end

.load_dependencies!Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rails_grpc/dependencies.rb', line 46

def load_dependencies!
  return if @@loaded

  find_proto_message_definitions.each do |f|
    parent_const_name = File.basename(f).gsub(".", "::").split("::")[0].classify
    load_and_watch(f, parent_const_name)
  end

  find_proto_service_definitions.each do |f|
    parent_const_name = File.basename(f).gsub(".", "::").split("::")[0].classify
    load_and_watch(f, parent_const_name)
  end

  find_grpc_model_files.each do |f|
    parent_const_name = File.basename(f).gsub(".", "::").split("::")[0].classify
    load_and_watch(f, parent_const_name)
  end

  find_grpc_service_files.each do |f|
    parent_const_name = File.basename(f).gsub(".", "::").split("::")[0].classify
    load_and_watch(f, parent_const_name)
  end

  @@loaded = true
end

.rails_rootObject



13
14
15
# File 'lib/rails_grpc/dependencies.rb', line 13

def rails_root
  ::Rails.root
end

.watch_file_and_dir(file_path) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/rails_grpc/dependencies.rb', line 82

def watch_file_and_dir(file_path)
  dir_path = File.dirname(file_path)

  ::Rails.application.config.watchable_dirs[dir_path] = [:rb]
  unless ::Rails.application.config.watchable_files.include?(file_path)
    ::Rails.application.config.watchable_files.concat([file_path])
  end
end