lib/active_merchant_hacks.rb
BraintreeData has initialize method Open
Open
module BraintreeData
- Read upRead up
- Exclude checks
A module is usually a mixin, so when an #initialize
method is present it is
hard to tell initialization order and parameters so having #initialize
in a module is usually a bad idea.
Example
The Foo
module below contains a method initialize
. Although class B
inherits from A
, the inclusion of Foo
stops A#initialize
from being called.
class A
def initialize(a)
@a = a
end
end
module Foo
def initialize(foo)
@foo = foo
end
end
class B < A
include Foo
def initialize(b)
super('bar')
@b = b
end
end
A simple solution is to rename Foo#initialize
and call that method by name:
module Foo
def setup_foo_module(foo)
@foo = foo
end
end
class B < A
include Foo
def initialize(b)
super 'bar'
setup_foo_module('foo')
@b = b
end
end
ActiveMerchant::Billing::Gateway#threescale_unstore manually dispatches method call Open
Open
return nil unless identification.present? && respond_to?(:unstore)
- Read upRead up
- Exclude checks
Reek reports a Manual Dispatch smell if it finds source code that manually checks whether an object responds to a method before that method is called. Manual dispatch is a type of Simulated Polymorphism which leads to code that is harder to reason about, debug, and refactor.
Example
class MyManualDispatcher
attr_reader :foo
def initialize(foo)
@foo = foo
end
def call
foo.bar if foo.respond_to?(:bar)
end
end
Reek would emit the following warning:
test.rb -- 1 warning:
[9]: MyManualDispatcher manually dispatches method call (ManualDispatch)