Class: Temporalio::SearchAttributes
- Inherits:
-
Object
- Object
- Temporalio::SearchAttributes
- Defined in:
- lib/temporalio/search_attributes.rb
Overview
Defined Under Namespace
Modules: IndexedValueType Classes: Key, Update
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Get a search attribute value for a key.
-
#[]=(key, value) ⇒ Object
Set a search attribute value for a key.
-
#delete(key) ⇒ Object
Delete a search attribute key.
-
#dup ⇒ SearchAttributes
Copy of the search attributes.
-
#each ⇒ Object
Like Hash#each.
-
#empty? ⇒ Boolean
Whether the set of attributes is empty.
-
#initialize(existing = nil) ⇒ SearchAttributes
constructor
Create a search attribute collection.
-
#length ⇒ Integer
(also: #size)
Number of attributes.
-
#to_h ⇒ Hash<Key, Object>
Copy of the search attributes as a hash.
-
#update(*updates) ⇒ SearchAttributes
Return a new search attributes collection with updates applied.
-
#update!(*updates) ⇒ Object
Update this search attribute collection with given updates.
Constructor Details
#initialize(existing = nil) ⇒ SearchAttributes
Create a search attribute collection.
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/temporalio/search_attributes.rb', line 133 def initialize(existing = nil) if existing.nil? @raw_hash = {} elsif existing.is_a?(SearchAttributes) @raw_hash = existing.to_h elsif existing.is_a?(Hash) @raw_hash = {} existing.each { |key, value| self[key] = value } else raise ArgumentError, 'Existing must be nil, a SearchAttributes instance, or a valid Hash' end end |
Instance Method Details
#[](key) ⇒ Object?
Get a search attribute value for a key.
168 169 170 171 172 173 174 175 176 177 |
# File 'lib/temporalio/search_attributes.rb', line 168 def [](key) # Key must be a Key or a string if key.is_a?(Key) @raw_hash[key] elsif key.is_a?(String) @raw_hash.find { |hash_key, _| hash_key.name == key }&.last else raise ArgumentError, 'Key must be a key or string' end end |
#[]=(key, value) ⇒ Object
Set a search attribute value for a key. This will replace any existing value for the {Key#name }regardless of Temporalio::SearchAttributes::Key#type.
151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/temporalio/search_attributes.rb', line 151 def []=(key, value) # Key must be a Key raise ArgumentError, 'Key must be a key' unless key.is_a?(Key) key.validate_value(value) unless value.nil? # Remove any key with the same name and set delete(key) # We only set the value if it's non-nil, otherwise it's a delete @raw_hash[key] = value unless value.nil? end |
#delete(key) ⇒ Object
Delete a search attribute key
183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/temporalio/search_attributes.rb', line 183 def delete(key) # Key must be a Key or a string, but we delete all values for the # name no matter what name = if key.is_a?(Key) key.name elsif key.is_a?(String) key else raise ArgumentError, 'Key must be a key or string' end @raw_hash.delete_if { |hash_key, _| hash_key.name == name } end |
#dup ⇒ SearchAttributes
Returns Copy of the search attributes.
207 208 209 |
# File 'lib/temporalio/search_attributes.rb', line 207 def dup SearchAttributes.new(self) end |
#each ⇒ Object
Like Hash#each.
197 198 199 |
# File 'lib/temporalio/search_attributes.rb', line 197 def each(&) @raw_hash.each(&) end |
#empty? ⇒ Boolean
Returns Whether the set of attributes is empty.
212 213 214 |
# File 'lib/temporalio/search_attributes.rb', line 212 def empty? length.zero? end |
#length ⇒ Integer Also known as: size
Returns Number of attributes.
217 218 219 |
# File 'lib/temporalio/search_attributes.rb', line 217 def length @raw_hash.length end |
#to_h ⇒ Hash<Key, Object>
Returns Copy of the search attributes as a hash.
202 203 204 |
# File 'lib/temporalio/search_attributes.rb', line 202 def to_h @raw_hash.dup end |
#update(*updates) ⇒ SearchAttributes
Return a new search attributes collection with updates applied.
227 228 229 230 231 |
# File 'lib/temporalio/search_attributes.rb', line 227 def update(*updates) attrs = dup attrs.update!(*updates) attrs end |
#update!(*updates) ⇒ Object
Update this search attribute collection with given updates.
236 237 238 239 240 241 242 |
# File 'lib/temporalio/search_attributes.rb', line 236 def update!(*updates) updates.each do |update| raise ArgumentError, 'Update must be an update' unless update.is_a?(Update) self[update.key] = update.value end end |