Class: StackCommander::DependencyInjection

Inherits:
Object
  • Object
show all
Defined in:
lib/stack_commander/dependency_injection.rb

Constant Summary collapse

InvalidScope =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ DependencyInjection

Returns a new instance of DependencyInjection.



5
6
7
# File 'lib/stack_commander/dependency_injection.rb', line 5

def initialize(klass)
  @klass = klass
end

Instance Method Details

#extract(scope) ⇒ Object

Raises:



24
25
26
27
28
29
30
# File 'lib/stack_commander/dependency_injection.rb', line 24

def extract(scope)
  raise InvalidScope, scope unless matches?(scope)

  required_parameters.map do |param|
    scope.public_send(param)
  end
end

#initialize_parametersObject



9
10
11
# File 'lib/stack_commander/dependency_injection.rb', line 9

def initialize_parameters
  @klass.instance_method(:initialize).parameters
end

#matches?(scope) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/stack_commander/dependency_injection.rb', line 18

def matches?(scope)
  required_parameters.all? do |param|
    scope.respond_to?(param)
  end
end

#required_parametersObject



13
14
15
16
# File 'lib/stack_commander/dependency_injection.rb', line 13

def required_parameters
  _, parameters = initialize_parameters.transpose
  Array(parameters)
end