Sha256: 54cab89a6c80521e808be1566e4a3691e168b485ee20b7cfbdb54383141de539
Contents?: true
Size: 1.94 KB
Versions: 2
Compression:
Stored size: 1.94 KB
Contents
require_relative "value_types/string" require_relative "value_types/symbol" require_relative "value_types/fixnum" require_relative "value_types/hash" require_relative "value_types/float" require_relative "value_types/array" require_relative "value_types/boolean" module RailsSettingsUi class UnknownDefaultValueType < StandardError;end class TypeConverter VALUE_TYPES_MAP = { String => RailsSettingsUi::ValueTypes::String, Symbol => RailsSettingsUi::ValueTypes::Symbol, Fixnum => RailsSettingsUi::ValueTypes::Fixnum, ActiveSupport::HashWithIndifferentAccess => RailsSettingsUi::ValueTypes::Hash, Float => RailsSettingsUi::ValueTypes::Float, Array => RailsSettingsUi::ValueTypes::Array, FalseClass => RailsSettingsUi::ValueTypes::Boolean, TrueClass => RailsSettingsUi::ValueTypes::Boolean } def self.cast(settings) errors = {} settings.each do |name, value| type = setting_value_type(name, value) settings[name] = type.cast if type.errors.any? errors[name.to_sym] = type.errors end end settings = set_non_presented_boolean_settings_to_false(settings) settings[:errors] = errors settings end def self.setting_value_type(name, value) default_setting_value_type = Settings.defaults[name.to_sym].class unless VALUE_TYPES_MAP.keys.include?(default_setting_value_type) raise RailsSettingsUi::UnknownDefaultValueType, "Unknown default value type #{default_setting_value_type}" end setting_value_type_class = VALUE_TYPES_MAP[default_setting_value_type] setting_value_type_class.new(value) end def self.set_non_presented_boolean_settings_to_false(settings) Settings.defaults.each do |name, value| if !settings[name.to_sym].present? && [TrueClass, FalseClass].include?(value.class) settings[name.to_sym] = false end end settings end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
rails-settings-ui-0.1.1 | lib/rails-settings-ui/type_converter.rb |
rails-settings-ui-0.1.0 | lib/rails-settings-ui/type_converter.rb |