Class: GitQueue::Queue
- Inherits:
-
Object
- Object
- GitQueue::Queue
- Defined in:
- lib/git_queue/queue.rb
Overview
Queue
Defined Under Namespace
Classes: Configure
Constant Summary collapse
- @@push_message =
-> (item) { "Add item #{item}" }
- @@pop_message =
-> (item) { "Pop item #{item}" }
- @@switch_message =
-> (left, right) { "Switch #{left} with #{right}" }
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #down(index) ⇒ Object
- #history(length = nil) ⇒ Object
-
#initialize(name) ⇒ Queue
constructor
A new instance of Queue.
- #pop ⇒ Object
- #push(task) ⇒ Object (also: #<<)
- #queue ⇒ Object
- #switch(l_index, r_index) ⇒ Object
- #up(index) ⇒ Object
Constructor Details
#initialize(name) ⇒ Queue
Returns a new instance of Queue.
28 29 30 31 |
# File 'lib/git_queue/queue.rb', line 28 def initialize(name) @name = name @queue = [] end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
26 27 28 |
# File 'lib/git_queue/queue.rb', line 26 def name @name end |
Instance Method Details
#down(index) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/git_queue/queue.rb', line 62 def down(index) sync return queue if index < 0 || @queue.size - 2 < index _switch(index, index + 1) store(@@switch_message.call(@queue[index + 1], @queue[index])) end |
#history(length = nil) ⇒ Object
69 70 71 |
# File 'lib/git_queue/queue.rb', line 69 def history(length = nil) @storage.history(length) end |
#pop ⇒ Object
40 41 42 43 44 45 |
# File 'lib/git_queue/queue.rb', line 40 def pop sync ret = @queue.shift store(@@pop_message.call(ret)) ret end |
#push(task) ⇒ Object Also known as: <<
33 34 35 36 37 |
# File 'lib/git_queue/queue.rb', line 33 def push(task) sync @queue << task store(@@push_message.call(task)) end |
#queue ⇒ Object
73 74 75 76 |
# File 'lib/git_queue/queue.rb', line 73 def queue sync @queue.dup end |
#switch(l_index, r_index) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/git_queue/queue.rb', line 47 def switch(l_index, r_index) sync return queue if l_index < 0 || @queue.size - 1 < l_index return queue if r_index < 0 || @queue.size - 1 < r_index _switch(l_index, r_index) store(@@switch_message.call(@queue[r_index], @queue[l_index])) end |
#up(index) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/git_queue/queue.rb', line 55 def up(index) sync return queue if index <= 0 || @queue.size - 1 < index _switch(index, index - 1) store(@@switch_message.call(@queue[index - 1], @queue[index])) end |