Module: SettingAccessors::Internal

Extended by:
Helpers
Defined in:
lib/setting_accessors/internal.rb

Class Method Summary collapse

Methods included from Helpers

ensure_nested_hash!, lookup_nested_hash

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



74
75
76
77
78
# File 'lib/setting_accessors/internal.rb', line 74

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

.class_settingsObject



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

def self.class_settings
  @@class_settings ||= {}
end

.converter(value_type) ⇒ SettingAccessors::Converter

Returns A value converter for the given type.

Returns:

  • (SettingAccessors::Converter)

    A value converter for the given type



56
57
58
# File 'lib/setting_accessors/internal.rb', line 56

def self.converter(value_type)
  Converters.const_get("#{value_type.to_s.camelize}Converter")
end

.get_class_setting(klass, setting_name) ⇒ Hash?

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

Returns:

  • (Hash, nil)

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



63
64
65
66
67
# File 'lib/setting_accessors/internal.rb', line 63

def self.get_class_setting(klass, setting_name)
  lookup_nested_hash(class_settings, klass.to_s, setting_name.to_s)
rescue ::SettingAccessors::Helpers::NestedHashKeyNotFoundException
  nil
end

.json_setting_names(klass, **options) ⇒ Object

Mainly a helper for #as_json calls. Evaluates the given options and determines the setting names to be returned.



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/setting_accessors/internal.rb', line 92

def self.json_setting_names(klass, **options)
  setting_names = setting_accessor_names(klass)

  if options[:only]
    setting_names & Array(options[:only]).map(&:to_s)
  elsif options[:except]
    setting_names - Array(options[:except]).map(&:to_s)
  else
    setting_names
  end
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



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/setting_accessors/internal.rb', line 20

def self.set_class_setting(klass, setting_name, options = {})
  # If there are no options given, the setting *has* to be defined globally.
  if options.empty?
    raise ArgumentError, "The setting '#{setting_name}' in model '#{klass}' is lacking options."
  # If the setting is defined on class base, we have to store its options
  else
    ensure_nested_hash!(class_settings, klass.to_s)
    class_settings[klass.to_s][setting_name.to_s] = options.deep_stringify_keys
    add_setting_accessor_name(klass, setting_name)
  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



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

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

.setting_data(setting_name, assignable_class = 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



39
40
41
42
43
44
# File 'lib/setting_accessors/internal.rb', line 39

def self.setting_data(setting_name, assignable_class = nil)
  # As a convenience function (and to keep the existing code working),
  # it is possible to provide a class or an instance of said class
  assignable_class &&= assignable_class.class unless assignable_class.is_a?(Class)
  (assignable_class && get_class_setting(assignable_class, setting_name)) || {}
end

.setting_value_type(*args) ⇒ String

Returns the given setting’s value type.

Returns:

  • (String)

    the given setting’s value type



49
50
51
# File 'lib/setting_accessors/internal.rb', line 49

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