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
62
# File 'lib/feature_setting/orm/active_record/fs_feature.rb', line 54

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

.define_disabler_method(key, &block) ⇒ Object



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

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



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

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



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

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

.disable!(key) ⇒ Object



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

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



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

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



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

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

.reset_features!Object



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

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