Class: Karafka::Helpers::ClassMatcher
- Inherits:
-
Object
- Object
- Karafka::Helpers::ClassMatcher
- Defined in:
- lib/karafka/helpers/class_matcher.rb
Overview
Class used to autodetect corresponding classes that are internally inside Karafka framework It is used among others to match:
consumer => responder
Instance Method Summary collapse
-
#initialize(klass, from:, to:) ⇒ ClassMatcher
constructor
A new instance of ClassMatcher.
- #match ⇒ Class?
-
#name ⇒ String
Name of a new class that we're looking for.
-
#scope ⇒ Class, Module
Class or module in which we're looking for a matching.
Constructor Details
#initialize(klass, from:, to:) ⇒ ClassMatcher
Returns a new instance of ClassMatcher.
24 25 26 27 28 |
# File 'lib/karafka/helpers/class_matcher.rb', line 24 def initialize(klass, from:, to:) @klass = klass @from = from @to = to end |
Instance Method Details
#match ⇒ Class?
32 33 34 35 36 37 38 |
# File 'lib/karafka/helpers/class_matcher.rb', line 32 def match return nil if name.empty? return nil unless scope.const_defined?(name) matching = scope.const_get(name) same_scope?(matching) ? matching : nil end |
#name ⇒ String
Note:
This method returns name of a class without a namespace
Returns name of a new class that we're looking for.
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/karafka/helpers/class_matcher.rb', line 46 def name inflected = +@klass.to_s.split('::').last.to_s # We inject the from into the name just in case it is missing as in a situation like # that it would just sanitize the name without adding the "to" postfix. # It could create cases when we want to build for example a responder to a consumer # that does not have the "Consumer" postfix and would do nothing returning the same name. # That would be bad as the matching classes shouldn't be matched to themselves. inflected << @from unless inflected.include?(@from) inflected.gsub!(@from, @to) inflected.gsub!(CONSTANT_REGEXP, '') inflected end |
#scope ⇒ Class, Module
Returns class or module in which we're looking for a matching.
60 61 62 |
# File 'lib/karafka/helpers/class_matcher.rb', line 60 def scope scope_of(@klass) end |