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.5.1'.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)

NOTE: This method will be deprecated soon.



128
129
130
131
132
133
134
# File 'lib/fflags.rb', line 128

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

  return super unless method_name.to_s.end_with?('?')

  api.enabled?(flag_name)
end

Class Method Details

.allObject

Returns all supported flags.

Ex.

FFlags.all


41
42
43
# File 'lib/fflags.rb', line 41

def all
  api.flags
end

.apiObject



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

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
22
# File 'lib/fflags.rb', line 18

def config
  yield configuration
  reset_api
  api.load_flags
end

.configurationObject



145
146
147
# File 'lib/fflags.rb', line 145

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)


76
77
78
# File 'lib/fflags.rb', line 76

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)


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

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

.method_missing(method_name, *args) ⇒ Object

NOTE: This method will be deprecated soon.



128
129
130
131
132
133
134
# File 'lib/fflags.rb', line 128

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

  return super unless method_name.to_s.end_with?('?')

  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


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

def reset
  api.reset
end

.reset_apiObject



24
25
26
# File 'lib/fflags.rb', line 24

def reset_api
  @api = nil
end

.reset_configObject

Reset the whole config to the default values

Ex.

FFlags.reset_config


32
33
34
35
# File 'lib/fflags.rb', line 32

def reset_config
  @configuration = nil
  reset
end

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

Returns:

  • (Boolean)


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

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


89
90
91
92
93
94
95
96
97
98
# File 'lib/fflags.rb', line 89

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)


58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fflags.rb', line 58

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


49
50
51
# File 'lib/fflags.rb', line 49

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)


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

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