lib/rails-settings/base.rb in rails-settings-cached-2.5.3 vs lib/rails-settings/base.rb in rails-settings-cached-2.6.0

- old
+ new

@@ -1,12 +1,17 @@ # frozen_string_literal: true module RailsSettings - class Base < ActiveRecord::Base - class SettingNotFound < RuntimeError; end + class ProcetedKeyError < RuntimeError + def initialize(key) + super("Can't use #{key} as setting key.") + end + end + class Base < ActiveRecord::Base SEPARATOR_REGEXP = /[\n,;]+/ + PROTECTED_KEYS = %w[var value] self.table_name = table_name_prefix + "settings" # get the value field, YAML decoded def value # rubocop:disable Security/YAMLLoad @@ -62,10 +67,12 @@ private def _define_field(key, default: nil, type: :string, readonly: false, separator: nil, validates: nil) key = key.to_s + raise ProcetedKeyError.new(key) if PROTECTED_KEYS.include?(key) + @defined_fields ||= [] @defined_fields << { key: key, default: default, type: type || :string, @@ -117,9 +124,16 @@ if type == :boolean define_singleton_method("#{key}?") do send(key) end + end + + # delegate instance get method to class for support: + # setting = Setting.new + # setting.admin_emails + define_method(key) do + self.class.public_send(key) end end def _convert_string_to_typeof_value(type, value, separator: nil) return value unless [String, Integer, Float, BigDecimal].include?(value.class)