Module: Hexx::Dependable

Defined in:
lib/hexx/dependable.rb

Overview

Makes the class dependable from implementations through setter injections.

Adds the private #depends_on helper method to the class. Basically the method is similar to the attr_accessor with the following differencies:

  • it adds a semantics to the declaration.

  • it allows setting the default implementation.

  • dependency setters accepts classes and modules only and fails with the TypeError otherwise.

  • dependency getters fails with the NotImplemented error if the implementation hasn't been set.

  • if a default implementation is defined, the dependency cannot be set to nil.

Examples:

class MyClass
  extend Hexx::Dependable

  depends_on :another_class, default: AnotherClass
  depends_on :looks_for_implementation
end

object = MyClass.new
object.another_class # => AnotherClass

object.looks_for_implementation
# => fails with NotImplementedError

object.looks_for_implementation = SomeInjection
object.looks_for_implementation # => SomeInjection

Instance Method Summary collapse

Instance Method Details

#depends_on(name, options = {}) ⇒ Object (private)

Declares the dependency with its default implementation.

Examples:

class MyClass
  extend Hexx::Dependable

  depends_on :another_class, default: AnotherClass
  depends_on :looks_for_implementation
end

object = MyClass.new
object.another_class # => AnotherClass

object.looks_for_implementation
# => fails with NotImplementedError

object.looks_for_implementation = SomeInjection
object.looks_for_implementation # => SomeInjection

Options Hash (options):

  • :default (String, Symbol, Class) — default: nil

    Optional default implementation for the dependency.


47
48
49
# File 'lib/hexx/dependable.rb', line 47

def depends_on(name, default: nil)
  Creators::Dependency.add self, name, default
end