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.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/smplkit/config/models.rb', line 79

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.



77
78
79
# File 'lib/smplkit/config/models.rb', line 77

def created_at
  @created_at
end

#descriptionObject

Returns the value of attribute description.



77
78
79
# File 'lib/smplkit/config/models.rb', line 77

def description
  @description
end

#idObject

Returns the value of attribute id.



77
78
79
# File 'lib/smplkit/config/models.rb', line 77

def id
  @id
end

#keyObject

Returns the value of attribute key.



77
78
79
# File 'lib/smplkit/config/models.rb', line 77

def key
  @key
end

#nameObject

Returns the value of attribute name.



77
78
79
# File 'lib/smplkit/config/models.rb', line 77

def name
  @name
end

#parent_idObject

Returns the value of attribute parent_id.



77
78
79
# File 'lib/smplkit/config/models.rb', line 77

def parent_id
  @parent_id
end

#updated_atObject

Returns the value of attribute updated_at.



77
78
79
# File 'lib/smplkit/config/models.rb', line 77

def updated_at
  @updated_at
end

Instance Method Details

#_apply(other) ⇒ Object



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

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!



116
117
118
119
120
# File 'lib/smplkit/config/models.rb', line 116

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

  @client.delete(@key)
end

#environmentsObject



98
99
100
# File 'lib/smplkit/config/models.rb', line 98

def environments
  @environments.dup
end

#itemsObject



94
95
96
# File 'lib/smplkit/config/models.rb', line 94

def items
  @items.dup
end

#remove_item(name, environment: nil) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/smplkit/config/models.rb', line 139

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!



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/smplkit/config/models.rb', line 102

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



131
132
133
# File 'lib/smplkit/config/models.rb', line 131

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



135
136
137
# File 'lib/smplkit/config/models.rb', line 135

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



127
128
129
# File 'lib/smplkit/config/models.rb', line 127

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



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

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