Class: SettingAccessors::SettingSet

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/setting_accessors/setting_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#ensure_nested_hash!, #lookup_nested_hash

Constructor Details

#initialize(record) ⇒ SettingSet

Returns a new instance of SettingSet.



12
13
14
15
# File 'lib/setting_accessors/setting_set.rb', line 12

def initialize(record)
  @record = record
  @temp_settings = {}
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



10
11
12
# File 'lib/setting_accessors/setting_set.rb', line 10

def record
  @record
end

Instance Method Details

#changed_settingsHash

Returns the settings which were actually changed since the last persist!.

Returns:

  • (Hash)

    the settings which were actually changed since the last persist!



107
108
109
# File 'lib/setting_accessors/setting_set.rb', line 107

def changed_settings
  @temp_settings.select { |k, _| value_changed?(k) }
end

#get(key) ⇒ Object Also known as: []

Tries to retrieve the given setting’s value from the temp settings (already read/written values in this instance). If the setting hasn’t been used before, its value is retrieved from the database.

If a setting hasn’t been read by this record (instance) before, its value is stored in the local read set.



25
26
27
28
29
30
31
# File 'lib/setting_accessors/setting_set.rb', line 25

def get(key)
  return @temp_settings[key.to_sym] if key?(key)

  @temp_settings[key.to_sym] = current_database_value!(key)
rescue SettingNotFoundError
  nil
end

#get_or_default(key, store_default: true) ⇒ Object

Tries to find a setting for this record. If none is found, will return the default setting value specified in the setting accessor call

Parameters:

  • store_default (Boolean) (defaults to: true)

    If set to true, the setting’s default value is written to the temporary settings for faster access. Otherwise, a database lookup is performed every time.



59
60
61
62
63
64
65
66
# File 'lib/setting_accessors/setting_set.rb', line 59

def get_or_default(key, store_default: true)
  result = get(key)
  return result if key?(key) # If no key is found here, we didn't have a setting in the database

  try_dup(default_value(key)).tap do |value|
    set(key, value) if store_default
  end
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/setting_accessors/setting_set.rb', line 35

def key?(key)
  @temp_settings.key?(key.to_sym)
end

#set(key, val) ⇒ Object Also known as: []=

Writes a setting’s value



42
43
44
45
46
# File 'lib/setting_accessors/setting_set.rb', line 42

def set(key, val)
  track_old_value(key)
  set_value_before_type_cast(key, val)
  @temp_settings[key.to_sym] = SettingAccessors::Internal.converter(value_type(key)).new(val).convert
end

#value_before_type_cast(key) ⇒ Object

Returns The raw value passed to this setting before it was casted to the correct type.

Returns:

  • (Object)

    The raw value passed to this setting before it was casted to the correct type



98
99
100
101
102
# File 'lib/setting_accessors/setting_set.rb', line 98

def value_before_type_cast(key)
  lookup_nested_hash(@values_before_type_casts, key.to_s)
rescue NestedHashKeyNotFoundError
  get(key)
end

#value_changed?(key) ⇒ Boolean

Returns true if the value for the given setting differs from the database or default value.

Returns:

  • (Boolean)

    true if the value for the given setting differs from the database or default value



91
92
93
# File 'lib/setting_accessors/setting_set.rb', line 91

def value_changed?(key)
  get(key) != value_was(key)
end

#value_type(key) ⇒ String

Returns the setting’s value type in the record‘s context.

Returns:

  • (String)

    the setting’s value type in the record‘s context



71
72
73
# File 'lib/setting_accessors/setting_set.rb', line 71

def value_type(key)
  SettingAccessors::Internal.setting_value_type(key, record)
end

#value_was(key) ⇒ Object

Returns the value the given setting had after it was last persisted.

Returns:

  • (Object)

    the value the given setting had after it was last persisted



82
83
84
85
86
# File 'lib/setting_accessors/setting_set.rb', line 82

def value_was(key)
  lookup_nested_hash(@old_values, key.to_s)
rescue NestedHashKeyNotFoundError
  current_database_value(key)
end