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



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

def initialize(klass)
  @klass = klass
end

Instance Method Details

#extract(scope) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/stack_commander/dependency_injection.rb', line 28

def extract(scope)
  match!(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

#match!(scope) ⇒ Object

Raises:



24
25
26
# File 'lib/stack_commander/dependency_injection.rb', line 24

def match!(scope)
  raise InvalidScope, scope unless matches?(scope)
end

#matches?(scope) ⇒ 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