Class: SettingAccessors::Converter
- Inherits:
-
Object
- Object
- SettingAccessors::Converter
- Defined in:
- lib/setting_accessors/converter.rb
Overview
This class hopefully will hopefully one day mimic ActiveRecord’s attribute assigning methods, meaning that a conversion to the column type is done as soon as a new value is assigned by the programmer.
If the value cannot be parsed in the required type, nil is assigned. Please make sure that you specify the correct validations in settings.yml or assigned model to avoid this.
Currently supported types:
- Fixnum
- String
- Boolean
If the type is ‘polymorphic’, it is not converted at all.
Instance Method Summary collapse
-
#convert(new_value) ⇒ Object
Converts the setting’s value to the correct type.
-
#initialize(value_type) ⇒ Converter
constructor
A new instance of Converter.
Constructor Details
#initialize(value_type) ⇒ Converter
Returns a new instance of Converter.
19 20 21 |
# File 'lib/setting_accessors/converter.rb', line 19 def initialize(value_type) @value_type = value_type end |
Instance Method Details
#convert(new_value) ⇒ Object
Converts the setting’s value to the correct type
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/setting_accessors/converter.rb', line 26 def convert(new_value) #If the value is set to be polymorphic, we don't have to convert anything. return new_value if @value_type == 'polymorphic' #ActiveRecord only converts non-nil values to their database type #during assignment return new_value if new_value.nil? parse_method = :"parse_#{@value_type}" if private_methods.include?(parse_method) send(parse_method, new_value) else Rails.logger.warn("Invalid Setting type: #{@value_type}") new_value end end |