Class: Basket::BackendAdapter::MemoryBackend
- Inherits:
-
Basket::BackendAdapter
- Object
- Basket::BackendAdapter
- Basket::BackendAdapter::MemoryBackend
- Defined in:
- lib/basket/backend_adapter/memory_backend.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
- #clear(queue) ⇒ Object
-
#initialize ⇒ MemoryBackend
constructor
A new instance of MemoryBackend.
- #length(queue) ⇒ Object
- #push(queue, data) ⇒ Object
- #read(queue) ⇒ Object
- #remove(queue, id) ⇒ Object
- #search(queue, &block) ⇒ Object
Constructor Details
#initialize ⇒ MemoryBackend
Returns a new instance of MemoryBackend.
4 5 6 |
# File 'lib/basket/backend_adapter/memory_backend.rb', line 4 def initialize @data = {} end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
8 9 10 |
# File 'lib/basket/backend_adapter/memory_backend.rb', line 8 def data @data end |
Instance Method Details
#clear(queue) ⇒ Object
36 37 38 |
# File 'lib/basket/backend_adapter/memory_backend.rb', line 36 def clear(queue) @data[queue] = [] end |
#length(queue) ⇒ Object
15 16 17 18 19 |
# File 'lib/basket/backend_adapter/memory_backend.rb', line 15 def length(queue) return 0 if @data[queue].nil? @data[queue].length end |
#push(queue, data) ⇒ Object
10 11 12 13 |
# File 'lib/basket/backend_adapter/memory_backend.rb', line 10 def push(queue, data) @data[queue] = [] if @data[queue].nil? @data[queue] <<= data end |
#read(queue) ⇒ Object
21 22 23 |
# File 'lib/basket/backend_adapter/memory_backend.rb', line 21 def read(queue) @data[queue] end |
#remove(queue, id) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/basket/backend_adapter/memory_backend.rb', line 29 def remove(queue, id) index_of_element_to_delete = @data[queue].index { |element| element.id == id } @data[queue].delete_at(index_of_element_to_delete) rescue nil end |
#search(queue, &block) ⇒ Object
25 26 27 |
# File 'lib/basket/backend_adapter/memory_backend.rb', line 25 def search(queue, &block) @data[queue].select { |element| block.call(element.data) } end |