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 = [], metadata: nil) ⇒ 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:

  • name (String)

    The name of the Feature.

  • enabled_for (Array<EightBall::Conditions>, EightBall::Conditions) (defaults to: [])

    The Condition(s) that need to be satisfied for the Feature to be enabled.

  • disabled_for (Array<EightBall::Conditions>, EightBall::Conditions) (defaults to: [])

    The Condition(s) that need to be satisfied for the Feature to be disabled.

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

    Optional eval-agnostic metadata (e.g. { "type" => ..., "owner" => ..., "expires_at" => ... }).



21
22
23
24
25
26
27
# File 'lib/eight_ball/feature.rb', line 21

def initialize(name, enabled_for = [], disabled_for = [], metadata: nil)
  @name = name
  @enabled_for = inject_flag_name(Array(enabled_for))
  @disabled_for = inject_flag_name(Array(disabled_for))
  @metadata = 
  @un_evaluable = false
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

#metadataObject (readonly)

Returns the value of attribute metadata.



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

def 
  @metadata
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?



71
72
73
74
75
76
77
78
# File 'lib/eight_ball/feature.rb', line 71

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 } } &&
     == other.
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



48
49
50
51
52
53
54
55
# File 'lib/eight_ball/feature.rb', line 48

def enabled?(parameters = {})
  return false if @un_evaluable

  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

#un_evaluable!nil

Marks this Feature un-evaluable: #enabled? always returns false and its Conditions are never inspected. Used by Marshallers to fail a bad flag closed.

Returns:

  • (nil)


61
62
63
64
# File 'lib/eight_ball/feature.rb', line 61

def un_evaluable!
  @un_evaluable = true
  nil
end

#un_evaluable?Boolean

Returns whether this Feature has been marked un-evaluable.

Returns:

  • (Boolean)

    whether this Feature has been marked un-evaluable.



67
68
69
# File 'lib/eight_ball/feature.rb', line 67

def un_evaluable?
  @un_evaluable
end