Class: Saper::Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/saper/core/stack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runtime, *items) ⇒ Saper::Stack

Returns a new Stack instance.

Parameters:



16
17
18
# File 'lib/saper/core/stack.rb', line 16

def initialize(runtime, *items)
  @runtime, @items = runtime, items.flatten
end

Instance Attribute Details

#itemsArray<Saper::Item> (readonly)

Returns all items in the stack.

Returns:



10
11
12
# File 'lib/saper/core/stack.rb', line 10

def items
  @items
end

#runtimeSaper::Runtime (readonly)

Returns Runtime instance used by the stack

Returns:



6
7
8
# File 'lib/saper/core/stack.rb', line 6

def runtime
  @runtime
end

Instance Method Details

#add(item) ⇒ self

Adds action result to the end of the stack and returns self.

Parameters:

Returns:

  • (self)


29
30
31
# File 'lib/saper/core/stack.rb', line 29

def add(item)
  @items.push(item); self
end

#copySaper::Stack

Returns a duplicate instance of self.

Returns:



22
23
24
# File 'lib/saper/core/stack.rb', line 22

def copy
  Stack.new(runtime.copy, *to_a)
end

#error?Boolean

TODO:

Returns:

  • (Boolean)


46
47
48
# File 'lib/saper/core/stack.rb', line 46

def error?
  last.is_a?(Saper::Error)
end

#lastSaper::Item

Returns the result of the last action.

Returns:



41
42
43
# File 'lib/saper/core/stack.rb', line 41

def last
  @items.last || Items::Nothing.new
end

#resultSaper::Item

Returns the result of the last action or nil (in case of an error).

Returns:



35
36
37
# File 'lib/saper/core/stack.rb', line 35

def result
  error? ? Items::Nothing.new : last
end

#to_aArray<Saper::Item>

Returns an array of action results.

Returns:



52
53
54
# File 'lib/saper/core/stack.rb', line 52

def to_a
  @items.dup
end