Class: FeatureSetting::FsFeature

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/feature_setting/orm/active_record/fs_feature.rb

Constant Summary collapse

FEATURES =
{
  test: false
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cache_features!Object



44
45
46
47
48
49
50
51
52
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 44

def cache_features!
  features.each do |key, value|
    self.create_with(key: key, enabled: value, klass: klass).find_or_create_by(klass: klass, key: key)
    value = self.where(key: key, klass: klass).first.enabled
    define_checker_method(key) { value }
    define_enabler_method(key) { false }
    define_disabler_method(key) { false }
  end
end

.define_checker_method(key, &block) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 54

def define_checker_method(key, &block)
  block = Proc.new do
    record = self.where(key: key, klass: klass).first
    record.enabled
  end unless block_given?
  define_singleton_method("#{key}_enabled?") { block.call }
end

.define_disabler_method(key, &block) ⇒ Object



69
70
71
72
73
74
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 69

def define_disabler_method(key, &block)
  block = Proc.new do
    disable!(key)
  end unless block_given?
  define_singleton_method("disable_#{key}!") { block.call }
end

.define_enabler_method(key, &block) ⇒ Object



62
63
64
65
66
67
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 62

def define_enabler_method(key, &block)
  block = Proc.new do
    enable!(key)
  end unless block_given?
  define_singleton_method("enable_#{key}!") { block.call }
end

.defined_featuresObject



99
100
101
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 99

def defined_features
  features.keys.map(&:to_s)
end

.disable!(key) ⇒ Object



92
93
94
95
96
97
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 92

def disable!(key)
  if features.key?(key.to_sym)
    record = self.where(key: key, klass: klass).first
    record.update(enabled: false)
  end
end

.enable!(key) ⇒ Object



85
86
87
88
89
90
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 85

def enable!(key)
  if features.key?(key.to_sym)
    record = self.where(key: key, klass: klass).first
    record.update(enabled: true)
  end
end

.featuresObject



26
27
28
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 26

def features
  self.new.features
end

.init_features!(remove_old_features = false) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 34

def init_features!(remove_old_features = false)
  features.each do |key, value|
    self.create_with(key: key, enabled: value, klass: klass).find_or_create_by(klass: klass, key: key)
    define_checker_method(key)
    define_enabler_method(key)
    define_disabler_method(key)
  end
  remove_old_features! if remove_old_features
end

.klassObject



30
31
32
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 30

def klass
  self.new.klass.to_s
end

.method_missing(m, *args) ⇒ Object



18
19
20
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 18

def method_missing(m, *args)
  false
end

.remove_old_features!Object



76
77
78
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 76

def remove_old_features!
  self.where(key: all_stored_features - defined_features).destroy_all
end

.reset_features!Object



80
81
82
83
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 80

def reset_features!
  self.where(klass: klass).destroy_all
  init_features!
end

.respond_to_missing?(*args) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 22

def respond_to_missing?(*args)
  true
end

Instance Method Details

#featuresObject



9
10
11
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 9

def features
  self.class::FEATURES
end

#klassObject



13
14
15
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 13

def klass
  self.class
end