Class: Smplkit::Platform::Service

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

Overview

Service resource (sync). Mutate fields, then call save.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create a service instance.

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

Parameters:

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

    Client used to persist and delete this service. 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 service.

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

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

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

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



122
123
124
125
126
127
128
# File 'lib/smplkit/platform/models.rb', line 122

def initialize(client = nil, name:, id: nil, created_at: nil, updated_at: nil)
  @client = client
  @id = id
  @name = name
  @created_at = created_at
  @updated_at = updated_at
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



106
107
108
# File 'lib/smplkit/platform/models.rb', line 106

def created_at
  @created_at
end

#idObject

Returns the value of attribute id.



106
107
108
# File 'lib/smplkit/platform/models.rb', line 106

def id
  @id
end

#nameObject

Returns the value of attribute name.



106
107
108
# File 'lib/smplkit/platform/models.rb', line 106

def name
  @name
end

#updated_atObject

Returns the value of attribute updated_at.



106
107
108
# File 'lib/smplkit/platform/models.rb', line 106

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.



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

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

#deletevoid Also known as: delete!

This method returns an undefined value.

Delete this service from the server.

Raises:

  • (RuntimeError)

    If this service was constructed without a client or id.



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

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

  @client.delete(@id)
end

#saveService Also known as: save!

Create or update this service on the server.

Returns:

  • (Service)

    self, updated with the server response.

Raises:

  • (RuntimeError)

    If this service was constructed without a client.



134
135
136
137
138
139
140
# File 'lib/smplkit/platform/models.rb', line 134

def save
  raise "Service 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



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

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