module Rely::Dependencies

Defines a macro to allow object dependencies to be specified at the class level with a block defining a default value.

Based on attr_defaultable.

Public Instance Methods

dependencies() click to toggle source

Stores a list of dependency attributes for use by the initializer.

# File lib/rely/dependencies.rb, line 19
def dependencies
  @dependencies ||= Set.new
end
dependency(attribute, default) click to toggle source

Defines a new dependency attribute with a default value.

# File lib/rely/dependencies.rb, line 24
def dependency(attribute, default)
  # Define a getter that lazily sets a default value.
  define_method attribute do
    if instance_variable_defined?(:"@#{attribute}")
      instance_variable_get(:"@#{attribute}")
    else
      default_value = instance_exec(&default)
      instance_variable_set(:"@#{attribute}", default_value)
    end
  end

  # Define a setter that lazily sets a default value.
  attr_writer attribute

  # Mark the getter and setter as protected.
  protected attribute, "#{attribute}="

  # Add the attribute to the list of dependencies.
  dependencies << attribute
end
inherited(subclass) click to toggle source

Ensure dependencies from the superclass are inherited by the subclass.

Calls superclass method
# File lib/rely/dependencies.rb, line 10
def inherited(subclass)
  if superclass.respond_to?(:dependencies)
    subclass.dependencies.merge dependencies
  end

  super
end