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
-
#==(other) ⇒ Boolean
Check equality.
-
#[](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.
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/temporalio/search_attributes.rb', line 159 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
#==(other) ⇒ Boolean
Check equality.
258 259 260 |
# File 'lib/temporalio/search_attributes.rb', line 258 def ==(other) other.is_a?(SearchAttributes) && @raw_hash == other._raw_hash end |
#[](key) ⇒ Object?
Get a search attribute value for a key.
195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/temporalio/search_attributes.rb', line 195 def [](key) # Key must be a Key or a string case key when Key @raw_hash[key] when String, Symbol @raw_hash.find { |hash_key, _| hash_key.name == key.to_s }&.last else raise ArgumentError, 'Key must be a key or string/symbol' 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.
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/temporalio/search_attributes.rb', line 177 def []=(key, value) _assert_mutations_enabled # 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
212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/temporalio/search_attributes.rb', line 212 def delete(key) _assert_mutations_enabled # Key must be a Key or a string, but we delete all values for the # name no matter what name = case key when Key key.name when String, Symbol key.to_s else raise ArgumentError, 'Key must be a key or string/symbol' end @raw_hash.delete_if { |hash_key, _| hash_key.name == name } end |
#dup ⇒ SearchAttributes
Returns Copy of the search attributes.
238 239 240 241 242 |
# File 'lib/temporalio/search_attributes.rb', line 238 def dup attrs = SearchAttributes.new(self) attrs._disable_mutations = false attrs end |
#each ⇒ Object
Like Hash#each.
228 229 230 |
# File 'lib/temporalio/search_attributes.rb', line 228 def each(&) @raw_hash.each(&) end |
#empty? ⇒ Boolean
Returns Whether the set of attributes is empty.
245 246 247 |
# File 'lib/temporalio/search_attributes.rb', line 245 def empty? length.zero? end |
#length ⇒ Integer Also known as: size
Returns Number of attributes.
250 251 252 |
# File 'lib/temporalio/search_attributes.rb', line 250 def length @raw_hash.length end |
#to_h ⇒ Hash<Key, Object>
Returns Copy of the search attributes as a hash.
233 234 235 |
# File 'lib/temporalio/search_attributes.rb', line 233 def to_h @raw_hash.dup end |
#update(*updates) ⇒ SearchAttributes
Return a new search attributes collection with updates applied.
268 269 270 271 272 273 |
# File 'lib/temporalio/search_attributes.rb', line 268 def update(*updates) _assert_mutations_enabled attrs = dup attrs.update!(*updates) attrs end |
#update!(*updates) ⇒ Object
Update this search attribute collection with given updates.
278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/temporalio/search_attributes.rb', line 278 def update!(*updates) _assert_mutations_enabled updates.each do |update| raise ArgumentError, 'Update must be an update' unless update.is_a?(Update) if update.value.nil? delete(update.key) else self[update.key] = update.value end end end |