Class: FeatureSetting::FsSetting
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- FeatureSetting::FsSetting
- Defined in:
- lib/feature_setting/orm/active_record/fs_setting.rb
Constant Summary collapse
- SETTINGS =
{ test: 'value' }.freeze
Class Method Summary collapse
- .cache_settings! ⇒ Object
- .define_getter_method(key, &block) ⇒ Object
- .define_setter_method(key) ⇒ Object
- .defined_keys ⇒ Object
- .existing_key(key = nil, hash = {}) ⇒ Object
- .init_settings!(remove_old_settings = false) ⇒ Object
- .klass ⇒ Object
- .remove_old_settings! ⇒ Object
- .reset_settings! ⇒ Object
- .set!(key = nil, value = nil) ⇒ Object (also: update!)
- .settings ⇒ Object
- .stored_settings ⇒ Object
Instance Method Summary collapse
Class Method Details
.cache_settings! ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/feature_setting/orm/active_record/fs_setting.rb', line 36 def cache_settings! settings.each do |key, value| create_with(key: key, value: convert_to_string(value, value.class.to_s), value_type: value.class.to_s, klass: klass).find_or_create_by(klass: klass, key: key) record = where(key: key, klass: klass).first value = convert_to_type(record.value, record.value_type) define_getter_method(key) { value } end end |
.define_getter_method(key, &block) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/feature_setting/orm/active_record/fs_setting.rb', line 45 def define_getter_method(key, &block) block = Proc.new do record = where(key: key, klass: klass).first convert_to_type(record.value, record.value_type) end unless block_given? define_singleton_method(key.to_s) { block.call } end |
.define_setter_method(key) ⇒ Object
54 55 56 57 58 |
# File 'lib/feature_setting/orm/active_record/fs_setting.rb', line 54 def define_setter_method(key) define_singleton_method("#{key}=") do |value| set!(key, value) end end |
.defined_keys ⇒ Object
99 100 101 |
# File 'lib/feature_setting/orm/active_record/fs_setting.rb', line 99 def defined_keys settings.keys.map(&:to_s) end |
.existing_key(key = nil, hash = {}) ⇒ Object
92 93 94 95 96 97 |
# File 'lib/feature_setting/orm/active_record/fs_setting.rb', line 92 def existing_key(key = nil, hash = {}) settings.key?(hash.keys.first) || settings.key?(key.to_sym) hash.keys.first || key.to_sym rescue nil end |
.init_settings!(remove_old_settings = false) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/feature_setting/orm/active_record/fs_setting.rb', line 27 def init_settings!(remove_old_settings = false) settings.each do |key, value| create_with(key: key, value: convert_to_string(value, value.class.to_s), value_type: value.class.to_s, klass: klass).find_or_create_by(klass: klass, key: key) define_getter_method(key) define_setter_method(key) end remove_old_settings! if remove_old_settings end |
.klass ⇒ Object
23 24 25 |
# File 'lib/feature_setting/orm/active_record/fs_setting.rb', line 23 def klass new.klass.to_s end |
.remove_old_settings! ⇒ Object
60 61 62 |
# File 'lib/feature_setting/orm/active_record/fs_setting.rb', line 60 def remove_old_settings! where(klass: klass, key: all_stored_keys - defined_keys).destroy_all end |
.reset_settings! ⇒ Object
64 65 66 67 |
# File 'lib/feature_setting/orm/active_record/fs_setting.rb', line 64 def reset_settings! where(klass: klass).destroy_all init_settings! end |
.set!(key = nil, value = nil) ⇒ Object Also known as: update!
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/feature_setting/orm/active_record/fs_setting.rb', line 69 def set!(key = nil, value = nil) raise 'ERROR: FsSetting key is missing or does not exist.' unless defined_keys.include?(key.to_s) record = where(key: key.to_s, klass: klass).first old_value = convert_to_type(record.value, record.value_type) if record.value_type == 'Hash' raise 'ERROR: The value for a setting of type Hash must be a Hash.' unless value.is_a?(Hash) new_value = old_value.update(value) value_type = 'Hash' else new_value = value value_type = value.class.to_s end record.update( value: convert_to_string(new_value, new_value.class.to_s), value_type: value_type ) end |
.settings ⇒ Object
19 20 21 |
# File 'lib/feature_setting/orm/active_record/fs_setting.rb', line 19 def settings new.settings end |
.stored_settings ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/feature_setting/orm/active_record/fs_setting.rb', line 103 def stored_settings hash = {} where(klass: klass).each do |record| hash[record.key.to_sym] = convert_to_type(record.value, record.value_type) end hash end |
Instance Method Details
#klass ⇒ Object
14 15 16 |
# File 'lib/feature_setting/orm/active_record/fs_setting.rb', line 14 def klass self.class end |
#settings ⇒ Object
10 11 12 |
# File 'lib/feature_setting/orm/active_record/fs_setting.rb', line 10 def settings self.class::SETTINGS end |