Module: FFlags

Defined in:
lib/fflags.rb,
lib/fflags/api.rb,
lib/fflags/version.rb,
lib/fflags/redis_client.rb,
lib/fflags/configuration.rb

Overview

FFlags module

Defined Under Namespace

Classes: Api, Configuration, RedisClient

Constant Summary collapse

VERSION =
'0.4.3'.freeze

Class Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object (private)



122
123
124
125
126
127
128
129
130
131
# File 'lib/fflags.rb', line 122

def method_missing(method_name, *args)
  flag_name = method_name[0..-2]

  if !method_name.to_s.end_with?('?') ||
     !all.include?(flag_name)
    return super
  end

  api.enabled?(flag_name)
end

Class Method Details

.allObject

Returns all supported flags.

Ex.

FFlags.all


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

def all
  api.flags
end

.apiObject



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

def api
  @api ||= Api.new
end

.config {|configuration| ... } ⇒ Object

Sets FFlags configuration using a block that will be invoked on initialization.

Ex.

FFlags.config do |config|
  config.flags = { flag_1: true, flag_2: false }
end

Yields:



18
19
20
21
# File 'lib/fflags.rb', line 18

def config
  yield configuration
  api.load_flags
end

.configurationObject



142
143
144
# File 'lib/fflags.rb', line 142

def configuration
  @configuration ||= Configuration.new
end

.enabled?(flag_name) ⇒ Boolean

Check if the flag is enabled, it returns true | false.

Ex.

FFlags.enabled?(:new_flag)

Returns:

  • (Boolean)


71
72
73
# File 'lib/fflags.rb', line 71

def enabled?(flag_name)
  api.enabled?(flag_name)
end

.get(flag_name) ⇒ Object

Gets value (as String) of a given flag.

Ex.

FFlags.get(:new_flag)


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

def get(flag_name)
  api.get_flag(flag_name)
end

.method_missing(method_name, *args) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/fflags.rb', line 122

def method_missing(method_name, *args)
  flag_name = method_name[0..-2]

  if !method_name.to_s.end_with?('?') ||
     !all.include?(flag_name)
    return super
  end

  api.enabled?(flag_name)
end

.resetObject

Reset all the flags to the default value. The default values are as defined in the config under flags attribute.

Ex.

FFlags.reset


118
119
120
# File 'lib/fflags.rb', line 118

def reset
  api.reset
end

.reset_configObject

Reset the whole config to the default values

Ex.

FFlags.reset_config


27
28
29
30
# File 'lib/fflags.rb', line 27

def reset_config
  @configuration = nil
  reset
end

.respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to_missing?(method_name, include_private = false)
  flag_name = method_name[0..-2]
  method_name.to_s.end_with?('?') && all.include?(flag_name) || super
end

.set(flag_name, bool) ⇒ Object

Sets flag.

Ex.

FFlags.set(:new_flag, true)

(Not thread safe)
FFlags.set(:new_flag, true) do
  puts 'hello'
end


84
85
86
87
88
89
90
91
92
93
# File 'lib/fflags.rb', line 84

def set(flag_name, bool)
  if block_given?
    prev_flag = get(flag_name)
    api.set_flag(flag_name, bool)
    yield
    api.set_flag(flag_name, prev_flag)
  else
    api.set_flag(flag_name, bool)
  end
end

.set_as_template(template) ⇒ Object

Sets as given template. It will returns false if the template doesn’t exists

Ex.

FFlags.set_as_template(:template_name)


53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fflags.rb', line 53

def set_as_template(template)
  template = templates[template.to_sym] || templates[template.to_s]

  return false unless template

  status = true
  template.each_pair do |key, value|
    status &&= set(key, value)
  end

  status
end

.templatesObject

Returns all templates.

Ex.

FFlags.templates


44
45
46
# File 'lib/fflags.rb', line 44

def templates
  configuration.templates
end

.toggle(flag_name) ⇒ Object

Toggle the given flag. If its true, then the flag would be set to false. If its false, then the flag would be set to true.

Ex.

FFlags.toggle(:new_flag)


109
110
111
# File 'lib/fflags.rb', line 109

def toggle(flag_name)
  api.toggle_flag(flag_name)
end