Module: SettingAccessors::Integration::ClassMethods

Defined in:
lib/setting_accessors/integration.rb

Instance Method Summary collapse

Instance Method Details

#setting_accessor(setting_name, options = {}) ⇒ Object

Generates a new accessor (=getter and setter) for the given setting

Options Hash (options):

  • :fallback (Symbol, Object) — default: nil

    If set to :default, the getter will return the setting’s default value if no own value was specified for this record

    If set to :global, the getter will try to find a global setting if no record specific setting was found

    If set to another value, this value is used by default

    If not set at all or to nil, the getter will only search for a record specific setting and return nil if none was specified previously.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/setting_accessors/integration.rb', line 38

def setting_accessor(setting_name, options = {})
  fallback = options.delete(:fallback)

  SettingAccessors::Internal.set_class_setting(self, setting_name, options)

  setting_type = SettingAccessors::Internal.setting_value_type(setting_name, self).to_sym

  #Add the setting's name to the list of setting_accessors for this class
  SettingAccessors::Internal.add_setting_accessor_name(self, setting_name)

  # Getter
  define_method(setting_name) do
    settings.get_with_fallback(setting_name, fallback)
  end

  # Getter alias for boolean settings
  alias_method "#{setting_name}?", setting_name if setting_type == :boolean

  # Setter
  define_method("#{setting_name}=") do |new_value|
    settings[setting_name] = new_value
  end

  #NAME_was
  define_method("#{setting_name}_was") do
    settings.value_was(setting_name, fallback)
  end

  #NAME_before_type_cast
  define_method("#{setting_name}_before_type_cast") do
    settings.value_before_type_cast(setting_name)
  end

  #NAME_changed?
  define_method("#{setting_name}_changed?") do
    settings.value_changed?(setting_name)
  end
end