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 frozen.

Defined Under Namespace

Modules: IndexedValueType Classes: Key, Update

Instance Method Summary collapse

Constructor Details

#initialize(existing = nil) ⇒ SearchAttributes

Create a search attribute collection.

Parameters:



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.

Parameters:

  • key (Key, String)

    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.



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.

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)


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

Parameters:

  • key (Key, String)

    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.



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

#dupSearchAttributes

Returns Copy of the search attributes.

Returns:



207
208
209
# File 'lib/temporalio/search_attributes.rb', line 207

def dup
  SearchAttributes.new(self)
end

#eachObject

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.

Returns:

  • (Boolean)

    Whether the set of attributes is empty.



212
213
214
# File 'lib/temporalio/search_attributes.rb', line 212

def empty?
  length.zero?
end

#lengthInteger Also known as: size

Returns Number of attributes.

Returns:

  • (Integer)

    Number of attributes.



217
218
219
# File 'lib/temporalio/search_attributes.rb', line 217

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.



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.

Parameters:

Returns:



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