Module: Detour::Flaggable

Defined in:
lib/detour/flaggable.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#detour_featuresObject



63
64
65
# File 'lib/detour/flaggable.rb', line 63

def detour_features
  @detour_features ||= []
end

#has_feature?(feature_name, &block) ⇒ Boolean

Returns whether or not the object has access to the given feature. If given a block, it will call the block if the user has access to the feature.

If an exception is raised in the block, it will increment the ‘failure_count` of the feature and raise the exception.

Examples:

# Exceptions will be tracked in the `failure_count` of :new_user_interface.
user.has_feature?(:new_user_interface) do
  # ...
end
# Exceptions will *not* be tracked in the `failure_count` of :new_user_interface.
if user.has_feature?(:new_user_interface)
  # ...
end

Parameters:

  • feature_name (Symbol)

    The name of the Feature being checked.

  • &block (Proc)

    A block to be called if the user is flagged in to the feature.

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/detour/flaggable.rb', line 34

def has_feature?(feature_name, &block)
  if detour_features.include? feature_name.to_s
    match = true
  else
    feature = Detour::Feature.find_by_name(feature_name)
    return false unless feature

    opt_out = opt_out_flags.find_by_feature_id(feature.id)
    return false if opt_out

    match = feature.match? self

    if match
      detour_features << feature.name.to_s
    end
  end

  if match && block_given?
    begin
      yield
    rescue => e
      feature.increment! :failure_count
      raise e
    end
  end

  match
end