Class: RServiceBus2::SagaLoader
- Inherits:
-
Object
- Object
- RServiceBus2::SagaLoader
- Defined in:
- lib/rservicebus2/saga_loader.rb
Overview
Given a directory, this class is responsible loading Sagas
Instance Attribute Summary collapse
-
#saga_list ⇒ Object
readonly
Returns the value of attribute saga_list.
Instance Method Summary collapse
-
#get_list_of_files_for_dir(path) ⇒ Array
This method is overloaded for unit tests.
-
#get_require_path(file_path) ⇒ Object
Cleans the given path to ensure it can be used for as a parameter for the require statement.
-
#get_saga_name(file_path) ⇒ Object
Extract the top level dir or file name as it is the msg name.
-
#initialize(host, saga_manager) ⇒ SagaLoader
constructor
Constructor.
-
#load_saga(file_path, saga_name) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#load_saga_from_file(saga_name, file_path) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#load_sagas_from_path(base_dir) ⇒ Object
Entry point for loading Sagas.
Constructor Details
#initialize(host, saga_manager) ⇒ SagaLoader
Constructor
12 13 14 15 16 |
# File 'lib/rservicebus2/saga_loader.rb', line 12 def initialize(host, saga_manager) @host = host @saga_manager = saga_manager @list_of_loaded_paths = {} end |
Instance Attribute Details
#saga_list ⇒ Object (readonly)
Returns the value of attribute saga_list.
6 7 8 |
# File 'lib/rservicebus2/saga_loader.rb', line 6 def saga_list @saga_list end |
Instance Method Details
#get_list_of_files_for_dir(path) ⇒ Array
This method is overloaded for unit tests
87 88 89 90 91 92 93 |
# File 'lib/rservicebus2/saga_loader.rb', line 87 def get_list_of_files_for_dir(path) list = Dir["#{path}/*"] RServiceBus2.rlog "SagaLoader.getListOfFilesForDir. path: #{path}, list: #{list}" list end |
#get_require_path(file_path) ⇒ Object
Cleans the given path to ensure it can be used for as a parameter for the
require statement.
21 22 23 24 25 26 27 |
# File 'lib/rservicebus2/saga_loader.rb', line 21 def get_require_path(file_path) file_path = "./#{file_path}" unless file_path.start_with?('/') return file_path.sub('.rb', '') if File.exist?(file_path) abort("Filepath, #{file_path}, given for Saga require doesn't exist") end |
#get_saga_name(file_path) ⇒ Object
Extract the top level dir or file name as it is the msg name
97 98 99 100 101 102 103 104 |
# File 'lib/rservicebus2/saga_loader.rb', line 97 def get_saga_name(file_path) base_name = File.basename(file_path) ext_name = File.extname(base_name) saga_name = base_name.sub(ext_name, '') "saga_#{saga_name}".gsub(/(?<=_|^)(\w)/) { Regexp.last_match(1).upcase }.gsub(/(?:_)(\w)/, '\1') end |
#load_saga(file_path, saga_name) ⇒ Object
rubocop:disable Metrics/MethodLength
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rservicebus2/saga_loader.rb', line 61 def load_saga(file_path, saga_name) if @list_of_loaded_paths.key?(file_path) RServiceBus2.log "Not reloading, #{file_path}" return end RServiceBus2.rlog "file_path: #{file_path}" RServiceBus2.rlog "saga_name: #{saga_name}" saga = load_saga_from_file(saga_name, file_path) RServiceBus2.log "Loaded Saga: #{saga_name}" @saga_manager.register_saga(saga) @list_of_loaded_paths[file_path] = 1 rescue StandardError => e puts "Exception loading saga from file: #{file_path}" puts e. puts e.backtrace[0] abort end |
#load_saga_from_file(saga_name, file_path) ⇒ Object
rubocop:disable Metrics/MethodLength
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rservicebus2/saga_loader.rb', line 39 def load_saga_from_file(saga_name, file_path) require_path = get_require_path(file_path) require require_path begin saga = Object.const_get(saga_name) rescue StandardError => e puts "Expected class name: #{saga_name}, not found after require: #{require_path}" \ "**** Check in #{file_path} that the class is named: #{saga_name}" \ '( In case its not that )' raise e end saga end |
#load_sagas_from_path(base_dir) ⇒ Object
Entry point for loading Sagas
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/rservicebus2/saga_loader.rb', line 108 def load_sagas_from_path(base_dir) RServiceBus2.rlog "SagaLoader.loadSagasFromPath. base_dir: #{base_dir}" get_list_of_files_for_dir(base_dir).each do |file_path| unless file_path.end_with?('.') saga_name = get_saga_name(file_path) load_saga(file_path, saga_name) end end self end |