Class: Subflag::Rails::Flag

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/subflag/rails/models/flag.rb

Overview

ActiveRecord model for storing feature flags in your database.

Supports targeting rules to show different values to different users. Perfect for internal testing before wider rollout.

Examples:

Simple flag (everyone gets the same value)

Subflag::Rails::Flag.create!(
  key: "new-checkout",
  value: "true",
  value_type: "boolean"
)

Flag with targeting rules (internal team gets different value)

Subflag::Rails::Flag.create!(
  key: "new-dashboard",
  value: "false",
  value_type: "boolean",
  targeting_rules: [
    {
      "value" => "true",
      "conditions" => {
        "type" => "OR",
        "conditions" => [
          { "attribute" => "email", "operator" => "ENDS_WITH", "value" => "@company.com" },
          { "attribute" => "role", "operator" => "IN", "value" => ["admin", "developer", "qa"] }
        ]
      }
    }
  ]
)

Progressive rollout (first match wins)

Subflag::Rails::Flag.create!(
  key: "max-projects",
  value: "5",
  value_type: "integer",
  targeting_rules: [
    { "value" => "1000", "conditions" => { "type" => "AND", "conditions" => [{ "attribute" => "role", "operator" => "EQUALS", "value" => "admin" }] } },
    { "value" => "100", "conditions" => { "type" => "AND", "conditions" => [{ "attribute" => "email", "operator" => "ENDS_WITH", "value" => "@company.com" }] } },
    { "value" => "25", "conditions" => { "type" => "AND", "conditions" => [{ "attribute" => "plan", "operator" => "EQUALS", "value" => "pro" }] } }
  ]
)

Constant Summary collapse

VALUE_TYPES =
%w[boolean string integer float object].freeze

Instance Method Summary collapse

Instance Method Details

#evaluate(context: nil, expected_type: nil) ⇒ Object

Evaluate the flag for a given context

Returns the matched rule's value if context matches targeting rules, otherwise returns the default value.

Parameters:

  • context (Hash, nil) (defaults to: nil)

    Evaluation context with user attributes

  • expected_type (Symbol, String, nil) (defaults to: nil)

    Override the value_type for casting

Returns:

  • (Object)

    The evaluated value, cast to the appropriate type



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/subflag/rails/models/flag.rb', line 70

def evaluate(context: nil, expected_type: nil)
  rules = parsed_targeting_rules
  raw_value = if rules.present? && context.present?
                matched = TargetingEngine.evaluate(rules, context, flag_key: key)
                matched || value
              else
                value
              end

  cast_value(raw_value, expected_type)
end

#typed_value(expected_type = nil) ⇒ Object

Get the flag's default value cast to its declared type (ignores targeting)

Parameters:

  • expected_type (Symbol, String, nil) (defaults to: nil)

    Override the value_type for casting

Returns:

  • (Object)

    The typed value



86
87
88
# File 'lib/subflag/rails/models/flag.rb', line 86

def typed_value(expected_type = nil)
  cast_value(value, expected_type)
end