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.


17
18
19
20
21
22
# File 'lib/flipper/dsl.rb', line 17

def initialize(adapter, options = {})
  @instrumenter = options.fetch(:instrumenter, Instrumenters::Noop)
  memoized = Adapters::Memoizable.new(adapter)
  @adapter = memoized
  @memoized_features = {}
end

Instance Attribute Details

#adapterObject (readonly)

Private



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

def adapter
  @adapter
end

#instrumenterObject (readonly)

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



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

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 does not respond to ‘flipper_id`.



205
206
207
# File 'lib/flipper/dsl.rb', line 205

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.



224
225
226
# File 'lib/flipper/dsl.rb', line 224

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.



178
179
180
# File 'lib/flipper/dsl.rb', line 178

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.



94
95
96
# File 'lib/flipper/dsl.rb', line 94

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

#disable_actor(name, actor) ⇒ Object

Public: Disable a feature for an actor.

name - The String or Symbol name of the feature. actor - a Flipper::Types::Actor instance or an object that responds

to flipper_id.

Returns result of disable.



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

def disable_actor(name, actor)
  feature(name).disable_actor(actor)
end

#disable_group(name, group) ⇒ Object

Public: Disable a feature for a group.

name - The String or Symbol name of the feature. group - a Flipper::Types::Group instance or a String or Symbol name of a

registered group.

Returns result of disable.



116
117
118
# File 'lib/flipper/dsl.rb', line 116

def disable_group(name, group)
  feature(name).disable_group(group)
end

#disable_percentage_of_actors(name) ⇒ Object

Public: Disable a feature for a percentage of actors.

name - The String or Symbol name of the feature. percentage - a Flipper::Types::PercentageOfActors instance or an object

that responds to to_i.

Returns result of disable.



138
139
140
# File 'lib/flipper/dsl.rb', line 138

def disable_percentage_of_actors(name)
  feature(name).disable_percentage_of_actors
end

#disable_percentage_of_time(name) ⇒ Object

Public: Disable a feature a percentage of time.

name - The String or Symbol name of the feature. percentage - a Flipper::Types::PercentageOfTime instance or an object

that responds to to_i.

Returns result of disable.



127
128
129
# File 'lib/flipper/dsl.rb', line 127

def disable_percentage_of_time(name)
  feature(name).disable_percentage_of_time
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.



40
41
42
# File 'lib/flipper/dsl.rb', line 40

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

#enable_actor(name, actor) ⇒ Object

Public: Enable a feature for an actor.

name - The String or Symbol name of the feature. actor - a Flipper::Types::Actor instance or an object that responds

to flipper_id.

Returns result of Feature#enable.



51
52
53
# File 'lib/flipper/dsl.rb', line 51

def enable_actor(name, actor)
  feature(name).enable_actor(actor)
end

#enable_group(name, group) ⇒ Object

Public: Enable a feature for a group.

name - The String or Symbol name of the feature. group - a Flipper::Types::Group instance or a String or Symbol name of a

registered group.

Returns result of Feature#enable.



62
63
64
# File 'lib/flipper/dsl.rb', line 62

def enable_group(name, group)
  feature(name).enable_group(group)
end

#enable_percentage_of_actors(name, percentage) ⇒ Object

Public: Enable a feature for a percentage of actors.

name - The String or Symbol name of the feature. percentage - a Flipper::Types::PercentageOfActors instance or an object

that responds to to_i.

Returns result of Feature#enable.



84
85
86
# File 'lib/flipper/dsl.rb', line 84

def enable_percentage_of_actors(name, percentage)
  feature(name).enable_percentage_of_actors(percentage)
end

#enable_percentage_of_time(name, percentage) ⇒ Object

Public: Enable a feature a percentage of time.

name - The String or Symbol name of the feature. percentage - a Flipper::Types::PercentageOfTime instance or an object

that responds to to_i.

Returns result of Feature#enable.



73
74
75
# File 'lib/flipper/dsl.rb', line 73

def enable_percentage_of_time(name, percentage)
  feature(name).enable_percentage_of_time(percentage)
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)


30
31
32
# File 'lib/flipper/dsl.rb', line 30

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.



156
157
158
159
160
161
162
163
164
# File 'lib/flipper/dsl.rb', line 156

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

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

Returns Set of Flipper::Feature instances.



232
233
234
# File 'lib/flipper/dsl.rb', line 232

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.



195
196
197
# File 'lib/flipper/dsl.rb', line 195

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

#remove(name) ⇒ Object

Public: Remove a feature.

name - The String or Symbol name of the feature.

Returns result of remove.



147
148
149
# File 'lib/flipper/dsl.rb', line 147

def remove(name)
  feature(name).remove
end

#time(number) ⇒ Object Also known as: percentage_of_time

Public: Shortcut for getting a percentage of time instance.

number - The percentage of time that should be enabled.

Returns Flipper::Types::PercentageOfTime.



214
215
216
# File 'lib/flipper/dsl.rb', line 214

def time(number)
  Types::PercentageOfTime.new(number)
end