Class: StackCommander::Stack

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/stack_commander/stack.rb

Instance Method Summary collapse

Constructor Details

#initialize(scope) ⇒ Stack

Returns a new instance of Stack.



10
11
12
13
# File 'lib/stack_commander/stack.rb', line 10

def initialize(scope)
  @queue = []
  @scope = scope
end

Instance Method Details

#<<(command) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/stack_commander/stack.rb', line 19

def <<(command)
  StackCommander::DependencyInjection.new(command).match!(@scope)

  @queue.push(command)

  self # for chaining awesomeness << cmd << cmd2 << cmd3
end

#callObject



27
28
29
30
31
# File 'lib/stack_commander/stack.rb', line 27

def call
  if command_class = @queue.shift
    run_command(command_class)
  end
end

#initialize_command(klass) ⇒ Object

TODO: maybe this should be responsibility of the command? it would make testing easier



34
35
36
37
38
39
40
41
42
# File 'lib/stack_commander/stack.rb', line 34

def initialize_command(klass)
  parameters = StackCommander::DependencyInjection.new(klass).extract(@scope)

  command = klass.allocate
  command.instance_variable_set :@scope, @scope
  command.__send__(:initialize, *parameters)

  command
end

#sizeObject



15
16
17
# File 'lib/stack_commander/stack.rb', line 15

def size
  @queue.length
end