Class: FakeServiceBus::FileDatabase
- Inherits:
-
Object
- Object
- FakeServiceBus::FileDatabase
- Defined in:
- lib/fake_servicebus/databases/file.rb
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete(key) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(filename) ⇒ FileDatabase
constructor
A new instance of FileDatabase.
- #load ⇒ Object
- #reset ⇒ Object
- #select(&block) ⇒ Object
- #transaction ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(filename) ⇒ FileDatabase
Returns a new instance of FileDatabase.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/fake_servicebus/databases/file.rb', line 8 def initialize(filename) @filename = filename @queue_objects = {} unless thread_safe_store? # before ruby 2.4, YAML::Store cannot be declared thread safe # # without that declaration, attempting to have some thread B enter a # store.transaction on the store while another thread A is in one # already will raise an error unnecessarily. # # to prevent this, we'll use our own mutex around store.transaction, # so only one thread can even _try_ to enter the transaction at a # time. @store_mutex = Mutex.new @store_mutex_owner = nil end end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
6 7 8 |
# File 'lib/fake_servicebus/databases/file.rb', line 6 def filename @filename end |
Instance Method Details
#[](key) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/fake_servicebus/databases/file.rb', line 70 def [](key) value = storage[key] if value deserialize(key) else value end end |
#[]=(key, value) ⇒ Object
66 67 68 |
# File 'lib/fake_servicebus/databases/file.rb', line 66 def []=(key, value) storage[key] = value.to_yaml end |
#delete(key) ⇒ Object
92 93 94 95 |
# File 'lib/fake_servicebus/databases/file.rb', line 92 def delete(key) @queue_objects.delete(key) storage.delete(key) end |
#each(&block) ⇒ Object
79 80 81 82 83 |
# File 'lib/fake_servicebus/databases/file.rb', line 79 def each(&block) storage.each do |key, value| yield key, deserialize(key) end end |
#load ⇒ Object
26 27 28 29 30 |
# File 'lib/fake_servicebus/databases/file.rb', line 26 def load transaction do store["queues"] ||= {} end end |
#reset ⇒ Object
59 60 61 62 63 64 |
# File 'lib/fake_servicebus/databases/file.rb', line 59 def reset transaction do store["queues"] = {} end @queue_objects = {} end |
#select(&block) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/fake_servicebus/databases/file.rb', line 85 def select(&block) new_list = storage.select do |key, value| yield key, deserialize(key) end Hash[new_list.map { |key, value| [key, deserialize(key)] }] end |
#transaction ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/fake_servicebus/databases/file.rb', line 32 def transaction if thread_safe_store? || store_mutex_owned? # if we already own the store mutex, we can expect the next line to # raise (appropriately) when we try to nest transactions in the store. # but if we took the other branch, the # @store_mutex.synchronize call # would self-deadlock before we could raise the error. store.transaction do yield end else # we still need to use an inner store.transaction block because it does # more than just lock synchronization. it's unfortunately inefficient, # but this isn't a production-oriented library. @store_mutex.synchronize do begin # allows us to answer `store_mutex_owned?` above @store_mutex_owner = Thread.current store.transaction do yield end ensure @store_mutex_owner = nil end end end end |
#values ⇒ Object
97 98 99 100 101 |
# File 'lib/fake_servicebus/databases/file.rb', line 97 def values storage.map { |key, value| deserialize(key) } end |