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



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.

Parameters:

  • built_rule (Hash)

    the Hash produced by Smplkit::Rule.new(…, environment: …).when(…).serve(…); must include an “environment” key naming the target environment.

Returns:

  • (self)

    this flag, so calls can be chained.

Raises:

  • (ArgumentError)

    the built_rule Hash has no “environment” key.



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.

Parameters:

  • flag_value (FlagValue)

    the value entry to allow the flag to serve.

Returns:

  • (self)

    this flag, so calls can be chained.



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.

Parameters:

  • environment (String)

    name of the environment whose default override to clear.

Returns:

  • (self)

    this flag, so calls can be chained.



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.

Parameters:

  • environment (String, nil) (defaults to: nil)

    name of the environment whose rules to remove; when nil (the default), removes rules from every environment configured on this flag.

Returns:

  • (self)

    this flag, so calls can be chained.



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_valuesself

Clear the constrained values list (unconstrained). Call save to persist.

Returns:

  • (self)

    this flag, so calls can be chained.



267
268
269
270
# File 'lib/smplkit/flags/models.rb', line 267

def clear_values
  @values = []
  self
end

#deletevoid Also known as: delete!

This method returns an undefined value.

Delete this flag from the server.

Raises:



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).

Parameters:

  • environment (String, nil) (defaults to: nil)

    name of the environment to disable; when nil (the default), disables rules in every environment configured on this flag.

Returns:

  • (self)

    this flag, so calls can be chained.



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.

Parameters:

  • environment (String, nil) (defaults to: nil)

    name of the environment to enable; when nil (the default), enables rules in every environment configured on this flag.

Returns:

  • (self)

    this flag, so calls can be chained.



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

#environmentsHash{String => FlagEnvironment}

Read-only view of per-environment configuration.

Mutate via add_rule / enable_rules / disable_rules / set_default (with environment:) / clear_rules.

Returns:



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.

Parameters:

  • name (String)

    the value-entry name to remove; the first match is removed and others are left in place.

Returns:

  • (self)

    this flag, so calls can be chained.



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

#saveself 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).

Returns:

  • (self)

    this flag, with server-assigned fields applied.



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.

Parameters:

  • value (Object)

    the default value to serve when no rule matches.

  • environment (String)

    name of the environment whose default to set.

Returns:

  • (self)

    this flag, so calls can be chained.



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

#valuesArray<FlagValue>?

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

Mutate via add_value / remove_value / clear_values.

Returns:

  • (Array<FlagValue>, nil)

    the constrained values, or nil when the flag is unconstrained.



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

def values
  @values&.dup
end