Module: Philiprehberger::FeatureFlag::Targeting

Included in:
Philiprehberger::FeatureFlag
Defined in:
lib/philiprehberger/feature_flag/targeting.rb

Instance Method Summary collapse

Instance Method Details

#disable_for(flag, users:) ⇒ Object



27
28
29
30
31
32
# File 'lib/philiprehberger/feature_flag/targeting.rb', line 27

def disable_for(flag, users:)
  @targets ||= {}
  return unless @targets.key?(flag.to_sym)

  @targets[flag.to_sym] -= users.map(&:to_s)
end

#enable_for(flag, users: nil, context: nil) ⇒ void

This method returns an undefined value.

Whitelist users or a context predicate for flag. Either (or both) may be passed. Context predicates are matched against the hash supplied to Philiprehberger::FeatureFlag.enabled?.

Parameters:

  • flag (Symbol, String)

    flag name

  • users (Array<String, Integer, Symbol>, nil) (defaults to: nil)

    user identifiers to add to the whitelist (normalized to strings)

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

    optional predicate hash. A request matches when every key/value in the predicate equals the value in the supplied context (Array values match any element).



17
18
19
20
21
22
23
24
25
# File 'lib/philiprehberger/feature_flag/targeting.rb', line 17

def enable_for(flag, users: nil, context: nil)
  @targets ||= {}
  @targets[flag.to_sym] ||= []
  @targets[flag.to_sym] |= users.map(&:to_s) if users

  @context_predicates ||= {}
  @context_predicates[flag.to_sym] ||= []
  @context_predicates[flag.to_sym] << context if context.is_a?(Hash)
end

#reset_targets!Object



49
50
51
52
# File 'lib/philiprehberger/feature_flag/targeting.rb', line 49

def reset_targets!
  @targets = nil
  @context_predicates = nil
end

#targeted?(flag, user, context: {}) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
# File 'lib/philiprehberger/feature_flag/targeting.rb', line 42

def targeted?(flag, user, context: {})
  return true if targeted_by_context?(flag, context)
  return false if user.nil?

  targeted_users(flag).include?(user.to_s)
end

#targeted_contexts(flag) ⇒ Object



38
39
40
# File 'lib/philiprehberger/feature_flag/targeting.rb', line 38

def targeted_contexts(flag)
  @context_predicates&.dig(flag.to_sym) || []
end

#targeted_users(flag) ⇒ Object



34
35
36
# File 'lib/philiprehberger/feature_flag/targeting.rb', line 34

def targeted_users(flag)
  @targets&.dig(flag.to_sym) || []
end