Class: Smplkit::Flags::Flag

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

Overview

A flag resource.

Provides management operations (save, add_rule, environment settings) and runtime evaluation via get.

Use typed variants (BooleanFlag, StringFlag, NumberFlag, JsonFlag) for type-safe get return values.

Direct Known Subclasses

BooleanFlag, JsonFlag, NumberFlag, StringFlag

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client = nil, name:, type:, default:, id: nil, values: nil, description: nil, environments: nil, created_at: nil, updated_at: nil) ⇒ Flag

Returns a new instance of Flag.



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

def initialize(client = nil, name:, type:, default:, id: nil, values: nil,
               description: nil, environments: nil, created_at: nil, updated_at: nil)
  @client = client
  @id = id
  @name = name
  @type = type
  @default = default
  @values = values&.dup
  @description = description
  @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.



85
86
87
# File 'lib/smplkit/flags/models.rb', line 85

def created_at
  @created_at
end

#defaultObject

Returns the value of attribute default.



85
86
87
# File 'lib/smplkit/flags/models.rb', line 85

def default
  @default
end

#descriptionObject

Returns the value of attribute description.



85
86
87
# File 'lib/smplkit/flags/models.rb', line 85

def description
  @description
end

#idObject

Returns the value of attribute id.



85
86
87
# File 'lib/smplkit/flags/models.rb', line 85

def id
  @id
end

#nameObject

Returns the value of attribute name.



85
86
87
# File 'lib/smplkit/flags/models.rb', line 85

def name
  @name
end

#typeObject

Returns the value of attribute type.



85
86
87
# File 'lib/smplkit/flags/models.rb', line 85

def type
  @type
end

#updated_atObject

Returns the value of attribute updated_at.



85
86
87
# File 'lib/smplkit/flags/models.rb', line 85

def updated_at
  @updated_at
end

Instance Method Details

#_apply(other) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
# File 'lib/smplkit/flags/models.rb', line 204

def _apply(other)
  @id = other.id
  @name = other.name
  @type = other.type
  @default = other.default
  @description = other.description
  @values = other.instance_variable_get(:@values)&.dup
  @environments = other.instance_variable_get(:@environments).dup
  @created_at = other.created_at
  @updated_at = other.updated_at
end

#add_rule(built_rule) ⇒ Object

Append a rule to a specific environment.

The built_rule Hash must include an “environment” key. Call save to persist.



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

def add_rule(built_rule)
  env_key = built_rule["environment"]
  if env_key.nil?
    raise ArgumentError,
          "Built rule must include 'environment' key. " \
          "Use Smplkit::Rule.new(..., environment: 'env_key').when(...).serve(...)"
  end

  flag_rule = FlagRule.new(
    logic: (built_rule["logic"] || {}).dup,
    value: built_rule["value"],
    description: built_rule["description"]
  )
  existing = @environments[env_key] || FlagEnvironment.new
  @environments[env_key] = existing.with(rules: (existing.rules + [flag_rule]).freeze)
  self
end

#add_value(flag_value) ⇒ Object



186
187
188
189
190
# File 'lib/smplkit/flags/models.rb', line 186

def add_value(flag_value)
  @values ||= []
  @values << flag_value
  self
end

#clear_default(environment:) ⇒ Object



181
182
183
184
# File 'lib/smplkit/flags/models.rb', line 181

def clear_default(environment:)
  @environments[environment] = (@environments[environment] || FlagEnvironment.new).with(default: nil)
  self
end

#clear_rules(environment: nil) ⇒ Object



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

def clear_rules(environment: nil)
  scoped(environment) do |k|
    @environments[k] = (@environments[k] || FlagEnvironment.new).with(rules: [].freeze)
  end
  self
end

#clear_valuesObject



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

def clear_values
  @values = []
  self
end

#deleteObject Also known as: delete!



130
131
132
133
134
# File 'lib/smplkit/flags/models.rb', line 130

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

  @client.delete(@id)
end

#disable_rules(environment: nil) ⇒ Object



164
165
166
167
# File 'lib/smplkit/flags/models.rb', line 164

def disable_rules(environment: nil)
  scoped(environment) { |k| @environments[k] = (@environments[k] || FlagEnvironment.new).with(enabled: false) }
  self
end

#enable_rules(environment: nil) ⇒ Object



159
160
161
162
# File 'lib/smplkit/flags/models.rb', line 159

def enable_rules(environment: nil)
  scoped(environment) { |k| @environments[k] = (@environments[k] || FlagEnvironment.new).with(enabled: true) }
  self
end

#environmentsObject

Read-only view of per-environment configuration.



107
108
109
# File 'lib/smplkit/flags/models.rb', line 107

def environments
  @environments.dup
end

#remove_value(name) ⇒ Object



192
193
194
195
196
197
# File 'lib/smplkit/flags/models.rb', line 192

def remove_value(name)
  return self unless @values

  @values = @values.reject { |v| v.name == name }
  self
end

#saveObject Also known as: save!

Persist this flag to the server.

Creates a new flag if unsaved, or updates the existing one. Requires a management client (i.e. the flag was constructed via mgmt.flags.new_* or returned from mgmt.flags.get/list).



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/smplkit/flags/models.rb', line 116

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

  updated =
    if @created_at.nil?
      @client._create_flag(self)
    else
      @client._update_flag(self)
    end
  _apply(updated)
  self
end

#set_default(value, environment:) ⇒ Object



176
177
178
179
# File 'lib/smplkit/flags/models.rb', line 176

def set_default(value, environment:)
  @environments[environment] = (@environments[environment] || FlagEnvironment.new).with(default: value)
  self
end

#valuesObject

Read-only view of constrained values. nil means unconstrained.



102
103
104
# File 'lib/smplkit/flags/models.rb', line 102

def values
  @values&.dup
end