# encoding: utf-8 module Hexx # Contains methods to declare and inject module dependencies. # # Allows to provide DJ framework for the gem. # # @example # # Declare the dependencies for the gem root module # # # lib/my_gem.rb # module MyGem # extend Hexx::Configurable # self.depends_on :get_item, :add_item # self.depends_on :delete_item, default: DeleteItem # end # # # Inject dependencies (as a constant or its name) # # # config/my_gem.rb # MyGem.configure do |config| # config.get_item = AnotherGem::Services::Get # config.add_item # end # # # Use dependency in the module code (models, services, etc.) # # When the dependency hasn't been set, fails with +NotImplementedError+ # # MyGem.get_item # => AnotherGem::Services::Get # MyGem.get_item # => DeleteItem # MyGem.add_item # fails with a NotImplementedError module Configurable # Yields a block and gives it self # # @example # module MyGem # extend Hexx::Configurable # end # # MyGem.configure do |config| # c # => MyGem # end # # @yield The block. # @yieldparam [Hexx::Configurable] +self+. def configure yield(self) end # @!method depends_on(*names, options) # Declares a dependency getter and setter. # # @example # module MyGem # extend Hexx::Configurable # self.depends_on :some_class # end # # MyGem.some_class = String # # => "String" # MyGem.some_class # => String # # MyGem.some_class = "String" # # => "String" # MyGem.some_class # => String # # MyGem.some_class = :String # # => "String" # MyGem.some_class # => String # # @example Dependencies can be declared by a plain list of names # module MyGem # extend Hexx::Configurable # self.depends_on :some_class, "another_class" # end # # @example Dependencies can be declared by an array of names # module MyGem # extend Hexx::Configurable # self.depends_on %w(some_class another_class) # end # # @example A dependency can have default implementation # module MyGem # extend Hexx::Configurable # self.depends_on :item, default: Item # end # # @param [Array] names The list of names for dependencies. # @param [Hash] options The list of options for dependency declaration. # @option options [Module, nil] :default (nil) The default implementation # for the dependency. def depends_on(*names, default: nil) names.flatten.each do |name| Creators::ModuleDependency.add self, name, default end end end end