Class: Saper::Stack
- Inherits:
-
Object
- Object
- Saper::Stack
- Defined in:
- lib/saper/core/stack.rb
Instance Attribute Summary collapse
-
#items ⇒ Array<Saper::Item>
readonly
Returns all items in the stack.
-
#runtime ⇒ Saper::Runtime
readonly
Returns Runtime instance used by the stack.
Instance Method Summary collapse
-
#add(item) ⇒ self
Adds action result to the end of the stack and returns self.
-
#copy ⇒ Saper::Stack
Returns a duplicate instance of self.
- #error? ⇒ Boolean
-
#initialize(runtime, *items) ⇒ Saper::Stack
constructor
Returns a new Stack instance.
-
#last ⇒ Saper::Item
Returns the result of the last action.
-
#result ⇒ Saper::Item
Returns the result of the last action or nil (in case of an error).
-
#to_a ⇒ Array<Saper::Item>
Returns an array of action results.
Constructor Details
#initialize(runtime, *items) ⇒ Saper::Stack
Returns a new Stack instance.
16 17 18 |
# File 'lib/saper/core/stack.rb', line 16 def initialize(runtime, *items) @runtime, @items = runtime, items.flatten end |
Instance Attribute Details
#items ⇒ Array<Saper::Item> (readonly)
Returns all items in the stack.
10 11 12 |
# File 'lib/saper/core/stack.rb', line 10 def items @items end |
#runtime ⇒ Saper::Runtime (readonly)
Returns Runtime instance used by the stack
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.
29 30 31 |
# File 'lib/saper/core/stack.rb', line 29 def add(item) @items.push(item); self end |
#copy ⇒ Saper::Stack
Returns a duplicate instance of self.
22 23 24 |
# File 'lib/saper/core/stack.rb', line 22 def copy Stack.new(runtime.copy, *to_a) end |
#error? ⇒ Boolean
TODO:
46 47 48 |
# File 'lib/saper/core/stack.rb', line 46 def error? last.is_a?(Saper::Error) end |
#last ⇒ Saper::Item
Returns the result of the last action.
41 42 43 |
# File 'lib/saper/core/stack.rb', line 41 def last @items.last || Items::Nothing.new end |
#result ⇒ Saper::Item
Returns the result of the last action or nil (in case of an error).
35 36 37 |
# File 'lib/saper/core/stack.rb', line 35 def result error? ? Items::Nothing.new : last end |
#to_a ⇒ Array<Saper::Item>
Returns an array of action results.
52 53 54 |
# File 'lib/saper/core/stack.rb', line 52 def to_a @items.dup end |