Attributes that have default implementations that are substitutes or null objects
Has a default value of a null object that responds to any method (a weak null object)
class Example
subst_attr :some_attr
end
e = Example.new
e.anything # => No error raised
Has a default value of a null object that responds only to the methods of the specified class
class SomeDependency
def some_method
# ...
end
end
class Example
subst_attr :some_attr, SomeDependency
end
e = Example.new
e.some_method # => No error raised
e.anything # => NoMethodError is raised
If the class used to define the attribute's strict interface has an inner NullObject
namespace that has a build
method, the object that will be used as the null object is the one returned from the build
method.
NOTE: Use this if a custom substitute implementation is needed, including a specialized null object
class SomeDependency
def some_method
# ...
end
module Substitute
def self.build
SomeOtherThing.etc
end
end
end
class Example
subst_attr :some_attr, SomeDependency
end
e = Example.new
[Deprecated: Use the Substitute feature instead]
If the class used to define the attribute's strict interface has an inner NullObject
namespace that has a build
method, the object that will be used as the null object is the one returned from the build
method.
class SomeDependency
def some_method
# ...
end
module NullObject
def self.build
SomeOtherNullObjectLibrary.etc
end
end
end
class Example
subst_attr :some_attr, SomeDependency
end
e = Example.new
Typically, the SubstAddr module must be included in a class that will use the subst_attr
macro.
The SubstAddr
can be included in Ruby's Object
archetype, making the subst_attr
macro available to all classes.
The activate
method is provided as a shortcut to opening the Object
class and including the SubstAttr
explicitly.
SubstAttr.activate
class Example
subst_attr :some_attr
end
Alternatively, the SubstAddr
module can be included anywhere within the object hierarchy by specifying the class to activate.
class Example
end
SubstAttr.activate Example
class Example
subst_attr :some_attr
end
As in the example above, for a class to be "activated" for SubstAddr
, the class must have already been defined. The subst_addr
class method must be visible to the class using it, either my including it in the class directly, or by including it in any super class of the using class.
The subst_attr
library is released under the MIT License.