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
19
# File 'lib/saper/core/stack.rb', line 16

def initialize(runtime, *items)
  @runtime  = runtime
  @items    = 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)

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

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

#copySaper::Stack

Returns a duplicate instance of self.

Returns:


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

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

#errorObject

TODO:

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

def error
  error? ? last : nil
end

#error?Boolean

TODO:

Returns:

  • (Boolean)

62
63
64
# File 'lib/saper/core/stack.rb', line 62

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

#filtered?Boolean

TODO:

Returns:

  • (Boolean)

57
58
59
# File 'lib/saper/core/stack.rb', line 57

def filtered?
  last.is_a?(Items::Nothing) && last.filtered?
end

#lastSaper::Item

Returns the result of the last action.

Returns:


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

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:


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

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

#sizeObject

TODO:

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

def size
  @items.size
end

#to_aArray<Saper::Item>

Returns an array of action results.

Returns:


68
69
70
# File 'lib/saper/core/stack.rb', line 68

def to_a
  @items.dup
end