Module: BrainzLab::Cortex
- Defined in:
- lib/brainzlab/cortex.rb,
lib/brainzlab/cortex/cache.rb,
lib/brainzlab/cortex/client.rb,
lib/brainzlab/cortex/provisioner.rb
Defined Under Namespace
Classes: Cache, Client, Provisioner
Class Method Summary collapse
-
.all(**context) ⇒ Hash
Get all flags for a context.
- .cache ⇒ Object
-
.clear_cache! ⇒ Object
Clear the flag cache.
-
.clear_context! ⇒ Object
Clear the current context.
- .client ⇒ Object
-
.create_flag(flag_name, name: nil, type: 'boolean', description: nil, tags: []) ⇒ Hash?
Create (push) a new feature flag definition.
-
.disable!(flag_name, environment: nil) ⇒ Object
Disable a flag in an environment (idempotent).
-
.disabled?(flag_name, **context) ⇒ Boolean
Check if a feature flag is disabled.
-
.enable!(flag_name, environment: nil) ⇒ Hash?
Enable a flag in an environment (idempotent).
-
.enabled?(flag_name, **context) ⇒ Boolean
Check if a feature flag is enabled.
-
.ensure_provisioned! ⇒ Object
INTERNAL ===.
-
.flag_config(flag_name) ⇒ Hash?
Get a flag’s configuration.
-
.get(flag_name, default: nil, **context) ⇒ Object
Get the value of a feature flag.
-
.list_flags ⇒ Array<Hash>
List all flag definitions.
- .provisioner ⇒ Object
- .reset! ⇒ Object
-
.set_context(**context) ⇒ Object
Set default context for all evaluations in current request.
-
.set_percentage(flag_name, percentage, environment: nil) ⇒ Object
Set the rollout percentage (0-100) for a percentage flag.
-
.variant(flag_name, default: nil, **context) ⇒ String?
Get the variant for an A/B test flag.
-
.with_context(**context) ⇒ Object
Evaluate flags with a temporary context.
Class Method Details
.all(**context) ⇒ Hash
Get all flags for a context
93 94 95 96 97 98 99 100 101 |
# File 'lib/brainzlab/cortex.rb', line 93 def all(**context) return {} unless module_enabled? ensure_provisioned! return {} unless BrainzLab.configuration.cortex_valid? merged_context = merge_context(context) client.evaluate_all(context: merged_context) end |
.cache ⇒ Object
217 218 219 |
# File 'lib/brainzlab/cortex.rb', line 217 def cache @cache ||= Cache.new(BrainzLab.configuration.cortex_cache_ttl) end |
.clear_cache! ⇒ Object
Clear the flag cache
173 174 175 |
# File 'lib/brainzlab/cortex.rb', line 173 def clear_cache! cache.clear! end |
.clear_context! ⇒ Object
Clear the current context
186 187 188 |
# File 'lib/brainzlab/cortex.rb', line 186 def clear_context! Thread.current[:cortex_context] = nil end |
.client ⇒ Object
213 214 215 |
# File 'lib/brainzlab/cortex.rb', line 213 def client @client ||= Client.new(BrainzLab.configuration) end |
.create_flag(flag_name, name: nil, type: 'boolean', description: nil, tags: []) ⇒ Hash?
Create (push) a new feature flag definition.
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/brainzlab/cortex.rb', line 136 def create_flag(flag_name, name: nil, type: 'boolean', description: nil, tags: []) return nil unless writable? client.create_flag( key: flag_name.to_s, name: name || flag_name.to_s, flag_type: type, description: description, tags: ) end |
.disable!(flag_name, environment: nil) ⇒ Object
Disable a flag in an environment (idempotent).
155 156 157 |
# File 'lib/brainzlab/cortex.rb', line 155 def disable!(flag_name, environment: nil) set_enabled(flag_name, false, environment: environment) end |
.disabled?(flag_name, **context) ⇒ Boolean
Check if a feature flag is disabled
29 30 31 |
# File 'lib/brainzlab/cortex.rb', line 29 def disabled?(flag_name, **context) !enabled?(flag_name, **context) end |
.enable!(flag_name, environment: nil) ⇒ Hash?
Enable a flag in an environment (idempotent).
150 151 152 |
# File 'lib/brainzlab/cortex.rb', line 150 def enable!(flag_name, environment: nil) set_enabled(flag_name, true, environment: environment) end |
.enabled?(flag_name, **context) ⇒ Boolean
Check if a feature flag is enabled
20 21 22 23 |
# File 'lib/brainzlab/cortex.rb', line 20 def enabled?(flag_name, **context) result = get(flag_name, **context) [true, 'true'].include?(result) end |
.ensure_provisioned! ⇒ Object
INTERNAL ===
202 203 204 205 206 207 |
# File 'lib/brainzlab/cortex.rb', line 202 def ensure_provisioned! return if @provisioned @provisioned = true provisioner.ensure_project! end |
.flag_config(flag_name) ⇒ Hash?
Get a flag’s configuration
117 118 119 120 121 122 123 124 |
# File 'lib/brainzlab/cortex.rb', line 117 def flag_config(flag_name) return nil unless module_enabled? ensure_provisioned! return nil unless BrainzLab.configuration.cortex_valid? client.get_flag(flag_name.to_s) end |
.get(flag_name, default: nil, **context) ⇒ Object
Get the value of a feature flag
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/brainzlab/cortex.rb', line 42 def get(flag_name, default: nil, **context) return default unless module_enabled? ensure_provisioned! return default unless BrainzLab.configuration.cortex_valid? flag_key = flag_name.to_s merged_context = merge_context(context) cache_key = build_cache_key(flag_key, merged_context) # Check cache first return cache.get(cache_key) if BrainzLab.configuration.cortex_cache_enabled && cache.has?(cache_key) result = client.evaluate(flag_key, context: merged_context) if result.nil? default else cache.set(cache_key, result) if BrainzLab.configuration.cortex_cache_enabled result end end |
.list_flags ⇒ Array<Hash>
List all flag definitions
105 106 107 108 109 110 111 112 |
# File 'lib/brainzlab/cortex.rb', line 105 def list_flags return [] unless module_enabled? ensure_provisioned! return [] unless BrainzLab.configuration.cortex_valid? client.list end |
.provisioner ⇒ Object
209 210 211 |
# File 'lib/brainzlab/cortex.rb', line 209 def provisioner @provisioner ||= Provisioner.new(BrainzLab.configuration) end |
.reset! ⇒ Object
221 222 223 224 225 226 227 |
# File 'lib/brainzlab/cortex.rb', line 221 def reset! @client = nil @provisioner = nil @cache = nil @provisioned = false Thread.current[:cortex_context] = nil end |
.set_context(**context) ⇒ Object
Set default context for all evaluations in current request
181 182 183 |
# File 'lib/brainzlab/cortex.rb', line 181 def set_context(**context) Thread.current[:cortex_context] = (Thread.current[:cortex_context] || {}).merge(context) end |
.set_percentage(flag_name, percentage, environment: nil) ⇒ Object
Set the rollout percentage (0-100) for a percentage flag.
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/brainzlab/cortex.rb', line 160 def set_percentage(flag_name, percentage, environment: nil) return nil unless writable? result = client.set_percentage( flag_name.to_s, percentage: percentage, environment: environment || BrainzLab.configuration.environment ) clear_cache! result end |
.variant(flag_name, default: nil, **context) ⇒ String?
Get the variant for an A/B test flag
79 80 81 82 |
# File 'lib/brainzlab/cortex.rb', line 79 def variant(flag_name, default: nil, **context) result = get(flag_name, **context) result.is_a?(String) ? result : default end |
.with_context(**context) ⇒ Object
Evaluate flags with a temporary context
192 193 194 195 196 197 198 |
# File 'lib/brainzlab/cortex.rb', line 192 def with_context(**context) previous = Thread.current[:cortex_context] Thread.current[:cortex_context] = (previous || {}).merge(context) yield ensure Thread.current[:cortex_context] = previous end |