It's sometimes handy to setup a namespaced class when looking to utilise an overidden implementation of another class, i.e. template method pattern etc.
class MailProcessor
class Importer < Foo::Bar::Importer
# override inherited methods etc.
end
def perform
MailProcessor::Importer.new.perform
end
end
If you start to do override multiple classes, your base class is going to get rather large. Here's one way to clean to this up, let's move all of this into a simple mixin.
For the sake of this example, we are creating a ProcessorMixin
which
will namespace Processor
under the base _klass
.
# mail_processor/processor_mixin.rb
class MailProcessor
module ProcessorMixin
class << self
def included(_klass)
processor = Class.new(Foo::Processor) do
# your custom implementation goes here; override as you will!
end
_klass.const_set :Processor, processor
end
end
end
end
We only have to include our ProcessorMixin
and it's quite simple to
access it within our base class.
# mail_processor.rb
class MailProcessor
require_relative 'mail_processor/processor_mixin'
include ProcessorMixin
def perform
MailProcessor::Processor.new.perform
end
end
Why not just declare each in its own file?