Class: OFFS::Flags

Inherits:
Object
  • Object
show all
Defined in:
lib/offs/flags.rb

Constant Summary collapse

UndefinedFlagError =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



6
7
8
# File 'lib/offs/flags.rb', line 6

def instance
  @instance ||= new
end

.set(&block) ⇒ Object



10
11
12
13
# File 'lib/offs/flags.rb', line 10

def set(&block)
  block.call(instance)
  instance
end

Instance Method Details

#enabled?(flag) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
# File 'lib/offs/flags.rb', line 29

def enabled?(flag)
  status = feature_flags[flag]
  if status.respond_to?(:call)
    status.call
  else
    !!status
  end
end

#feature_flagsObject



25
26
27
# File 'lib/offs/flags.rb', line 25

def feature_flags
  @feature_flags ||= {}
end

#flag(name, default) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/offs/flags.rb', line 16

def flag(name, default)
  env_var_name = name.to_s.upcase
  feature_flags[name] = if ENV.has_key?(env_var_name)
                          ENV[env_var_name].strip == '1'
                        else
                          default
                        end
end

#to_aObject



50
51
52
# File 'lib/offs/flags.rb', line 50

def to_a
  feature_flags.keys
end

#valid?(flag) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/offs/flags.rb', line 38

def valid?(flag)
  feature_flags.has_key?(flag)
end

#validate!(flag) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/offs/flags.rb', line 42

def validate!(flag)
  if valid?(flag)
    flag
  else
    raise UndefinedFlagError, "The #{flag} flag has not been defined."
  end
end