Class: Farce::Transaction::Atom
- Inherits:
-
Abstract::Atom
- Object
- Abstract::Atom
- Farce::Transaction::Atom
- Defined in:
- lib/farce/transaction/atom.rb
Overview
An Atom that is part of a transaction.
Instance Method Summary collapse
-
#compare_and_set(expected, replacement, mode: nil) ⇒ Boolean
Atomically replace the current value if it matches the expected value.
-
#compare_by_identity? ⇒ Boolean
Whether comparisons use object identity instead of equality.
-
#store(value, mode: nil) { ... } ⇒ BasicObject?
Store a new value, waiting for any update in progress.
-
#store_if_absent(mode: nil) { ... } ⇒ BasicObject?
Compute and store a value if the current value is nil.
-
#swap(value, mode: nil) { ... } ⇒ BasicObject?
Replace the current value and return the previous value.
-
#update(mode: nil) {|current| ... } ⇒ BasicObject?
Atomically replace the current value with the result of a block.
-
#upsert(initial, mode: nil) {|current| ... } ⇒ BasicObject?
Store an initial value if the current value is nil, otherwise replace it with the result of a block.
-
#value ⇒ BasicObject?
(also: #get)
Return the current value without waiting for an update in progress.
-
#value=(value) ⇒ BasicObject?
Store a new value.
Methods included from Unshareable
#initialize, #ractor_shareable?
Methods inherited from Abstract::Atom
#blank?, #wait_until, #wait_until_changed, #wait_until_match, #wait_until_non_nil, #wait_until_value, #wait_while, #wait_while_match, #wait_while_value
Methods included from Internal::ValueSerialization
#as_extended_json, #as_json, #to_bson, #to_bson_normalized_value, #to_cbor, #to_json, #to_msgpack
Methods included from Abstract::Value
Methods included from Internal::Copyable
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Farce::Transaction::Wrapper
Instance Method Details
#compare_and_set(expected, replacement, mode: nil) ⇒ Boolean
Atomically replace the current value if it matches the expected value.
61 62 63 64 65 66 67 68 |
# File 'lib/farce/transaction/atom.rb', line 61 def compare_and_set(expected, replacement, mode: nil) write do next compared(@working.compare_and_set(expected, replacement)) unless @manager next compared(false) unless matches?(@working.value, expected, identity: @object.compare_by_identity?) @working.store(wrap(replacement, mode:)) true end end |
#compare_by_identity? ⇒ Boolean
Whether comparisons use object identity instead of equality.
13 |
# File 'lib/farce/transaction/atom.rb', line 13 def compare_by_identity? = access { @object.compare_by_identity? } |
#store(value, mode: nil) { ... } ⇒ BasicObject?
Store a new value, waiting for any update in progress.
28 29 30 |
# File 'lib/farce/transaction/atom.rb', line 28 def store(value, mode: nil) write { unwrap_stored(@working.store(wrap(value, mode:))) } end |
#store_if_absent(mode: nil) { ... } ⇒ BasicObject?
Compute and store a value if the current value is nil.
47 48 49 50 |
# File 'lib/farce/transaction/atom.rb', line 47 def store_if_absent(mode: nil) require_block!(block_given?) write { value.nil? ? store(yield, mode:) : value } end |
#swap(value, mode: nil) { ... } ⇒ BasicObject?
Replace the current value and return the previous value.
34 35 36 |
# File 'lib/farce/transaction/atom.rb', line 34 def swap(value, mode: nil) write { unwrap_stored(@working.swap(wrap(value, mode:))) } end |
#update(mode: nil) {|current| ... } ⇒ BasicObject?
Atomically replace the current value with the result of a block.
40 41 42 43 |
# File 'lib/farce/transaction/atom.rb', line 40 def update(mode: nil) require_block!(block_given?) write { unwrap_stored(@working.store(wrap(yield(unwrap_stored(@working.value)), mode:))) } end |
#upsert(initial, mode: nil) {|current| ... } ⇒ BasicObject?
Store an initial value if the current value is nil, otherwise replace it with the result of a block.
54 55 56 57 |
# File 'lib/farce/transaction/atom.rb', line 54 def upsert(initial, mode: nil) require_block!(block_given?) write { store(value.nil? ? initial : yield(value), mode:) } end |
#value ⇒ BasicObject? Also known as: get
Return the current value without waiting for an update in progress.
17 |
# File 'lib/farce/transaction/atom.rb', line 17 def value = access { unwrap_stored(@working.value) } |
#value=(value) ⇒ BasicObject?
Store a new value.
22 23 24 |
# File 'lib/farce/transaction/atom.rb', line 22 def value=(value) store(value) end |