Class: Y::Map
Overview
A map can be used to store and retrieve key-value pairs.
The map is the replicated counterpart to a Hash. It supports a subset of the Hash operations, like adding, getting and deleting values by key.
Someone should not instantiate a map directly, but use Doc#get_map instead.
Instance Attribute Summary collapse
-
#document ⇒ Y::Doc
The document this map belongs to.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ void
-
#attach(callback, &block) ⇒ Integer
Attach a listener to get notified about any changes to the map.
-
#clear ⇒ Self
Removes all map entries.
-
#delete(key) ⇒ void
Deletes the entry for the given key and returns its associated value.
-
#detach(subscription_id) ⇒ void
Detach listener.
- #each(&block) ⇒ void
-
#initialize(doc = nil) ⇒ Map
constructor
Create a new map instance.
- #key?(key) ⇒ true, false (also: #has_key?)
-
#size ⇒ Integer
Returns size of map.
-
#to_h ⇒ Hash
Returns a Hash representation of this map.
-
#to_json(*_args) ⇒ String
Returns a JSON representation of map.
-
#ymap_clear(tx) ⇒ Object
Removes all key-value pairs from Map.
-
#ymap_contains(tx, key) ⇒ Object
Check if a certain key is in the Map.
-
#ymap_each(tx, proc) ⇒ Object
Iterates over all key-value pairs in Map by calling the provided proc with the key and the value as arguments.
-
#ymap_get(tx, key) ⇒ Object
Returns stored value for key or nil if none is present.
-
#ymap_insert(tx, key, value) ⇒ Object
Insert value for key.
- #ymap_observe(callback) ⇒ Integer
-
#ymap_remove(tx, key) ⇒ Object
Removes key-value pair from Map if key exists.
-
#ymap_size(tx) ⇒ Object
Returns number of key-value pairs stored in map.
-
#ymap_to_h(tx) ⇒ Object
Returns a Hash representation of the Map.
- #ymap_unobserve(subscription_id) ⇒ void
Constructor Details
Instance Attribute Details
#document ⇒ Y::Doc
Returns The document this map belongs to.
26 27 28 |
# File 'lib/y/map.rb', line 26 def document @document end |
Instance Method Details
#[](key) ⇒ Object
107 108 109 |
# File 'lib/y/map.rb', line 107 def [](key) document.current_transaction { |tx| ymap_get(tx, key) } end |
#[]=(key, val) ⇒ void
This method returns an undefined value.
112 113 114 |
# File 'lib/y/map.rb', line 112 def []=(key, val) document.current_transaction { |tx| ymap_insert(tx, key, val) } end |
#attach(callback, &block) ⇒ Integer
Attach a listener to get notified about any changes to the map
42 43 44 45 46 |
# File 'lib/y/map.rb', line 42 def attach(callback, &block) return ymap_observe(callback) unless callback.nil? ymap_observe(block.to_proc) unless block.nil? end |
#clear ⇒ Self
Removes all map entries
51 52 53 54 |
# File 'lib/y/map.rb', line 51 def clear document.current_transaction { |tx| ymap_clear(tx) } self end |
#delete(key) ⇒ void
This method returns an undefined value.
Deletes the entry for the given key and returns its associated value.
75 76 77 78 79 80 81 82 |
# File 'lib/y/map.rb', line 75 def delete(key) value = document.current_transaction { |tx| ymap_remove(tx, key) } if block_given? && key?(key) yield key else value end end |
#detach(subscription_id) ⇒ void
This method returns an undefined value.
Detach listener
90 91 92 |
# File 'lib/y/map.rb', line 90 def detach(subscription_id) ymap_unobserve(subscription_id) end |
#each(&block) ⇒ void
This method returns an undefined value.
95 96 97 |
# File 'lib/y/map.rb', line 95 def each(&block) document.current_transaction { |tx| ymap_each(tx, block) } end |
#key?(key) ⇒ true, false Also known as: has_key?
100 101 102 |
# File 'lib/y/map.rb', line 100 def key?(key) document.current_transaction { |tx| ymap_contains(tx, key) } end |
#size ⇒ Integer
Returns size of map
119 120 121 |
# File 'lib/y/map.rb', line 119 def size document.current_transaction { |tx| ymap_size(tx) } end |
#to_h ⇒ Hash
Returns a Hash representation of this map
126 127 128 |
# File 'lib/y/map.rb', line 126 def to_h document.current_transaction { |tx| ymap_to_h(tx) } end |
#to_json(*_args) ⇒ String
Returns a JSON representation of map
133 134 135 |
# File 'lib/y/map.rb', line 133 def to_json(*_args) to_h.to_json end |
#ymap_clear(tx) ⇒ Object
Removes all key-value pairs from Map
|
# File 'lib/y/map.rb', line 137
|
#ymap_contains(tx, key) ⇒ Object
Check if a certain key is in the Map
|
# File 'lib/y/map.rb', line 142
|
#ymap_each(tx, proc) ⇒ Object
Iterates over all key-value pairs in Map by calling the provided proc with the key and the value as arguments.
|
# File 'lib/y/map.rb', line 149
|
#ymap_get(tx, key) ⇒ Object
Returns stored value for key or nil if none is present
|
# File 'lib/y/map.rb', line 156
|
#ymap_insert(tx, key, value) ⇒ Object
Insert value for key. In case the key already exists, the previous value will be overwritten.
|
# File 'lib/y/map.rb', line 163
|
#ymap_observe(callback) ⇒ Integer
|
# File 'lib/y/map.rb', line 171
|
#ymap_remove(tx, key) ⇒ Object
Removes key-value pair from Map if key exists.
|
# File 'lib/y/map.rb', line 176
|
#ymap_size(tx) ⇒ Object
Returns number of key-value pairs stored in map
|
# File 'lib/y/map.rb', line 182
|
#ymap_to_h(tx) ⇒ Object
Returns a Hash representation of the Map
|
# File 'lib/y/map.rb', line 188
|
#ymap_unobserve(subscription_id) ⇒ void
This method returns an undefined value.
|
# File 'lib/y/map.rb', line 194
|