Class: Flipper::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/flipper/dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, options = {}) ⇒ DSL

Public: Returns a new instance of the DSL.

adapter - The adapter that this DSL instance should use. options - The Hash of options.

:instrumenter - What should be used to instrument all the things.


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/flipper/dsl.rb', line 18

def initialize(adapter, options = {})
  @instrumenter = options.fetch(:instrumenter, Flipper::Instrumenters::Noop)

  instrumented = Flipper::Adapters::Instrumented.new(adapter, {
    :instrumenter => @instrumenter,
  })
  memoized = Flipper::Adapters::Memoizable.new(instrumented)
  @adapter = memoized

  @memoized_features = {}
end

Instance Attribute Details

#adapterObject (readonly)

Private



8
9
10
# File 'lib/flipper/dsl.rb', line 8

def adapter
  @adapter
end

#instrumenterObject (readonly)

Private: What is being used to instrument all the things.



11
12
13
# File 'lib/flipper/dsl.rb', line 11

def instrumenter
  @instrumenter
end

Instance Method Details

#actor(thing) ⇒ Object

Public: Wraps an object as a flipper actor.

thing - The object that you would like to wrap.

Returns an instance of Flipper::Types::Actor. Raises ArgumentError if thing not wrappable.



114
115
116
# File 'lib/flipper/dsl.rb', line 114

def actor(thing)
  Types::Actor.new(thing)
end

#actors(number) ⇒ Object Also known as: percentage_of_actors

Public: Shortcut for getting a percentage of actors instance.

number - The percentage of actors that should be enabled.

Returns Flipper::Types::PercentageOfActors.



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

def actors(number)
  Types::PercentageOfActors.new(number)
end

#boolean(value = true) ⇒ Object Also known as: bool

Public: Shortcut for getting a boolean type instance.

value - The true or false value for the boolean.

Returns a Flipper::Types::Boolean instance.



87
88
89
# File 'lib/flipper/dsl.rb', line 87

def boolean(value = true)
  Types::Boolean.new(value)
end

#disable(name, *args) ⇒ Object

Public: Disable a feature.

name - The String or Symbol name of the feature. args - The args passed through to the feature instance enable call.

Returns the result of the feature instance disable call.



56
57
58
# File 'lib/flipper/dsl.rb', line 56

def disable(name, *args)
  feature(name).disable(*args)
end

#enable(name, *args) ⇒ Object

Public: Enable a feature.

name - The String or Symbol name of the feature. args - The args passed through to the feature instance enable call.

Returns the result of the feature instance enable call.



46
47
48
# File 'lib/flipper/dsl.rb', line 46

def enable(name, *args)
  feature(name).enable(*args)
end

#enabled?(name, *args) ⇒ Boolean

Public: Check if a feature is enabled.

name - The String or Symbol name of the feature. args - The args passed through to the enabled check.

Returns true if feature is enabled, false if not.

Returns:

  • (Boolean)


36
37
38
# File 'lib/flipper/dsl.rb', line 36

def enabled?(name, *args)
  feature(name).enabled?(*args)
end

#feature(name) ⇒ Object Also known as: []

Public: Access a feature instance by name.

name - The String or Symbol name of the feature.

Returns an instance of Flipper::Feature.



65
66
67
68
69
70
71
72
73
# File 'lib/flipper/dsl.rb', line 65

def feature(name)
  if !name.is_a?(String) && !name.is_a?(Symbol)
    raise ArgumentError, "#{name} must be a String or Symbol"
  end

  @memoized_features[name.to_sym] ||= Feature.new(name, @adapter, {
    :instrumenter => instrumenter,
  })
end

#featuresObject

Internal: Returns a Set of the known features for this adapter.

Returns Set of Flipper::Feature instances.



141
142
143
# File 'lib/flipper/dsl.rb', line 141

def features
  adapter.features.map { |name| feature(name) }.to_set
end

#group(name) ⇒ Object

Public: Access a flipper group by name.

name - The String or Symbol name of the feature.

Returns an instance of Flipper::Group. Raises Flipper::GroupNotRegistered if group has not been registered.



104
105
106
# File 'lib/flipper/dsl.rb', line 104

def group(name)
  Flipper.group(name)
end

#random(number) ⇒ Object Also known as: percentage_of_random

Public: Shortcut for getting a percentage of random instance.

number - The percentage of random that should be enabled.

Returns Flipper::Types::PercentageOfRandom.



123
124
125
# File 'lib/flipper/dsl.rb', line 123

def random(number)
  Types::PercentageOfRandom.new(number)
end