Class: Smplkit::Platform::ContextType

Inherits:
Object
  • Object
show all
Defined in:
lib/smplkit/platform/models.rb

Overview

A context type resource (e.g. “user”, “account”).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client = nil, name:, id: nil, attributes: nil, created_at: nil, updated_at: nil) ⇒ ContextType

Create a context-type instance.

Prefer client.platform.context_types.new(…) to build an unsaved context type; this constructor is also used internally to wrap server responses.

Parameters:

  • client (ContextTypesClient, nil) (defaults to: nil)

    Client used to persist and delete this context type. nil produces a detached instance that cannot save.

  • name (String)

    Display name shown in the Console.

  • id (String, nil) (defaults to: nil)

    Stable, human-readable identifier for the context type.

  • attributes (Hash, nil) (defaults to: nil)

    Known-attribute slots, keyed by attribute name, with a metadata dict per slot. Defaults to no declared attributes.

  • created_at (String, nil) (defaults to: nil)

    When the context type was created. Set on instances returned by the server; leave nil for unsaved ones.

  • updated_at (String, nil) (defaults to: nil)

    When the context type was last updated. Set on instances returned by the server; leave nil for unsaved ones.



192
193
194
195
196
197
198
199
# File 'lib/smplkit/platform/models.rb', line 192

def initialize(client = nil, name:, id: nil, attributes: nil, created_at: nil, updated_at: nil)
  @client = client
  @id = id
  @name = name
  @attributes = attributes ? deep_dup_attrs(attributes) : {}
  @created_at = created_at
  @updated_at = updated_at
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



171
172
173
# File 'lib/smplkit/platform/models.rb', line 171

def attributes
  @attributes
end

#created_atObject

Returns the value of attribute created_at.



171
172
173
# File 'lib/smplkit/platform/models.rb', line 171

def created_at
  @created_at
end

#idObject

Returns the value of attribute id.



171
172
173
# File 'lib/smplkit/platform/models.rb', line 171

def id
  @id
end

#nameObject

Returns the value of attribute name.



171
172
173
# File 'lib/smplkit/platform/models.rb', line 171

def name
  @name
end

#updated_atObject

Returns the value of attribute updated_at.



171
172
173
# File 'lib/smplkit/platform/models.rb', line 171

def updated_at
  @updated_at
end

Instance Method Details

#_apply(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



261
262
263
264
265
266
267
# File 'lib/smplkit/platform/models.rb', line 261

def _apply(other)
  @id = other.id
  @name = other.name
  @attributes = deep_dup_attrs(other.attributes)
  @created_at = other.created_at
  @updated_at = other.updated_at
end

#add_attribute(name, **metadata) ⇒ void

This method returns an undefined value.

Add a known-attribute slot. Local-only; call save to persist.

Parameters:

  • name (String)

    Attribute name to declare on this context type.

  • metadata (Hash)

    Arbitrary metadata stored for the attribute slot.



206
207
208
# File 'lib/smplkit/platform/models.rb', line 206

def add_attribute(name, **)
  @attributes[name] = stringify_meta()
end

#deletevoid Also known as: delete!

This method returns an undefined value.

Delete this context type from the server.

Raises:

  • (RuntimeError)

    If this context type was constructed without a client or id.



248
249
250
251
252
# File 'lib/smplkit/platform/models.rb', line 248

def delete
  raise "ContextType was constructed without a client or id; cannot delete" if @client.nil? || @id.nil?

  @client.delete(@id)
end

#remove_attribute(name) ⇒ void

This method returns an undefined value.

Remove a known-attribute slot. Local-only; call save to persist.

Parameters:

  • name (String)

    Attribute name to remove. A no-op if the attribute is not declared on this context type.



215
216
217
# File 'lib/smplkit/platform/models.rb', line 215

def remove_attribute(name)
  @attributes.delete(name)
end

#saveContextType Also known as: save!

Create or update this context type on the server.

Returns:

  • (ContextType)

    self, updated with the server response.

Raises:

  • (RuntimeError)

    If this context type was constructed without a client.



234
235
236
237
238
239
240
# File 'lib/smplkit/platform/models.rb', line 234

def save
  raise "ContextType was constructed without a client; cannot save" if @client.nil?

  other = @created_at.nil? ? @client._create(self) : @client._update(self)
  _apply(other)
  self
end

#to_sObject Also known as: inspect



255
256
257
# File 'lib/smplkit/platform/models.rb', line 255

def to_s
  "ContextType(id=#{@id.inspect}, name=#{@name.inspect})"
end

#update_attribute(name, **metadata) ⇒ void

This method returns an undefined value.

Replace a known-attribute slot’s metadata. Local-only; call save.

Parameters:

  • name (String)

    Attribute name whose metadata to replace.

  • metadata (Hash)

    New metadata for the attribute slot, replacing any existing metadata.



225
226
227
# File 'lib/smplkit/platform/models.rb', line 225

def update_attribute(name, **)
  @attributes[name] = stringify_meta()
end