Class: Smplkit::Config::Config
- Inherits:
-
Object
- Object
- Smplkit::Config::Config
- 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
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#description ⇒ Object
Returns the value of attribute description.
-
#id ⇒ Object
Returns the value of attribute id.
-
#key ⇒ Object
Returns the value of attribute key.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent_id ⇒ Object
Returns the value of attribute parent_id.
-
#updated_at ⇒ Object
Returns the value of attribute updated_at.
Instance Method Summary collapse
- #_apply(other) ⇒ Object
-
#delete ⇒ void
(also: #delete!)
Delete this config from the server.
-
#environments ⇒ Hash{String => ConfigEnvironment}
A read-only shallow copy of the per-environment overrides keyed by environment id.
-
#initialize(client = nil, key:, id: nil, name: nil, description: nil, parent_id: nil, items: nil, environments: nil, created_at: nil, updated_at: nil) ⇒ Config
constructor
A new instance of Config.
-
#items ⇒ Array<ConfigItem>
A read-only shallow copy of the base items.
-
#remove(name, environment: nil) ⇒ self
Remove an item by name.
-
#save ⇒ self
(also: #save!)
Persist this config to the server.
-
#set(item, environment: nil) ⇒ self
Set (or replace) an item.
-
#set_boolean(name, value, environment: nil, description: nil) ⇒ self
Convenience: set a BOOLEAN item (or environment override).
-
#set_json(name, value, environment: nil, description: nil) ⇒ self
Convenience: set a JSON item (or environment override).
-
#set_number(name, value, environment: nil, description: nil) ⇒ self
Convenience: set a NUMBER item (or environment override).
-
#set_string(name, value, environment: nil, description: nil) ⇒ self
Convenience: set a STRING item (or environment override).
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.
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_at ⇒ Object
Returns the value of attribute created_at.
101 102 103 |
# File 'lib/smplkit/config/models.rb', line 101 def created_at @created_at end |
#description ⇒ Object
Returns the value of attribute description.
101 102 103 |
# File 'lib/smplkit/config/models.rb', line 101 def description @description end |
#id ⇒ Object
Returns the value of attribute id.
101 102 103 |
# File 'lib/smplkit/config/models.rb', line 101 def id @id end |
#key ⇒ Object
Returns the value of attribute key.
101 102 103 |
# File 'lib/smplkit/config/models.rb', line 101 def key @key end |
#name ⇒ Object
Returns the value of attribute name.
101 102 103 |
# File 'lib/smplkit/config/models.rb', line 101 def name @name end |
#parent_id ⇒ Object
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_at ⇒ Object
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 |
#delete ⇒ void Also known as: delete!
This method returns an undefined value.
Delete this config from the server.
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 |
#environments ⇒ Hash{String => ConfigEnvironment}
Returns 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 |
#items ⇒ Array<ConfigItem>
Returns 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.
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 |
#save ⇒ self Also known as: save!
Persist this config to the server. Creates a new config if unsaved, or updates the existing one.
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.
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).
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).
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).
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).
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 |