class Core::Extension::Dependency

public

Defines a dependency that can be applied to objects.

flags - Changes how the dependencies are applied. Possible values include:

  * `:definition` - Extends the definition of the including object.

  * `:implementation` - Extends the implementation of the including object.

Dependencies are applied with the `:implementation` flag by default.

dependency - The dependency to be applied to objects, following the rules defined by flags.

prepend - If `true`, methods will be prepended.

Constants

ALLOWED_FLAGS
ALLOWED_FLAGS_STRING

Public Class Methods

new(*flags, dependency:, prepend: false) click to toggle source
# File lib/core/extension/dependency.rb, line 25
def initialize(*flags, dependency:, prepend: false)
  flags = flags.map(&:to_sym)
  enforce_allowed_flags(flags)

  @flags = flags
  @dependency = dependency
  @prepend = prepend
  @definition = @flags.include?(:definition)
  @implementation = @flags.include?(:implementation) || (!definition? && !prepend?)
end

Public Instance Methods

apply_extend(object) click to toggle source
public

Apply the defined dependencies to an object via extend.

# File lib/core/extension/dependency.rb, line 38
def apply_extend(object)
  if prepend?
    object.singleton_class.prepend(@dependency) if implementation? || definition?
  elsif definition? || implementation?
    object.extend(@dependency)
  end
end
apply_include(object) click to toggle source
public

Apply the defined dependencies to an object via include.

# File lib/core/extension/dependency.rb, line 48
def apply_include(object)
  if prepend?
    object.prepend(@dependency) if implementation?
    object.singleton_class.prepend(@dependency) if definition?
  else
    object.include(@dependency) if implementation?
    object.extend(@dependency) if definition?
  end
end

Private Instance Methods

allowed_flag?(flag) click to toggle source
# File lib/core/extension/dependency.rb, line 78
        def allowed_flag?(flag)
  ALLOWED_FLAGS.include?(flag)
end
definition?() click to toggle source
# File lib/core/extension/dependency.rb, line 62
        def definition?
  @definition == true
end
enforce_allowed_flags(flags) click to toggle source
# File lib/core/extension/dependency.rb, line 70
        def enforce_allowed_flags(flags)
  flags.each do |flag|
    unless allowed_flag?(flag)
      raise ArgumentError, "Expected flag `#{flag.inspect}' to be one of: #{ALLOWED_FLAGS_STRING}"
    end
  end
end
implementation?() click to toggle source
# File lib/core/extension/dependency.rb, line 58
        def implementation?
  @implementation == true
end
prepend?() click to toggle source
# File lib/core/extension/dependency.rb, line 66
        def prepend?
  @prepend == true
end