Sha256: e94d87ea2ac623acd93f0b9003d589bfd40c56acbfe512266b9018aaa84cd42e

Contents?: true

Size: 1.65 KB

Versions: 4

Compression:

Stored size: 1.65 KB

Contents

# encoding: utf-8

module Hexx

  # 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+.
  #
  # @example
  #   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
  module Dependable

    private

    # @!method depends_on(name, options = {})
    # Declares the dependency with its default implementation.
    # @example (see Hexx::Dependable)
    # @param   [String, Symbol] name The name of the dependency.
    # @param   [Hash] options ({}) The dependency declaration options.
    # @option  options [String, Symbol, Class] :default (nil) Optional default
    #   implementation for the dependency.
    def depends_on(name, default: nil)
      Helpers::Dependency.add self, name, default
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hexx-6.0.3 lib/hexx/dependable.rb
hexx-6.0.2 lib/hexx/dependable.rb
hexx-6.0.1 lib/hexx/dependable.rb
hexx-6.0.0 lib/hexx/dependable.rb