Module: SettingAccessors::Internal

Defined in:
lib/setting_accessors/internal.rb

Class Method Summary collapse

Class Method Details

.add_setting_accessor_name(klass, setting_name) ⇒ Object

Adds the given setting name to the list of used setting accessors in the given class. This is mainly to keep track of all accessors defined in the different classes



112
113
114
115
116
# File 'lib/setting_accessors/internal.rb', line 112

def self.add_setting_accessor_name(klass, setting_name)
  @@setting_accessor_names ||= {}
  @@setting_accessor_names[klass.to_s] ||= []
  @@setting_accessor_names[klass.to_s] << setting_name.to_s
end

.converter(value_type) ⇒ SettingAccessors::Converter

Returns A value converter for the given type.

Returns:



95
96
97
98
# File 'lib/setting_accessors/internal.rb', line 95

def self.converter(value_type)
  @@converters ||= {}
  @@converters[value_type.to_sym] ||= SettingAccessors::Converter.new(value_type)
end

.ensure_nested_hash!(hash, *keys) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/setting_accessors/internal.rb', line 7

def self.ensure_nested_hash!(hash, *keys)
  h = hash
  keys.each do |key|
    h[key] ||= {}
    h = h[key]
  end
end

.get_class_setting(klass, setting_name) ⇒ Hash, NilClass

Returns Information about a class specific setting or nil if it wasn’t set before.

Returns:

  • (Hash, NilClass)

    Information about a class specific setting or nil if it wasn’t set before



103
104
105
# File 'lib/setting_accessors/internal.rb', line 103

def self.get_class_setting(klass, setting_name)
  self.lookup_nested_hash(@@class_settings, klass.to_s, setting_name.to_s)
end

.global_configObject

Loads information about all settings from YAML file These are cached in the class so they don’t have to be reloaded every time.

Note: For development / test, this is flushed every time



33
34
35
36
37
38
39
# File 'lib/setting_accessors/internal.rb', line 33

def self.global_config
  if Rails.env.test? || Rails.env.development?
    (YAML.load(File.open(Rails.root.join('config/settings.yml'))) || {}).deep_stringify_keys
  else
    @@config ||= (YAML.load(File.open(Rails.root.join('config/settings.yml'))) || {}).deep_stringify_keys
  end
end

.globally_defined_setting?(setting_name) ⇒ TrueClass, FalseClass

Returns true if the setting is defined in config/settings.yml.

Returns:

  • (TrueClass, FalseClass)

    true if the setting is defined in config/settings.yml



44
45
46
# File 'lib/setting_accessors/internal.rb', line 44

def self.globally_defined_setting?(setting_name)
  self.global_config[setting_name.to_s].present?
end

.lookup_nested_hash(hash, *keys) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/setting_accessors/internal.rb', line 15

def self.lookup_nested_hash(hash, *keys)
  return nil if hash.nil?

  h = hash
  keys.each do |key|
    return nil if h[key].nil?
    h = h[key]
  end
  h
end

.set_class_setting(klass, setting_name, options = {}) ⇒ Object

Sets a class-specific setting For global settings, this is done in config/settings.yml Please do not call this method yourself, it is done automatically by using setting_accessor in your model class



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/setting_accessors/internal.rb', line 54

def self.set_class_setting(klass, setting_name, options = {})
  @@class_settings ||= {}

  #If there are no options given, the setting *has* to be defined globally.
  if options.empty? && !self.globally_defined_setting?(setting_name)
    raise ArgumentError.new "The setting '#{setting_name}' in model '#{klass.to_s}' is neither globally defined nor did it receive options"

  #A setting which is already globally defined, may not be re-defined on class base
  elsif self.globally_defined_setting?(setting_name) && options.any?
    raise ArgumentError.new("The setting #{setting_name} is already defined in config/settings.yml and may not be redefined in #{klass}")

  #If the setting is defined on class base, we have to store its options
  elsif options.any? && !self.globally_defined_setting?(setting_name)
    self.ensure_nested_hash!(@@class_settings, klass.to_s)
    @@class_settings[klass.to_s][setting_name.to_s] = options.deep_stringify_keys
  end
end

.setting_accessor_names(klass) ⇒ Array<String>

Returns all setting accessor names defined in the given class.

Returns:

  • (Array<String>)

    all setting accessor names defined in the given class



121
122
123
124
# File 'lib/setting_accessors/internal.rb', line 121

def self.setting_accessor_names(klass)
  @@setting_accessor_names ||= {}
  self.lookup_nested_hash(@@setting_accessor_names, klass.to_s) || []
end

.setting_data(setting_name, assignable = nil) ⇒ Hash

Returns configuration data regarding this setting

  • If it’s a globally defined setting, the value is taken from config/settings.yml

  • If it’s a setting defined in a setting_accessor call, the information is taken from this call

  • Otherwise, an empty hash is returned.

Returns:

  • (Hash)

    configuration data regarding this setting

    • If it’s a globally defined setting, the value is taken from config/settings.yml

    • If it’s a setting defined in a setting_accessor call, the information is taken from this call

    • Otherwise, an empty hash is returned



79
80
81
82
83
# File 'lib/setting_accessors/internal.rb', line 79

def self.setting_data(setting_name, assignable = nil)
  (assignable && self.get_class_setting(assignable.class, setting_name)) ||
      self.global_config[setting_name.to_s] ||
      {}
end

.setting_value_type(*args) ⇒ String

Returns the given setting’s value type.

Returns:

  • (String)

    the given setting’s value type



88
89
90
# File 'lib/setting_accessors/internal.rb', line 88

def self.setting_value_type(*args)
  self.setting_data(*args)['type'] || 'polymorphic'
end