# 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