Giter Club home page Giter Club logo

subst-attr's Introduction

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

A Strict Attribute that Provides a Specialized Substitute

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] A Strict Attribute that Provides a Specialized Null Object

[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

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.

subst-attr's People

Contributors

keisaacson avatar ntl avatar sbellware avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.