Module: Lich::Common::FeatureFlags

Defined in:
documented/common/feature_flags.rb

Overview

Provides persistent access to core feature flags.

Feature flags are stored in the lich_settings table using keys with the feature_flag: prefix. Missing keys fall back to values defined in DEFAULTS, or false when no explicit default is registered.

This module intentionally exposes a minimal surface area:

Keeping the API narrow makes it easier to adopt feature flags incrementally without introducing a second configuration framework.

Constant Summary collapse

SETTINGS_PREFIX =
'feature_flag:'
VALID_NAME_PATTERN =

Pattern that validates normalized feature flag names.

Flags must be lowercase alphanumeric or underscore, with no spaces or special characters. This ensures portable flag names that can be serialized as database keys without escaping.

Examples:

Valid flag names

"cache_enabled" matches
"enable_v2" matches
"flag_1" matches

Invalid flag names

"CacheEnabled" does not match (uppercase)
"cache-enabled" does not match (hyphen)
"cache enabled" does not match (space)

See Also:

  • normalize_name
  • validate_flag_name!
/\A[a-z0-9_]+\z/
DEFAULTS =

Defines default values for known feature flags.

Add new flags here as infrastructure is adopted by production code. The persisted value in lich_settings always overrides the default.

{}.freeze

Class Method Summary collapse

Class Method Details

.enabled?(name) ⇒ Boolean

Returns whether a feature flag is enabled.

Parameters:

  • name (String, Symbol)

    the feature flag name

Returns:

  • (Boolean)

    the persisted flag value, the configured default, or false if the flag is unknown or the value cannot be read

Raises:

  • (ArgumentError)

    if the flag name is blank or contains unsupported characters



60
61
62
63
64
65
66
67
68
69
70
71
# File 'documented/common/feature_flags.rb', line 60

def self.enabled?(name)
  flag_name = validate_flag_name!(normalize_name(name))
  begin
    stored = read_flag(flag_name)
    return default_for(flag_name) if stored.nil?

    truthy?(stored)
  rescue StandardError => e
    log_failure('read', flag_name, e)
    default_for(flag_name)
  end
end

.set(name, value) ⇒ Boolean

Persists a feature flag value in lich_settings.

Values are stored as strings to match the existing lich_settings storage model. Callers should prefer booleans, but any value responding to #to_s is accepted and later interpreted by enabled?.

Parameters:

  • name (String, Symbol)

    the feature flag name

  • value (Object)

    the value to persist

Returns:

  • (Boolean)

    true when the write succeeds, otherwise false

Raises:

  • (ArgumentError)

    if the flag name is blank or contains unsupported characters



84
85
86
87
88
89
90
91
92
# File 'documented/common/feature_flags.rb', line 84

def self.set(name, value)
  flag_name = validate_flag_name!(normalize_name(name))
  begin
    write_flag(flag_name, value)
  rescue StandardError => e
    log_failure('write', flag_name, e)
    false
  end
end