Class: Smplkit::Config::Config

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

Overview

A configuration resource — a typed bag of items with per-environment overrides.

Provides management operations (save, set_string/set_number/…) and runtime evaluation via get on the parent ConfigClient.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client = nil, key:, id: nil, name: nil, description: nil, parent_id: nil, items: nil, environments: nil, created_at: nil, updated_at: nil) ⇒ Config

Returns a new instance of Config.

Parameters:

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

    The owning client, or nil for a detached model that cannot save or delete.

  • key (String)

    The config identifier (slug).

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

    The server-assigned id, or nil for an unsaved config.

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

    Display name.

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

    Optional description.

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

    Parent config id (slug), or nil for a root config.

  • items (Array<ConfigItem>, nil) (defaults to: nil)

    Base items.

  • environments (Hash{String => ConfigEnvironment}, nil) (defaults to: nil)

    Per-environment overrides keyed by environment id.

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

    Creation timestamp.

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

    Last-modified timestamp.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/smplkit/config/models.rb', line 117

def initialize(client = nil, key:, id: nil, name: nil, description: nil,
               parent_id: nil, items: nil, environments: nil,
               created_at: nil, updated_at: nil)
  @client = client
  @id = id
  @key = key
  @name = name
  @description = description
  @parent_id = parent_id
  @items = items ? items.dup : []
  @environments = environments ? environments.dup : {}
  @created_at = created_at
  @updated_at = updated_at
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



101
102
103
# File 'lib/smplkit/config/models.rb', line 101

def created_at
  @created_at
end

#descriptionObject

Returns the value of attribute description.



101
102
103
# File 'lib/smplkit/config/models.rb', line 101

def description
  @description
end

#idObject

Returns the value of attribute id.



101
102
103
# File 'lib/smplkit/config/models.rb', line 101

def id
  @id
end

#keyObject

Returns the value of attribute key.



101
102
103
# File 'lib/smplkit/config/models.rb', line 101

def key
  @key
end

#nameObject

Returns the value of attribute name.



101
102
103
# File 'lib/smplkit/config/models.rb', line 101

def name
  @name
end

#parent_idObject

Returns the value of attribute parent_id.



101
102
103
# File 'lib/smplkit/config/models.rb', line 101

def parent_id
  @parent_id
end

#updated_atObject

Returns the value of attribute updated_at.



101
102
103
# File 'lib/smplkit/config/models.rb', line 101

def updated_at
  @updated_at
end

Instance Method Details

#_apply(other) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
# File 'lib/smplkit/config/models.rb', line 266

def _apply(other)
  @id = other.id
  @key = other.key
  @name = other.name
  @description = other.description
  @parent_id = other.parent_id
  @items = other.items
  @environments = other.environments
  @created_at = other.created_at
  @updated_at = other.updated_at
end

#deletevoid Also known as: delete!

This method returns an undefined value.

Delete this config from the server.

Raises:

  • (RuntimeError)

    If the model was constructed without a client.



169
170
171
172
173
# File 'lib/smplkit/config/models.rb', line 169

def delete
  raise "Config was constructed without a client; cannot delete" if @client.nil?

  @client.delete(@key)
end

#environmentsHash{String => ConfigEnvironment}

Returns A read-only shallow copy of the per-environment overrides keyed by environment id.

Returns:

  • (Hash{String => ConfigEnvironment})

    A read-only shallow copy of the per-environment overrides keyed by environment id.



139
140
141
# File 'lib/smplkit/config/models.rb', line 139

def environments
  @environments.dup
end

#itemsArray<ConfigItem>

Returns A read-only shallow copy of the base items.

Returns:

  • (Array<ConfigItem>)

    A read-only shallow copy of the base items.



133
134
135
# File 'lib/smplkit/config/models.rb', line 133

def items
  @items.dup
end

#remove(name, environment: nil) ⇒ self

Remove an item by name. When environment: is given, removes the per-environment override only. Removing an item that isn’t present is a no-op.

Parameters:

  • name (String)

    The item key to remove.

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

    When given, remove only this environment’s override for name, leaving the base item intact.

Returns:

  • (self)


252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/smplkit/config/models.rb', line 252

def remove(name, environment: nil)
  if environment
    env = @environments[environment]
    return self unless env

    raw = env.values_raw
    raw.delete(name)
    env._replace_raw(raw)
  else
    @items.reject! { |i| i.name == name }
  end
  self
end

#saveself Also known as: save!

Persist this config to the server. Creates a new config if unsaved, or updates the existing one.

Returns:

  • (self)

Raises:



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/smplkit/config/models.rb', line 151

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

  updated =
    if @created_at.nil?
      @client._create_config(self)
    else
      @client._update_config(self)
    end
  _apply(updated)
  self
end

#set(item, environment: nil) ⇒ self

Set (or replace) an item. When environment: is given, sets an override on that environment.

An environment override stores only the raw value; the declared type and description come from the base item, so the ConfigItem‘s type and description are ignored when environment: is supplied.

Parameters:

  • item (ConfigItem)

    The item to set; its name is the item key.

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

    When given, set the value as an override on this environment rather than on the base config.

Returns:

  • (self)


187
188
189
# File 'lib/smplkit/config/models.rb', line 187

def set(item, environment: nil)
  set_typed(item.name, item.value, item.type, environment: environment, description: item.description)
end

#set_boolean(name, value, environment: nil, description: nil) ⇒ self

Convenience: set a BOOLEAN item (or environment override).

Parameters:

  • name (String)

    The item key to set.

  • value (Boolean)

    The boolean value.

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

    When given, set the value as an override on this environment rather than on the base config.

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

    Optional human-readable description. Ignored when setting an environment override.

Returns:

  • (self)


226
227
228
# File 'lib/smplkit/config/models.rb', line 226

def set_boolean(name, value, environment: nil, description: nil)
  set_typed(name, value, ItemType::BOOLEAN, environment: environment, description: description)
end

#set_json(name, value, environment: nil, description: nil) ⇒ self

Convenience: set a JSON item (or environment override).

Parameters:

  • name (String)

    The item key to set.

  • value (Object)

    Any JSON-serializable value (Hash, Array, or primitive).

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

    When given, set the value as an override on this environment rather than on the base config.

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

    Optional human-readable description. Ignored when setting an environment override.

Returns:

  • (self)


240
241
242
# File 'lib/smplkit/config/models.rb', line 240

def set_json(name, value, environment: nil, description: nil)
  set_typed(name, value, ItemType::JSON, environment: environment, description: description)
end

#set_number(name, value, environment: nil, description: nil) ⇒ self

Convenience: set a NUMBER item (or environment override).

Parameters:

  • name (String)

    The item key to set.

  • value (Integer, Float)

    The numeric value.

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

    When given, set the value as an override on this environment rather than on the base config.

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

    Optional human-readable description. Ignored when setting an environment override.

Returns:

  • (self)


213
214
215
# File 'lib/smplkit/config/models.rb', line 213

def set_number(name, value, environment: nil, description: nil)
  set_typed(name, value, ItemType::NUMBER, environment: environment, description: description)
end

#set_string(name, value, environment: nil, description: nil) ⇒ self

Convenience: set a STRING item (or environment override).

Parameters:

  • name (String)

    The item key to set.

  • value (String)

    The string value.

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

    When given, set the value as an override on this environment rather than on the base config.

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

    Optional human-readable description. Ignored when setting an environment override.

Returns:

  • (self)


200
201
202
# File 'lib/smplkit/config/models.rb', line 200

def set_string(name, value, environment: nil, description: nil)
  set_typed(name, value, ItemType::STRING, environment: environment, description: description)
end