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

Returns a new instance of ContextType.



114
115
116
117
118
119
120
121
# File 'lib/smplkit/platform/models.rb', line 114

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.



112
113
114
# File 'lib/smplkit/platform/models.rb', line 112

def attributes
  @attributes
end

#created_atObject

Returns the value of attribute created_at.



112
113
114
# File 'lib/smplkit/platform/models.rb', line 112

def created_at
  @created_at
end

#idObject

Returns the value of attribute id.



112
113
114
# File 'lib/smplkit/platform/models.rb', line 112

def id
  @id
end

#nameObject

Returns the value of attribute name.



112
113
114
# File 'lib/smplkit/platform/models.rb', line 112

def name
  @name
end

#updated_atObject

Returns the value of attribute updated_at.



112
113
114
# File 'lib/smplkit/platform/models.rb', line 112

def updated_at
  @updated_at
end

Instance Method Details

#_apply(other) ⇒ Object



161
162
163
164
165
166
167
# File 'lib/smplkit/platform/models.rb', line 161

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) ⇒ Object

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



124
125
126
# File 'lib/smplkit/platform/models.rb', line 124

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

#deleteObject Also known as: delete!

Delete this context type from the server.



149
150
151
152
153
# File 'lib/smplkit/platform/models.rb', line 149

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) ⇒ Object

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



129
130
131
# File 'lib/smplkit/platform/models.rb', line 129

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

#saveObject Also known as: save!

Create or update this context type on the server.



139
140
141
142
143
144
145
# File 'lib/smplkit/platform/models.rb', line 139

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



156
157
158
# File 'lib/smplkit/platform/models.rb', line 156

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

#update_attribute(name, **metadata) ⇒ Object

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



134
135
136
# File 'lib/smplkit/platform/models.rb', line 134

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