Module: EightBall

Defined in:
lib/eight_ball.rb,
lib/eight_ball/feature.rb,
lib/eight_ball/version.rb,
lib/eight_ball/configuration_error.rb

Overview

For all your feature querying needs.

Defined Under Namespace

Modules: Conditions, Marshallers, Providers Classes: ConfigurationError, Feature

Constant Summary collapse

VERSION =
'2.1.0'

Class Method Summary collapse

Class Method Details

.disabled?(name, parameters = {}) ⇒ Boolean, true

“EightBall, is the feature named ‘NewFeature’ disabled?”

Examples:

EightBall.disabled? 'feature1', account_id: 1

Parameters:

  • name (String)

    The name of the Feature.

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

    The parameters the Conditions of this Feature are concerned with.

Returns:

  • (Boolean)

    whether or not the Feature is disabled.

  • (true)

    if Feature does not exist.



85
86
87
# File 'lib/eight_ball.rb', line 85

def self.disabled?(name, parameters = {})
  !enabled? name, parameters
end

.enabled?(name, parameters = {}) ⇒ Boolean, false

“EightBall, is the feature named ‘NewFeature’ enabled?”

Examples:

EightBall.enabled? 'feature1', account_id: 1

Parameters:

  • name (String)

    The name of the Feature.

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

    The parameters the Conditions of this Feature are concerned with.

Returns:

  • (Boolean)

    whether or not the Feature is enabled.

  • (false)

    if Feature does not exist.



67
68
69
70
71
72
# File 'lib/eight_ball.rb', line 67

def self.enabled?(name, parameters = {})
  feature = provider.features.find { |f| f.name == name }
  return false unless feature

  feature.enabled? parameters
end

.featuresArray<EightBall::Feature>

Serves as a shortcut to access the Features available on the configured Provider



50
51
52
53
54
# File 'lib/eight_ball.rb', line 50

def self.features
  raise EightBall::ConfigurationError, 'No Provider has been configured; there can be no features. Please see "EightBall.provider="' unless provider

  provider.features
end

.loggerObject



127
128
129
130
131
# File 'lib/eight_ball.rb', line 127

def self.logger
  @logger ||= Logger.new(STDOUT).tap do |log|
    log.progname = name
  end
end

.logger=(logger) ⇒ Object



133
134
135
# File 'lib/eight_ball.rb', line 133

def self.logger=(logger)
  @logger = logger
end

.marshall(marshaller = nil, features = EightBall.features) ⇒ Object

Marshalls the Features. This can be useful for converting the data to, e.g., a JSON file.

If a Marshaller is provided, use it.

If no Marshaller is provided, uses the same Marshaller that the Provider is configured with.

If the Provider does not expose a Marshaller, this will default to the JSON Marshaller.



119
120
121
122
123
124
125
# File 'lib/eight_ball.rb', line 119

def self.marshall(marshaller = nil, features = EightBall.features)
  marshaller ||=
    (provider.respond_to?(:marshaller) && provider.marshaller) ||
    EightBall::Marshallers::Json.new

  marshaller.marshall features
end

.providerEightBall::Providers Provider

Gets the Provider instance EightBall is configured to use

Returns:



42
43
44
# File 'lib/eight_ball.rb', line 42

def self.provider
  @provider
end

.provider=(provider) ⇒ nil

Sets the Provider instance EightBall will use to obtain your list of Features.

Examples:

EightBall.provider = EightBall::Providers::Http.new 'http://www.rewind.io'

Returns:

  • (nil)


34
35
36
# File 'lib/eight_ball.rb', line 34

def self.provider=(provider)
  @provider = provider
end

.with(name, parameters = {}) ⇒ nil, false

Yields to the given block of code if the Feature is enabled.

Examples:

EightBall.with 'feature1', account_id: 1 do
  puts 'Feature is enabled!'
end

Parameters:

  • name (String)

    The name of the Feature.

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

    The parameters the Conditions of this Feature are concerned with.

Returns:

  • (nil)

    if block is yielded to

  • (false)

    if no block is given



102
103
104
105
106
# File 'lib/eight_ball.rb', line 102

def self.with(name, parameters = {})
  return false unless block_given?

  yield if enabled? name, parameters
end