Class: SimpleSwitch::FeatureManagerDb

Inherits:
Object
  • Object
show all
Includes:
ManagerSharedMethods
Defined in:
lib/simple_switch/feature_manager_db.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ManagerSharedMethods

#off?, #on?, #turn_off, #turn_on

Constructor Details

#initializeFeatureManagerDb

Returns a new instance of FeatureManagerDb.



6
7
8
# File 'lib/simple_switch/feature_manager_db.rb', line 6

def initialize
  @feature_config = load_config
end

Instance Attribute Details

#feature_configObject (readonly)

Returns the value of attribute feature_config.



4
5
6
# File 'lib/simple_switch/feature_manager_db.rb', line 4

def feature_config
  @feature_config
end

Class Method Details

.instanceObject



10
11
12
# File 'lib/simple_switch/feature_manager_db.rb', line 10

def self.instance
  return @@instance ||= send(:new)
end

Instance Method Details

#add_environment(options) ⇒ Object



31
32
33
34
35
# File 'lib/simple_switch/feature_manager_db.rb', line 31

def add_environment(options)
  SimpleSwitch::Environment.find_or_create_by(name: options[:name])

  reload_config!
end

#add_feature(options) ⇒ Object



20
21
22
23
24
25
# File 'lib/simple_switch/feature_manager_db.rb', line 20

def add_feature(options)
  feature = SimpleSwitch::Feature.find_or_create_by(name: options[:name])
  feature.update(description: options[:description])

  reload_config!
end

#add_state(options) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/simple_switch/feature_manager_db.rb', line 37

def add_state(options)
  feature     = SimpleSwitch::Feature.find_by_name(options[:feature])
  environment = SimpleSwitch::Environment.find_by_name(options[:environment])

  feature.states.create(status: options[:status], environment: environment)
  reload_config!
end

#delete(feature) ⇒ Object



51
52
53
54
55
# File 'lib/simple_switch/feature_manager_db.rb', line 51

def delete(feature)
  feature_config.delete(feature) if valid_feature_name?(feature)

  SimpleSwitch::Feature.find_by_name(feature).destroy
end

#has_environment?(environment) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/simple_switch/feature_manager_db.rb', line 27

def has_environment?(environment)
  SimpleSwitch::Environment.find_by_name(environment).present?
end

#has_feature?(feature) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/simple_switch/feature_manager_db.rb', line 16

def has_feature?(feature)
  SimpleSwitch::Feature.find_by_name(feature).present?
end

#update(feature, env, value) ⇒ Object



45
46
47
48
49
# File 'lib/simple_switch/feature_manager_db.rb', line 45

def update(feature, env, value)
  states(feature)[env][0] = value if valid_feature_name_for_env?(feature, env)

  SimpleSwitch::State.update(states(feature)[env][1], status: value)
end