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 ⇒ Object
- #error? ⇒ Boolean
- #filtered? ⇒ 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).
- #size ⇒ Object
-
#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 19 |
# File 'lib/saper/core/stack.rb', line 16 def initialize(runtime, *items) @runtime = runtime @items = 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.
35 36 37 |
# File 'lib/saper/core/stack.rb', line 35 def add(item) @items.push(item); self end |
#copy ⇒ Saper::Stack
Returns a duplicate instance of self.
28 29 30 |
# File 'lib/saper/core/stack.rb', line 28 def copy Stack.new(runtime.copy, *to_a) end |
#error ⇒ Object
TODO:
46 47 48 |
# File 'lib/saper/core/stack.rb', line 46 def error error? ? last : nil end |
#error? ⇒ Boolean
TODO:
62 63 64 |
# File 'lib/saper/core/stack.rb', line 62 def error? last.is_a?(Saper::Error) end |
#filtered? ⇒ Boolean
TODO:
57 58 59 |
# File 'lib/saper/core/stack.rb', line 57 def filtered? last.is_a?(Items::Nothing) && last.filtered? end |
#last ⇒ Saper::Item
Returns the result of the last action.
52 53 54 |
# File 'lib/saper/core/stack.rb', line 52 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).
41 42 43 |
# File 'lib/saper/core/stack.rb', line 41 def result error? ? Items::Nothing.new : last end |
#size ⇒ Object
TODO:
22 23 24 |
# File 'lib/saper/core/stack.rb', line 22 def size @items.size end |
#to_a ⇒ Array<Saper::Item>
Returns an array of action results.
68 69 70 |
# File 'lib/saper/core/stack.rb', line 68 def to_a @items.dup end |