Module: SettingAccessors::Internal
- Defined in:
- lib/setting_accessors/internal.rb
Class Method Summary collapse
-
.add_setting_accessor_name(klass, setting_name) ⇒ Object
Adds the given setting name to the list of used setting accessors in the given class.
-
.converter(value_type) ⇒ SettingAccessors::Converter
A value converter for the given type.
- .ensure_nested_hash!(hash, *keys) ⇒ Object
-
.get_class_setting(klass, setting_name) ⇒ Hash, NilClass
Information about a class specific setting or
nilif it wasn’t set before. -
.global_config ⇒ Object
Loads information about all settings from YAML file These are cached in the class so they don’t have to be reloaded every time.
-
.globally_defined_setting?(setting_name) ⇒ TrueClass, FalseClass
trueif the setting is defined in config/settings.yml. - .lookup_nested_hash(hash, *keys) ⇒ Object
-
.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.
-
.setting_accessor_names(klass) ⇒ Array<String>
All setting accessor names defined in the given
class. -
.setting_data(setting_name, assignable_class = nil) ⇒ Hash
Configuration data regarding this setting.
-
.setting_value_type(*args) ⇒ String
The given setting’s value type.
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
116 117 118 119 120 |
# File 'lib/setting_accessors/internal.rb', line 116 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.
99 100 101 102 |
# File 'lib/setting_accessors/internal.rb', line 99 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.
107 108 109 |
# File 'lib/setting_accessors/internal.rb', line 107 def self.get_class_setting(klass, setting_name) self.lookup_nested_hash(@@class_settings, klass.to_s, setting_name.to_s) end |
.global_config ⇒ Object
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.
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, = {}) @@class_settings ||= {} #If there are no options given, the setting *has* to be defined globally. if .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) && .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 .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] = .deep_stringify_keys end end |
.setting_accessor_names(klass) ⇒ Array<String>
Returns all setting accessor names defined in the given class.
125 126 127 128 |
# File 'lib/setting_accessors/internal.rb', line 125 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_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.
79 80 81 82 83 84 85 86 87 |
# File 'lib/setting_accessors/internal.rb', line 79 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 && 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.
92 93 94 |
# File 'lib/setting_accessors/internal.rb', line 92 def self.setting_value_type(*args) self.setting_data(*args)['type'] || 'polymorphic' end |