Class: Temporalio::SearchAttributes

Inherits:
Object
  • Object
show all
Defined in:
lib/temporalio/search_attributes.rb

Overview

Collection of typed search attributes.

This is represented as a mapping of Key to object values. This is not a hash though it does have a few hash-like methods and can be converted to a hash via #to_h. In some situations, such as in workflows, this class is immutable for outside use.

Defined Under Namespace

Modules: IndexedValueType Classes: Key, Update

Instance Method Summary collapse

Constructor Details

#initialize(existing = nil) ⇒ SearchAttributes

Create a search attribute collection.

Parameters:



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.

Parameters:

Returns:

  • (Boolean)

    Whether equal.



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.

Parameters:

  • key (Key, String, Symbol)

    The key to find. If this is a Key, it will use key equality (i.e. name and type) to search. If this is a String, the type is not checked when finding the proper key.

Returns:

  • (Object, nil)

    Value if found or ‘nil` if not.



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.

Parameters:

  • key (Key)

    A key to set. This must be a Key and the value must be proper for the Temporalio::SearchAttributes::Key#type.

  • value (Object, nil)

    The value to set. If ‘nil`, the key is removed. The value must be proper for the `key`.

Raises:

  • (ArgumentError)


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

Parameters:

  • key (Key, String, Symbol)

    The key to delete. Regardless of whether this is a Key or a String, the key with the matching name will be deleted. This means a Key with a matching name but different type may be deleted.



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

#dupSearchAttributes

Returns Copy of the search attributes.

Returns:



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

#eachObject

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.

Returns:

  • (Boolean)

    Whether the set of attributes is empty.



245
246
247
# File 'lib/temporalio/search_attributes.rb', line 245

def empty?
  length.zero?
end

#lengthInteger Also known as: size

Returns Number of attributes.

Returns:

  • (Integer)

    Number of attributes.



250
251
252
# File 'lib/temporalio/search_attributes.rb', line 250

def length
  @raw_hash.length
end

#to_hHash<Key, Object>

Returns Copy of the search attributes as a hash.

Returns:

  • (Hash<Key, Object>)

    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.

Parameters:

Returns:



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