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
Constant Summary collapse
- CONSTANT_REGEXP =
Regexp used to remove any non classy like characters that might be in the consumer class name (if defined dynamically, etc)
%r{[?!=+\-\*/\^\|&\[\]<>%~\#\:\s\(\)]}
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.
22 23 24 25 26 |
# File 'lib/karafka/helpers/class_matcher.rb', line 22 def initialize(klass, from:, to:) @klass = klass @from = from @to = to end |
Instance Method Details
#match ⇒ Class?
30 31 32 33 34 35 |
# File 'lib/karafka/helpers/class_matcher.rb', line 30 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.
43 44 45 46 47 48 |
# File 'lib/karafka/helpers/class_matcher.rb', line 43 def name inflected = @klass.to_s.split('::').last.to_s 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.
51 52 53 |
# File 'lib/karafka/helpers/class_matcher.rb', line 51 def scope scope_of(@klass) end |