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.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/smplkit/config/models.rb', line 85

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.



83
84
85
# File 'lib/smplkit/config/models.rb', line 83

def created_at
  @created_at
end

#descriptionObject

Returns the value of attribute description.



83
84
85
# File 'lib/smplkit/config/models.rb', line 83

def description
  @description
end

#idObject

Returns the value of attribute id.



83
84
85
# File 'lib/smplkit/config/models.rb', line 83

def id
  @id
end

#keyObject

Returns the value of attribute key.



83
84
85
# File 'lib/smplkit/config/models.rb', line 83

def key
  @key
end

#nameObject

Returns the value of attribute name.



83
84
85
# File 'lib/smplkit/config/models.rb', line 83

def name
  @name
end

#parent_idObject

Returns the value of attribute parent_id.



83
84
85
# File 'lib/smplkit/config/models.rb', line 83

def parent_id
  @parent_id
end

#updated_atObject

Returns the value of attribute updated_at.



83
84
85
# File 'lib/smplkit/config/models.rb', line 83

def updated_at
  @updated_at
end

Instance Method Details

#_apply(other) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/smplkit/config/models.rb', line 159

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

#deleteObject Also known as: delete!



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

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

  @client.delete(@key)
end

#environmentsObject



104
105
106
# File 'lib/smplkit/config/models.rb', line 104

def environments
  @environments.dup
end

#itemsObject



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

def items
  @items.dup
end

#remove_item(name, environment: nil) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/smplkit/config/models.rb', line 145

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

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

#saveObject Also known as: save!



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/smplkit/config/models.rb', line 108

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_boolean(name, value, environment: nil, description: nil) ⇒ Object



137
138
139
# File 'lib/smplkit/config/models.rb', line 137

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



141
142
143
# File 'lib/smplkit/config/models.rb', line 141

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



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

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



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

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