Module: Dependencies

Defined in:
lib/dependencies.rb,
lib/dependencies/factory.rb,
lib/dependencies/dependency.rb,
lib/dependencies/repository.rb

Defined Under Namespace

Classes: Dependency, Factory, Repository

Class Method Summary collapse

Class Method Details

.[](*dependencies) ⇒ Object

Usage: “include Dependencies



12
13
14
15
16
17
18
19
# File 'lib/dependencies.rb', line 12

def [](*dependencies)
  class_dependencies = Factory.parse([*dependencies])

  # "include" doesn't know the class that did the include, however "included" happens immediately after.
  Repository.push(class_dependencies:)

  included_hook
end

.define_readers(dependencies, klass) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/dependencies.rb', line 48

def define_readers(dependencies, klass)
  dependencies.each do |dependency|
    var_name = name_from_namespace(dependency.var_name)

    klass.define_method(var_name) do
      instance_variable_get("@#{var_name}")
    end
  end
end

.included_hookObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dependencies.rb', line 21

def included_hook
  Module.new do
    def self.included(klass)
      klass.class_eval do
        # "include" doesn't know the class that did the include, however "included" happens immediately after.
        @dependencies = Repository.pop

        class << self
          attr_reader :dependencies
        end

        def initialize
          self.class.dependencies.each do |dependency|
            provider = Providers[dependency.provider_key]
            raise StandardError, "Provider #{dependency.provider_key} is missing or returning nil" if provider.nil?

            var_name = Dependencies.name_from_namespace(dependency.var_name)
            instance_variable_set("@#{var_name}", provider)
          end
        end

        Dependencies.define_readers(@dependencies, self)
      end
    end
  end
end

.name_from_namespace(namespace) ⇒ Object



58
59
60
61
62
# File 'lib/dependencies.rb', line 58

def name_from_namespace(namespace)
  return namespace.split('.').last if namespace.is_a?(String)

  namespace
end