Class: Subflag::Rails::Flag
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Subflag::Rails::Flag
- 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.
Constant Summary collapse
- VALUE_TYPES =
%w[boolean string integer float object].freeze
Instance Method Summary collapse
-
#evaluate(context: nil, expected_type: nil) ⇒ Object
Evaluate the flag for a given context.
-
#typed_value(expected_type = nil) ⇒ Object
Get the flag's default value cast to its declared type (ignores targeting).
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.
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)
86 87 88 |
# File 'lib/subflag/rails/models/flag.rb', line 86 def typed_value(expected_type = nil) cast_value(value, expected_type) end |