Module: Hexx::Configurable

Defined in:
lib/hexx/configurable.rb

Overview

Contains methods to declare and inject module dependencies.

Allows to provide DJ framework for the gem.

Examples:

# 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

Instance Method Summary collapse

Instance Method Details

#configure {|+self+.| ... } ⇒ Object

Yields a block and gives it self

Examples:

module MyGem
  extend Hexx::Configurable
end

MyGem.configure do |config|
  c # => MyGem
end

Yields:

  • The block.

Yield Parameters:


48
49
50
# File 'lib/hexx/configurable.rb', line 48

def configure
  yield(self)
end

#depends_on(*names, options) ⇒ Object

Declares a dependency getter and setter.

Examples:

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

Dependencies can be declared by a plain list of names

module MyGem
  extend Hexx::Configurable
  self.depends_on :some_class, "another_class"
end

Dependencies can be declared by an array of names

module MyGem
  extend Hexx::Configurable
  self.depends_on %w(some_class another_class)
end

A dependency can have default implementation

module MyGem
  extend Hexx::Configurable
  self.depends_on :item, default: Item
end

Options Hash (options):

  • :default (Module, nil) — default: nil

    The default implementation for the dependency.


95
96
97
98
99
# File 'lib/hexx/configurable.rb', line 95

def depends_on(*names, default: nil)
  names.flatten.each do |name|
    Creators::ModuleDependency.add self, name, default
  end
end