Class: Smplkit::Flags::Flag
- Inherits:
-
Object
- Object
- Smplkit::Flags::Flag
- 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
Instance Attribute Summary collapse
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#default ⇒ Object
Returns the value of attribute default.
-
#description ⇒ Object
Returns the value of attribute description.
-
#id ⇒ Object
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
-
#type ⇒ Object
Returns the value of attribute type.
-
#updated_at ⇒ Object
Returns the value of attribute updated_at.
Instance Method Summary collapse
- #_apply(other) ⇒ Object
-
#add_rule(built_rule) ⇒ self
Append a rule to a specific environment.
-
#add_value(flag_value) ⇒ self
Append a constrained value to the flag’s values list.
-
#clear_default(environment:) ⇒ self
Clear the per-environment default override on
environment. -
#clear_rules(environment: nil) ⇒ self
Remove rules.
-
#clear_values ⇒ self
Clear the constrained values list (unconstrained).
-
#delete ⇒ void
(also: #delete!)
Delete this flag from the server.
-
#disable_rules(environment: nil) ⇒ self
Disable rule evaluation (kill switch).
-
#enable_rules(environment: nil) ⇒ self
Enable rule evaluation.
-
#environments ⇒ Hash{String => FlagEnvironment}
Read-only view of per-environment configuration.
-
#initialize(client = nil, name:, type:, default:, id: nil, values: nil, description: nil, environments: nil, created_at: nil, updated_at: nil) ⇒ Flag
constructor
A new instance of Flag.
-
#remove_value(name) ⇒ self
Remove the first values entry whose
namematches. -
#save ⇒ self
(also: #save!)
Persist this flag to the server.
-
#set_default(value, environment:) ⇒ self
Set the flag’s per-environment default served value.
-
#values ⇒ Array<FlagValue>?
Read-only view of constrained values.
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_at ⇒ Object
Returns the value of attribute created_at.
85 86 87 |
# File 'lib/smplkit/flags/models.rb', line 85 def created_at @created_at end |
#default ⇒ Object
Returns the value of attribute default.
85 86 87 |
# File 'lib/smplkit/flags/models.rb', line 85 def default @default end |
#description ⇒ Object
Returns the value of attribute description.
85 86 87 |
# File 'lib/smplkit/flags/models.rb', line 85 def description @description end |
#id ⇒ Object
Returns the value of attribute id.
85 86 87 |
# File 'lib/smplkit/flags/models.rb', line 85 def id @id end |
#name ⇒ Object
Returns the value of attribute name.
85 86 87 |
# File 'lib/smplkit/flags/models.rb', line 85 def name @name end |
#type ⇒ Object
Returns the value of attribute type.
85 86 87 |
# File 'lib/smplkit/flags/models.rb', line 85 def type @type end |
#updated_at ⇒ Object
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
272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/smplkit/flags/models.rb', line 272 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) ⇒ self
Append a rule to a specific environment.
The built_rule Hash must include an “environment” key. Call save to persist.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/smplkit/flags/models.rb', line 163 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) ⇒ self
Append a constrained value to the flag’s values list. Call save to persist.
246 247 248 249 250 |
# File 'lib/smplkit/flags/models.rb', line 246 def add_value(flag_value) @values ||= [] @values << flag_value self end |
#clear_default(environment:) ⇒ self
Clear the per-environment default override on environment.
After clearing, the environment falls back to the flag’s base default when no rule matches. Call save to persist.
237 238 239 240 |
# File 'lib/smplkit/flags/models.rb', line 237 def clear_default(environment:) @environments[environment] = (@environments[environment] || FlagEnvironment.new).with(default: nil) self end |
#clear_rules(environment: nil) ⇒ self
Remove rules. Call save to persist.
212 213 214 215 216 217 |
# File 'lib/smplkit/flags/models.rb', line 212 def clear_rules(environment: nil) scoped(environment) do |k| @environments[k] = (@environments[k] || FlagEnvironment.new).with(rules: [].freeze) end self end |
#clear_values ⇒ self
Clear the constrained values list (unconstrained). Call save to persist.
267 268 269 270 |
# File 'lib/smplkit/flags/models.rb', line 267 def clear_values @values = [] self end |
#delete ⇒ void Also known as: delete!
This method returns an undefined value.
Delete this flag from the server.
146 147 148 149 150 |
# File 'lib/smplkit/flags/models.rb', line 146 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) ⇒ self
Disable rule evaluation (kill switch). Call save to persist.
When disabled, get skips rules and returns the env-specific default (or the flag’s base default).
201 202 203 204 |
# File 'lib/smplkit/flags/models.rb', line 201 def disable_rules(environment: nil) scoped(environment) { |k| @environments[k] = (@environments[k] || FlagEnvironment.new).with(enabled: false) } self end |
#enable_rules(environment: nil) ⇒ self
Enable rule evaluation. Call save to persist.
187 188 189 190 |
# File 'lib/smplkit/flags/models.rb', line 187 def enable_rules(environment: nil) scoped(environment) { |k| @environments[k] = (@environments[k] || FlagEnvironment.new).with(enabled: true) } self end |
#environments ⇒ Hash{String => FlagEnvironment}
Read-only view of per-environment configuration.
Mutate via add_rule / enable_rules / disable_rules / set_default (with environment:) / clear_rules.
117 118 119 |
# File 'lib/smplkit/flags/models.rb', line 117 def environments @environments.dup end |
#remove_value(name) ⇒ self
Remove the first values entry whose name matches.
257 258 259 260 261 262 |
# File 'lib/smplkit/flags/models.rb', line 257 def remove_value(name) return self unless @values @values = @values.reject { |v| v.name == name } self end |
#save ⇒ self Also known as: save!
Persist this flag to the server.
Creates a new flag if unsaved, or updates the existing one. Requires a client (i.e. the flag was constructed via client.flags.new_* or returned from client.flags.get / client.flags.list).
128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/smplkit/flags/models.rb', line 128 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:) ⇒ self
Set the flag’s per-environment default served value. Call save to persist.
224 225 226 227 |
# File 'lib/smplkit/flags/models.rb', line 224 def set_default(value, environment:) @environments[environment] = (@environments[environment] || FlagEnvironment.new).with(default: value) self end |
#values ⇒ Array<FlagValue>?
Read-only view of constrained values. nil means unconstrained.
Mutate via add_value / remove_value / clear_values.
107 108 109 |
# File 'lib/smplkit/flags/models.rb', line 107 def values @values&.dup end |