Class: ActiveConfiguration::SettingProxy
- Inherits:
-
Object
- Object
- ActiveConfiguration::SettingProxy
- Defined in:
- lib/active_configuration/setting_proxy.rb
Overview
Handles the reading and writing of ActiveConfiguration::Setting objects and ensures configuration requirements are upheld.
Instance Attribute Summary collapse
-
#key ⇒ Object
Returns the value of attribute key.
-
#manager ⇒ Object
Returns the value of attribute manager.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(manager, key) ⇒ SettingProxy
constructor
Initializes a new ActiveConfiguration::SettingProxy with a related SettingManager and a key for this setting.
-
#inspect ⇒ Hash, Array
Returns the Hash or Array representation of the underlying stored settings depending on whether or not this is a multiple option.
-
#replace(value_with_modifier) ⇒ Hash, Array
Replaces the underlying Hash or Array with a replacement.
-
#save ⇒ Boolean
Saves this setting’s modifiers and values.
-
#validate ⇒ Object
Checks modifiers and values on this setting for validation errors and, if found, adds those errors to this proxy’s model’s collection of errors.
Constructor Details
#initialize(manager, key) ⇒ SettingProxy
Initializes a new ActiveConfiguration::SettingProxy with a related SettingManager and a key for this setting. This setting’s modifiers and values are cached locally as either a Hash or an Array for later access and manipulation.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/active_configuration/setting_proxy.rb', line 21 def initialize(manager, key) @manager, @key = manager, key if settings = @manager.configurable.active_configuration_settings.with_key(@key).to_a if option.allow_multiple? @value = settings.collect{|setting| {:value => coerce(setting.value), :modifier => setting.modifier}} else setting = settings.first @value = { :modifier => (setting ? setting.modifier : nil), :value => coerce(setting ? setting.value : option.default_value) } end end end |
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
9 10 11 |
# File 'lib/active_configuration/setting_proxy.rb', line 9 def key @key end |
#manager ⇒ Object
Returns the value of attribute manager.
8 9 10 |
# File 'lib/active_configuration/setting_proxy.rb', line 8 def manager @manager end |
#value ⇒ Object
Returns the value of attribute value.
10 11 12 |
# File 'lib/active_configuration/setting_proxy.rb', line 10 def value @value end |
Instance Method Details
#inspect ⇒ Hash, Array
Returns the Hash or Array representation of the underlying stored settings depending on whether or not this is a multiple option.
160 161 162 |
# File 'lib/active_configuration/setting_proxy.rb', line 160 def inspect return @value.inspect end |
#replace(value_with_modifier) ⇒ Hash, Array
Replaces the underlying Hash or Array with a replacement. This handles reverting to defaults when nil is given as the value.
Note: Athough Hashes given may contain keys other than :modifier and :value, all other keys will be stripped out and not saved.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/active_configuration/setting_proxy.rb', line 54 def replace(value_with_modifier) if option.allow_multiple? if value_with_modifier.is_a?(Hash) or value_with_modifier.is_a?(Array) or value_with_modifier.is_a?(NilClass) value_with_modifier = [value_with_modifier].flatten.collect{|value_with_modifier| {:modifier => nil, :value => nil}.merge(value_with_modifier.nil? ? {} : value_with_modifier.slice(*[:modifier, :value]))} value_with_modifier.delete({:modifier => nil, :value => nil}) else raise ArgumentError, "Array expected." end else if value_with_modifier.is_a?(Hash) or value_with_modifier.is_a?(NilClass) value_with_modifier = {:modifier => nil, :value => nil}.merge(value_with_modifier.nil? ? {:value => coerce(option.default_value)} : value_with_modifier.slice(*[:modifier, :value])) else raise ArgumentError, "Hash expected." end end return (@value = value_with_modifier) end |
#save ⇒ Boolean
Saves this setting’s modifiers and values.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/active_configuration/setting_proxy.rb', line 132 def save save_status = true original_setting_ids = @manager.configurable.active_configuration_settings.with_key(@key).collect(&:id) replaced_setting_ids = [] [value].flatten.each do |value_with_modifier| if (setting = @manager.configurable.active_configuration_settings.create(:key => @key, :modifier => value_with_modifier[:modifier], :value => value_with_modifier[:value])).new_record? save_status = false && break else replaced_setting_ids << setting.id end end @manager.configurable.active_configuration_settings.reload @manager.configurable.active_configuration_settings.with_key(@key).where(:id => (save_status ? original_setting_ids : replaced_setting_ids)).destroy_all @manager.settings.delete(@key) return save_status end |
#validate ⇒ Object
Checks modifiers and values on this setting for validation errors and, if found, adds those errors to this proxy’s model’s collection of errors.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/active_configuration/setting_proxy.rb', line 75 def validate errors = Array.new [value].flatten.each do |value_with_modifier| value = value_with_modifier[:value] modifier = value_with_modifier[:modifier] if !option.allowed_values.nil? and !option.allowed_values.include?(value) errors << "The value '#{value}' for the '#{option.key}' setting isn't present in the list of allowed values." end if !option.allowed_format.nil? case option.allowed_format when 'string' if !value.is_a?(String) errors << "The value '#{value}' for the '#{option.key}' setting is not a String." end when 'fixnum' if !value.is_a?(Fixnum) errors << "The value '#{value}' for the '#{option.key}' setting is not a Fixnum." end when 'float' if !value.is_a?(Float) and !value.is_a?(Fixnum) errors << "The value '#{value}' for the '#{option.key}' setting is not a Float." end when 'boolean' if !value.is_a?(TrueClass) and !value.is_a?(FalseClass) errors << "The value '#{value}' for the '#{option.key}' setting is not a Boolean." end when 'email' if !value[/^[A-Z0-9_\.%\+\-\']+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2,4}|museum|travel)$/i] errors << "The value '#{value}' for the '#{option.key}' setting is not an Email Address." end when 'url' if !value[URI.regexp] errors << "The value '#{value}' for the '#{option.key}' setting is not a URL." end end if option.allowed_format.is_a?(Regexp) and !value[option.allowed_format] errors << "The value '#{value}' for the '#{option.key}' setting is not in the correct format." end end if !modifier.nil? and !option.allowed_modifiers.nil? and !option.allowed_modifiers.include?(modifier) errors << "The modifier '#{modifier}' for the '#{option.key}' setting isn't present in the list of allowed modifiers." end end errors.each do |error| @manager.configurable.errors.add(:settings, error) end end |