Skip to content

eventide-project/subst-attr

master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

subst_attr

Attributes that have default implementations that are substitutes or null objects

Examples

A Weak Attribute

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

A Strict Attribute

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

Specialized Substitute

If the class used to define the attribute's strict interface has an inner Substitute 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 Substitute
    def self.build
      SomeOtherThing.etc
    end
  end
end

class Example
  subst_attr :some_attr, SomeDependency
end

e = Example.new

Activation

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.

License

The subst_attr library is released under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published