class SimpleSwitch::FeatureManagerYaml

Attributes

feature_config[R]
file_dir[R]
file_name[R]

Public Class Methods

instance() click to toggle source
# File lib/simple_switch/feature_manager_yaml.rb, line 12
def self.instance
  return @@instance ||= send(:new)
end

Private Class Methods

new() click to toggle source
# File lib/simple_switch/feature_manager_yaml.rb, line 6
def initialize
  @file_dir       = SimpleSwitch.feature_config_file_dir
  @file_name      = SimpleSwitch.feature_config_file_name
  @feature_config = load_config
end

Public Instance Methods

delete(feature) click to toggle source
# File lib/simple_switch/feature_manager_yaml.rb, line 24
def delete(feature)
  feature_config.delete(feature) if valid_feature_name?(feature)

  save_to_yaml
end
update(feature, env, value) click to toggle source
# File lib/simple_switch/feature_manager_yaml.rb, line 18
def update(feature, env, value)
  states(feature)[env] = value if valid_feature_name_for_env?(feature, env)

  save_to_yaml
end

Private Instance Methods

file_path() click to toggle source
# File lib/simple_switch/feature_manager_yaml.rb, line 32
def file_path
  Rails.root.join(file_dir, file_name)
end
load_config() click to toggle source
# File lib/simple_switch/feature_manager_yaml.rb, line 36
def load_config
  HashWithIndifferentAccess.new(YAML::load(File.open(file_path)))
end
save_to_yaml() click to toggle source
# File lib/simple_switch/feature_manager_yaml.rb, line 40
def save_to_yaml
  begin
    File.open(file_path, 'w') do |f|
      f.puts @feature_config.to_hash.to_yaml
    end

    true
  rescue
    false
  end
end
valid_feature_name_for_env_with_message?(feature, env=Rails.env) click to toggle source
# File lib/simple_switch/feature_manager_yaml.rb, line 58
def valid_feature_name_for_env_with_message?(feature, env=Rails.env)
  return true if valid_feature_name_for_env_without_message?(feature, env)

  raise "Cannot find environment '#{env}' for feature '#{feature}', "\
        "check out your #{file_name} file."
end
valid_feature_name_with_message?(feature) click to toggle source
# File lib/simple_switch/feature_manager_yaml.rb, line 52
def valid_feature_name_with_message?(feature)
  return true if valid_feature_name_without_message?(feature)

  raise "Cannot find feature '#{feature}', check out your #{file_name} file."
end