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
}.freeze

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|
    create_feature(key, value)
    value = find_by(key: key, klass: klass).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
61
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 54

def define_checker_method(key, &block)
  unless block_given?
    block = proc do
      find_by(key: key, klass: klass)&.enabled ? true : false
    end
  end
  define_singleton_method("#{key}_enabled?") { block.call }
end

.define_disabler_method(key, &block) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 72

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

.define_enabler_method(key, &block) ⇒ Object



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

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

.defined_featuresObject



103
104
105
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 103

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

.disable!(key) ⇒ Object



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

def disable!(key)
  return unless features.key?(key.to_sym)

  record = find_by(key: key, klass: klass)
  record.update(enabled: false)
end

.enable!(key) ⇒ Object



89
90
91
92
93
94
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 89

def enable!(key)
  return unless features.key?(key.to_sym)

  record = find_by(key: key, klass: klass)
  record.update(enabled: true)
end

.featuresObject



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

def features
  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|
    create_feature(key, value)
    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
  new.klass.to_s
end

.method_missing(_method, *_args) ⇒ Object



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

def method_missing(_method, *_args)
  false
end

.remove_old_features!Object



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

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

.reset_features!Object



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

def reset_features!
  init_features! if where(klass: klass).delete_all
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