class FeatureSetting::FsSetting
Constants
- SETTINGS
Public Class Methods
cache_settings!()
click to toggle source
# File lib/feature_setting/orm/active_record/fs_setting.rb, line 49 def cache_settings! settings.each do |key, value| create_setting(key, value) record = find_by(key: key, klass: klass) value = ConvertValue.convert_to_type(record.value, record.value_type) define_getter_method(key) { value } end end
define_getter_method(key, &block)
click to toggle source
# File lib/feature_setting/orm/active_record/fs_setting.rb, line 58 def define_getter_method(key, &block) unless block_given? block = proc do record = find_by(key: key, klass: klass) ConvertValue.convert_to_type(record.value, record.value_type) end end define_singleton_method(key.to_s) { block.call } end
define_setter_method(key)
click to toggle source
# File lib/feature_setting/orm/active_record/fs_setting.rb, line 69 def define_setter_method(key) define_singleton_method("#{key}=") do |value| set!(key, value) end end
defined_keys()
click to toggle source
# File lib/feature_setting/orm/active_record/fs_setting.rb, line 116 def defined_keys settings.keys.map(&:to_s) end
existing_key(key = nil, hash = {})
click to toggle source
rubocop:enable Metrics/AbcSize, Metrics/MethodLength
# File lib/feature_setting/orm/active_record/fs_setting.rb, line 108 def existing_key(key = nil, hash = {}) return unless settings.key?(hash.keys.first) || settings.key?(key.to_sym) hash.keys.first || key.to_sym rescue StandardError nil end
init_settings!(remove_old_settings: false)
click to toggle source
# File lib/feature_setting/orm/active_record/fs_setting.rb, line 40 def init_settings!(remove_old_settings: false) settings.each do |key, value| create_setting(key, value) define_getter_method(key) define_setter_method(key) end remove_old_settings! if remove_old_settings end
klass()
click to toggle source
# File lib/feature_setting/orm/active_record/fs_setting.rb, line 36 def klass new.klass.to_s end
remove_old_settings!()
click to toggle source
# File lib/feature_setting/orm/active_record/fs_setting.rb, line 75 def remove_old_settings! where(klass: klass, key: all_stored_keys - defined_keys).delete_all end
reset_settings!()
click to toggle source
# File lib/feature_setting/orm/active_record/fs_setting.rb, line 79 def reset_settings! init_settings! if where(klass: klass).delete_all end
set!(key = nil, value = nil)
click to toggle source
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
# File lib/feature_setting/orm/active_record/fs_setting.rb, line 84 def set!(key = nil, value = nil) raise SettingNotExistsError unless defined_keys.include?(key.to_s) record = find_by(key: key.to_s, klass: klass) old_value = ConvertValue.convert_to_type(record.value, record.value_type) if record.value_type == 'Hash' raise SettingTypeMismatchError 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: ConvertValue.convert_to_string(new_value, new_value.class.to_s), value_type: value_type ) end
Also aliased as: update!
settings()
click to toggle source
# File lib/feature_setting/orm/active_record/fs_setting.rb, line 32 def settings new.settings end
stored_settings()
click to toggle source
# File lib/feature_setting/orm/active_record/fs_setting.rb, line 120 def stored_settings hash = {} where(klass: klass).each do |record| hash[record.key.to_sym] = ConvertValue.convert_to_type(record.value, record.value_type) end hash end
Private Class Methods
all_stored_keys()
click to toggle source
# File lib/feature_setting/orm/active_record/fs_setting.rb, line 131 def all_stored_keys all.pluck(:key) end
create_setting(key, value)
click to toggle source
# File lib/feature_setting/orm/active_record/fs_setting.rb, line 135 def create_setting(key, value) create_with( key: key, value: ConvertValue.convert_to_string(value, value.class.to_s), value_type: value.class.to_s, klass: klass ).find_or_create_by( klass: klass, key: key ) end
Public Instance Methods
klass()
click to toggle source
# File lib/feature_setting/orm/active_record/fs_setting.rb, line 27 def klass self.class end
settings()
click to toggle source
# File lib/feature_setting/orm/active_record/fs_setting.rb, line 23 def settings self.class::SETTINGS end