Class: Flipper::DSL

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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.


22
23
24
25
26
27
# File 'lib/flipper/dsl.rb', line 22

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



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

def adapter
  @adapter
end

#instrumenterObject (readonly)

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



13
14
15
# File 'lib/flipper/dsl.rb', line 13

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`.



235
236
237
# File 'lib/flipper/dsl.rb', line 235

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.



254
255
256
# File 'lib/flipper/dsl.rb', line 254

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

#add(name) ⇒ Object

Public: Add a feature.

name - The String or Symbol name of the feature.

Returns result of add.



152
153
154
# File 'lib/flipper/dsl.rb', line 152

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



209
210
211
# File 'lib/flipper/dsl.rb', line 209

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.



99
100
101
# File 'lib/flipper/dsl.rb', line 99

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.



110
111
112
# File 'lib/flipper/dsl.rb', line 110

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.



121
122
123
# File 'lib/flipper/dsl.rb', line 121

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.



143
144
145
# File 'lib/flipper/dsl.rb', line 143

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.



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

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.



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

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.



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

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.



67
68
69
# File 'lib/flipper/dsl.rb', line 67

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.



89
90
91
# File 'lib/flipper/dsl.rb', line 89

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.



78
79
80
# File 'lib/flipper/dsl.rb', line 78

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)


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

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.



170
171
172
173
174
175
176
# File 'lib/flipper/dsl.rb', line 170

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.



262
263
264
# File 'lib/flipper/dsl.rb', line 262

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.



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

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

#import(flipper) ⇒ Object



266
267
268
# File 'lib/flipper/dsl.rb', line 266

def import(flipper)
  adapter.import(flipper.adapter)
end

#preload(names) ⇒ Object

Public: Preload the features with the given names.

names - An Array of String or Symbol names of the features.

Returns an Array of Flipper::Feature.



183
184
185
186
187
# File 'lib/flipper/dsl.rb', line 183

def preload(names)
  features = names.map { |name| feature(name) }
  @adapter.get_multi(features)
  features
end

#preload_allObject

Public: Preload all the adapters features.

Returns an Array of Flipper::Feature.



192
193
194
195
# File 'lib/flipper/dsl.rb', line 192

def preload_all
  keys = @adapter.get_all.keys
  keys.map { |key| feature(key) }
end

#remove(name) ⇒ Object

Public: Remove a feature.

name - The String or Symbol name of the feature.

Returns result of remove.



161
162
163
# File 'lib/flipper/dsl.rb', line 161

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.



244
245
246
# File 'lib/flipper/dsl.rb', line 244

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