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) ⇒ Object
Append a rule to a specific environment.
- #add_value(flag_value) ⇒ Object
- #clear_default(environment:) ⇒ Object
- #clear_rules(environment: nil) ⇒ Object
- #clear_values ⇒ Object
- #delete ⇒ Object (also: #delete!)
- #disable_rules(environment: nil) ⇒ Object
- #enable_rules(environment: nil) ⇒ Object
-
#environments ⇒ Object
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) ⇒ Object
-
#save ⇒ Object
(also: #save!)
Persist this flag to the server.
- #set_default(value, environment:) ⇒ Object
-
#values ⇒ Object
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
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_values ⇒ Object
199 200 201 202 |
# File 'lib/smplkit/flags/models.rb', line 199 def clear_values @values = [] self end |
#delete ⇒ Object 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 |
#environments ⇒ Object
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 |
#save ⇒ Object 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 |
#values ⇒ Object
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 |