class SimpleFeatureFlags::RamStorage
Attributes
file[R]
flags[R]
mandatory_flags[R]
Public Class Methods
new(file)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 9 def initialize(file) @file = file @redis = redis @mandatory_flags = [] @flags = {} import_flags_from_file end
Public Instance Methods
activate(feature)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 91 def activate(feature) return false unless exists?(feature) flags[feature.to_sym]['active'] = 'globally' true end
Also aliased as: activate_globally
activate_for(feature, objects, object_id_method = CONFIG.default_id_method)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 109 def activate_for(feature, objects, object_id_method = CONFIG.default_id_method) return false unless exists?(feature) objects = [objects] unless objects.is_a? ::Array to_activate_hash = objects_to_hash(objects, object_id_method) active_objects_hash = active_objects(feature) to_activate_hash.each do |klass, ids| (active_objects_hash[klass] = ids) && next unless active_objects_hash[klass] active_objects_hash[klass].concat(ids).uniq!.sort! end flags[feature.to_sym]['active_for_objects'] = active_objects_hash true end
activate_for!(feature, objects, object_id_method = CONFIG.default_id_method)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 127 def activate_for!(feature, objects, object_id_method = CONFIG.default_id_method) return false unless activate_for(feature, objects, object_id_method) activate_partially(feature) end
activate_partially(feature)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 101 def activate_partially(feature) return false unless exists?(feature) flags[feature.to_sym]['active'] = 'partially' true end
active(feature)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 18 def active(feature) case flags.dig(feature.to_sym, 'active') when 'globally', :globally :globally when 'partially', :partially :partially when 'true', true true when 'false', false false end end
active?(feature)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 31 def active?(feature) return true if active(feature) false end
active_for?(feature, object, object_id_method = CONFIG.default_id_method)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 45 def active_for?(feature, object, object_id_method = CONFIG.default_id_method) return false unless active?(feature) return true if active_globally?(feature) active_objects_hash = active_objects(feature) active_ids = active_objects_hash[object.class.to_s] return false unless active_ids active_ids.include? object.public_send(object_id_method) end
active_globally?(feature)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 37 def active_globally?(feature) ACTIVE_GLOBALLY.include? flags.dig(feature.to_sym, 'active') end
active_objects(feature)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 150 def active_objects(feature) flags.dig(feature.to_sym, 'active_for_objects') || {} end
active_partially?(feature)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 41 def active_partially?(feature) ACTIVE_PARTIALLY.include? flags.dig(feature.to_sym, 'active') end
add(feature, description, active = 'false')
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 182 def add(feature, description, active = 'false') return false if exists?(feature) active = if ACTIVE_GLOBALLY.include?(active) 'globally' elsif ACTIVE_PARTIALLY.include?(active) 'partially' else 'false' end hash = { 'name' => feature.to_s, 'active' => active, 'description' => description } flags[feature.to_sym] = hash end
all()
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 211 def all hashes = [] flags.each do |key, _val| hashes << get(key) end hashes end
deactivate(feature)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 142 def deactivate(feature) return false unless exists?(feature) flags[feature.to_sym]['active'] = 'false' true end
deactivate!(feature)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 133 def deactivate!(feature) return false unless exists?(feature) flags[feature.to_sym]['active'] = 'false' flags[feature.to_sym]['active_for_objects'] = nil true end
deactivate_for(feature, objects, object_id_method = CONFIG.default_id_method)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 154 def deactivate_for(feature, objects, object_id_method = CONFIG.default_id_method) return false unless exists?(feature) active_objects_hash = active_objects(feature) objects_to_deactivate_hash = objects_to_hash(objects, object_id_method) objects_to_deactivate_hash.each do |klass, ids_to_remove| active_ids = active_objects_hash[klass] next unless active_ids active_ids.reject! { |id| ids_to_remove.include? id } end flags[feature.to_sym]['active_for_objects'] = active_objects_hash true end
description(feature)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 63 def description(feature) flags.dig(feature.to_sym, 'description') end
exists?(feature)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 57 def exists?(feature) return false if [nil, ''].include? flags[feature.to_sym] true end
get(feature)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 173 def get(feature) return unless exists?(feature) hash = flags[feature.to_sym] hash['mandatory'] = mandatory_flags.include?(feature.to_s) hash end
namespaced_redis()
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 223 def namespaced_redis; end
redis()
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 221 def redis; end
remove(feature)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 202 def remove(feature) return false unless exists?(feature) removed = get(feature) flags.delete(feature.to_sym) removed end
when_active(feature, &block)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 67 def when_active(feature, &block) return unless active?(feature) block.call end
when_active_for(feature, object, object_id_method = CONFIG.default_id_method, &block)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 85 def when_active_for(feature, object, object_id_method = CONFIG.default_id_method, &block) return unless active_for?(feature, object, object_id_method) block.call end
when_active_globally(feature, &block)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 73 def when_active_globally(feature, &block) return unless active_globally?(feature) block.call end
when_active_partially(feature, &block)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 79 def when_active_partially(feature, &block) return unless active_partially?(feature) block.call end
Private Instance Methods
import_flags_from_file()
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 233 def import_flags_from_file changes = YAML.load_file(file) changes = { mandatory: [], remove: [] } unless changes.is_a? ::Hash changes[:mandatory].each do |el| mandatory_flags << el['name'] add(el['name'], el['description'], el['active']) end changes[:remove].each do |el| remove(el) end end
objects_to_hash(objects, object_id_method = CONFIG.default_id_method)
click to toggle source
# File lib/simple_feature_flags/ram_storage.rb, line 227 def objects_to_hash(objects, object_id_method = CONFIG.default_id_method) objects = [objects] unless objects.is_a? ::Array objects.group_by { |ob| ob.class.to_s }.transform_values { |arr| arr.map(&object_id_method) } end