Class: ActiveConfiguration::SettingManager
- Inherits:
-
Object
- Object
- ActiveConfiguration::SettingManager
- Defined in:
- lib/active_configuration/setting_manager.rb
Overview
Returns a SettingProxy object for any option that has been configured.
Instance Attribute Summary collapse
-
#configurable ⇒ Object
Returns the value of attribute configurable.
-
#settings ⇒ Object
Returns the value of attribute settings.
Instance Method Summary collapse
-
#[](key) ⇒ Hash, ...
Provides access to setting details for a setting at the given key.
-
#[]=(key, value) ⇒ Hash, ...
Replaces the Hash or Array of Hashes for the setting with the given key with the given value.
-
#initialize(configurable) ⇒ SettingManager
constructor
Initializes this SettingManager and keeps track of what model this SettingManager is proxying settings for.
-
#reload ⇒ Object
Resets any pending setting modifications.
-
#save ⇒ Boolean
Saves all settings with pending modificaitons.
-
#update_settings(replacement_settings = {}) ⇒ Boolean
Writes over multiple settings and saves all setting updates at once.
-
#validate ⇒ Object
Runs validations against all settings with pending modificaitons.
-
#write_settings(replacement_settings = {}) ⇒ Object
Writes over multiple settings at once.
Constructor Details
#initialize(configurable) ⇒ SettingManager
Initializes this SettingManager and keeps track of what model this SettingManager is proxying settings for.
15 16 17 18 |
# File 'lib/active_configuration/setting_manager.rb', line 15 def initialize(configurable) @configurable = configurable @settings = Hash.new end |
Instance Attribute Details
#configurable ⇒ Object
Returns the value of attribute configurable.
7 8 9 |
# File 'lib/active_configuration/setting_manager.rb', line 7 def configurable @configurable end |
#settings ⇒ Object
Returns the value of attribute settings.
8 9 10 |
# File 'lib/active_configuration/setting_manager.rb', line 8 def settings @settings end |
Instance Method Details
#[](key) ⇒ Hash, ...
Provides access to setting details for a setting at the given key.
26 27 28 29 30 31 32 33 34 |
# File 'lib/active_configuration/setting_manager.rb', line 26 def [](key) if @configurable.class.configuration..has_key?(key) @settings[key] ||= SettingProxy.new(self, key) return @settings[key].value end return nil end |
#[]=(key, value) ⇒ Hash, ...
Replaces the Hash or Array of Hashes for the setting with the given key with the given value.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/active_configuration/setting_manager.rb', line 43 def []=(key, value) if @configurable.class.configuration..has_key?(key) @settings[key] ||= SettingProxy.new(self, key) @settings[key].replace(value) return @settings[key].value end return nil end |
#reload ⇒ Object
Resets any pending setting modifications.
90 91 92 |
# File 'lib/active_configuration/setting_manager.rb', line 90 def reload @settings = Hash.new end |
#save ⇒ Boolean
Saves all settings with pending modificaitons.
72 73 74 |
# File 'lib/active_configuration/setting_manager.rb', line 72 def save return !settings.values.collect{|setting| setting.save}.include?(false) end |
#update_settings(replacement_settings = {}) ⇒ Boolean
Writes over multiple settings and saves all setting updates at once.
81 82 83 84 85 86 87 |
# File 'lib/active_configuration/setting_manager.rb', line 81 def update_settings(replacement_settings = {}) write_settings(replacement_settings) validate return (@configurable.errors[:settings].empty? ? save : false) end |
#validate ⇒ Object
Runs validations against all settings with pending modificaitons. Any errors are added to @configurable.errors.
65 66 67 |
# File 'lib/active_configuration/setting_manager.rb', line 65 def validate settings.values.collect{|setting| setting.validate} end |
#write_settings(replacement_settings = {}) ⇒ Object
Writes over multiple settings at once.
57 58 59 60 61 |
# File 'lib/active_configuration/setting_manager.rb', line 57 def write_settings(replacement_settings = {}) replacement_settings.each_pair do |key, value| self[key] = value end end |