Module: Detour::Concerns::Matchers

Included in:
Feature
Defined in:
app/models/detour/concerns/matchers.rb

Instance Method Summary collapse

Instance Method Details

#match?(instance) ⇒ Boolean

Determines whether or not the given instance has had the feature rolled out to it either via direct flagging-in, percentage, or by group membership.

Examples:

feature.match?(current_user)

Parameters:

  • instance (ActiveRecord::Base)

    A record to be tested for feature rollout.

Returns:

  • (Boolean)

    Whether or not the given instance has the feature rolled out to it.



13
14
15
# File 'app/models/detour/concerns/matchers.rb', line 13

def match?(instance)
  match_id?(instance) || match_percentage?(instance) || match_groups?(instance)
end

#match_groups?(instance) ⇒ Boolean

Determines whether or not the given instance has had the feature rolled out to it via group membership.

Examples:

feature.match_groups?(current_user)

Parameters:

  • instance (ActiveRecord::Base)

    A record to be tested for feature rollout.

Returns:

  • (Boolean)

    Whether or not the given instance has the feature rolled out to it via direct group membership.



61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/detour/concerns/matchers.rb', line 61

def match_groups?(instance)
  klass = instance.class.to_s

  return unless Detour.config.defined_groups[klass]

  group_names = group_flags.find_all_by_flaggable_type(klass).collect(&:group_name)

  Detour.config.defined_groups[klass].collect { |group_name, block|
    block.call(instance) if group_names.include? group_name.to_s
  }.any?
end

#match_id?(instance) ⇒ Boolean

Determines whether or not the given instance has had the feature rolled out to it via direct flagging-in.

Examples:

feature.match_id?(current_user)

Parameters:

  • instance (ActiveRecord::Base)

    A record to be tested for feature rollout.

Returns:

  • (Boolean)

    Whether or not the given instance has the feature rolled out to it via direct flagging-in.



28
29
30
# File 'app/models/detour/concerns/matchers.rb', line 28

def match_id?(instance)
  flag_in_flags.where(flaggable_type: instance.class.to_s, flaggable_id: instance.id).any?
end

#match_percentage?(instance) ⇒ Boolean

Determines whether or not the given instance has had the feature rolled out to it via percentage.

Examples:

feature.match_percentage?(current_user)

Parameters:

  • instance (ActiveRecord::Base)

    A record to be tested for feature rollout.

Returns:

  • (Boolean)

    Whether or not the given instance has the feature rolled out to it via direct percentage.



43
44
45
46
47
48
# File 'app/models/detour/concerns/matchers.rb', line 43

def match_percentage?(instance)
  flag = percentage_flags.find(:first, conditions: ["flaggable_type = ?", instance.class.to_s])
  percentage = flag ? flag.percentage : 0

  instance.id % 10 < percentage / 10
end