Class: EightBall::Feature

Inherits:
Object
  • Object
show all
Defined in:
lib/eight_ball/feature.rb

Overview

A Feature is an element of your application that can be enabled or disabled based on various Conditions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, enabled_for = [], disabled_for = []) ⇒ Feature

Creates a new instance of an Interval RefreshPolicy.

Examples:

A Feature which is always enabled

feature = EightBall::Feature.new 'feature1', EightBall::Conditions::Always

Parameters:



19
20
21
22
23
# File 'lib/eight_ball/feature.rb', line 19

def initialize(name, enabled_for = [], disabled_for = [])
  @name = name
  @enabled_for = Array enabled_for
  @disabled_for = Array disabled_for
end

Instance Attribute Details

#disabled_forObject (readonly)

Returns the value of attribute disabled_for.



7
8
9
# File 'lib/eight_ball/feature.rb', line 7

def disabled_for
  @disabled_for
end

#enabled_forObject (readonly)

Returns the value of attribute enabled_for.



7
8
9
# File 'lib/eight_ball/feature.rb', line 7

def enabled_for
  @enabled_for
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/eight_ball/feature.rb', line 7

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



51
52
53
54
55
56
57
# File 'lib/eight_ball/feature.rb', line 51

def ==(other)
  name == other.name &&
    enabled_for.size == other.enabled_for.size &&
    enabled_for.all? { |condition| other.enabled_for.any? { |other_condition| condition == other_condition } } &&
    disabled_for.size == other.disabled_for.size &&
    disabled_for.all? { |condition| other.disabled_for.any? { |other_condition| condition == other_condition } }
end

#enabled?(parameters = {}) ⇒ true, false

“EightBall, is this Feature enabled?”

Examples:

The Feature’s Conditions do not require any parameters

feature.enabled?

The Feature’s Conditions require an account ID

feature.enabled? account_id: 123

Parameters:

  • parameters (Hash) (defaults to: {})

    The parameters the Conditions of this Feature are concerned with.

Returns:

Raises:

  • (ArgumentError)

    if no value is provided for a parameter required by one of the Feature’s Conditions



44
45
46
47
48
49
# File 'lib/eight_ball/feature.rb', line 44

def enabled?(parameters = {})
  return true if @enabled_for.empty? && @disabled_for.empty?
  return true if @enabled_for.empty? && !any_satisfied?(@disabled_for, parameters)

  any_satisfied?(@enabled_for, parameters) && !any_satisfied?(@disabled_for, parameters)
end