Class: QueueIt::Queue
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- QueueIt::Queue
- Defined in:
- app/models/queue_it/queue.rb
Instance Method Summary collapse
- #empty? ⇒ Boolean
- #get_next_in_queue_generic ⇒ Object
- #get_next_in_queue_with_length_two ⇒ Object
- #head_node ⇒ Object
- #one_node? ⇒ Boolean
- #push_in_head(nodable) ⇒ Object
- #push_in_tail(nodable) ⇒ Object
- #push_node_when_queue_length_is_one(nodable, in_head) ⇒ Object
- #push_node_when_queue_length_is_zero(nodable) ⇒ Object
- #size ⇒ Object
- #tail_node ⇒ Object
- #two_nodes? ⇒ Boolean
Instance Method Details
#empty? ⇒ Boolean
18 19 20 |
# File 'app/models/queue_it/queue.rb', line 18 def empty? size.zero? end |
#get_next_in_queue_generic ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/models/queue_it/queue.rb', line 43 def get_next_in_queue_generic next_node = ActiveRecord::Base.transaction do lock! old_head_node = head_node.lock! old_second_node = old_head_node.child_node.lock! old_tail_node = tail_node.lock! nodes.where.not(kind: :any).find_each { |node| node.update!(kind: :any) } old_head_node.update!(kind: :tail, parent_node: old_tail_node) old_second_node.update!(kind: :head, parent_node: nil) old_head_node end next_node end |
#get_next_in_queue_with_length_two ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/models/queue_it/queue.rb', line 30 def get_next_in_queue_with_length_two next_node = ActiveRecord::Base.transaction do lock! old_head_node = head_node.lock! old_tail_node = tail_node.lock! nodes.where.not(kind: :any).each { |node| node.update!(kind: :any) } old_head_node.update!(kind: :tail, parent_node: old_tail_node) old_tail_node.update!(kind: :head, parent_node: nil) old_head_node end next_node end |
#head_node ⇒ Object
6 7 8 |
# File 'app/models/queue_it/queue.rb', line 6 def head_node nodes.find_by(kind: :head) end |
#one_node? ⇒ Boolean
22 23 24 |
# File 'app/models/queue_it/queue.rb', line 22 def one_node? size == 1 end |
#push_in_head(nodable) ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'app/models/queue_it/queue.rb', line 75 def push_in_head(nodable) ActiveRecord::Base.transaction do lock! old_head_node = head_node.lock! kind = one_node? ? :tail : :any old_head_node.update!(kind: kind) new_head_node = nodes.create!(nodable: nodable, kind: :head) old_head_node.update!(parent_node: new_head_node) end end |
#push_in_tail(nodable) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'app/models/queue_it/queue.rb', line 86 def push_in_tail(nodable) ActiveRecord::Base.transaction do lock! old_tail_node = tail_node.lock! old_tail_node.update!(kind: :any) nodes.create!(nodable: nodable, kind: :tail, parent_node: old_tail_node) end end |
#push_node_when_queue_length_is_one(nodable, in_head) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'app/models/queue_it/queue.rb', line 64 def push_node_when_queue_length_is_one(nodable, in_head) if in_head push_in_head(nodable) else ActiveRecord::Base.transaction do lock! nodes.create!(nodable: nodable, kind: :tail, parent_node: head_node) end end end |
#push_node_when_queue_length_is_zero(nodable) ⇒ Object
57 58 59 60 61 62 |
# File 'app/models/queue_it/queue.rb', line 57 def push_node_when_queue_length_is_zero(nodable) ActiveRecord::Base.transaction do lock! nodes.create!(nodable: nodable, kind: :head) end end |
#size ⇒ Object
14 15 16 |
# File 'app/models/queue_it/queue.rb', line 14 def size nodes.size end |
#tail_node ⇒ Object
10 11 12 |
# File 'app/models/queue_it/queue.rb', line 10 def tail_node nodes.find_by(kind: :tail) end |
#two_nodes? ⇒ Boolean
26 27 28 |
# File 'app/models/queue_it/queue.rb', line 26 def two_nodes? size == 2 end |