class ActiveadminSettingsCached::Coercions

Coerce user input values to defined types

@api private

Constants

BOOLEAN_MAP
FALSE_VALUES
TRUE_VALUES

Attributes

defaults[R]
display[R]

Public Class Methods

new(defaults, display) click to toggle source
# File lib/activeadmin_settings_cached/coercions.rb, line 16
def initialize(defaults, display)
  @defaults = defaults
  @display  = display
end

Public Instance Methods

cast_params(params) { |name, call| ... } click to toggle source
# File lib/activeadmin_settings_cached/coercions.rb, line 21
def cast_params(params)
  coerced_params = params.to_unsafe_h.map do |name, value|
    [name, cast_value(name, value)]
  end

  return coerced_params unless block_given?

  coerced_params.each do |name, value|
    yield(name, value.call) unless value.nil?
  end
end

Private Instance Methods

cast_value(name, value) click to toggle source
# File lib/activeadmin_settings_cached/coercions.rb, line 35
def cast_value(name, value) # rubocop:disable Metrics/MethodLength
  case defaults[name]
  when TrueClass, FalseClass
    -> { BOOLEAN_MAP.fetch(value, false) }
  when Integer
    -> { value_or_default(0) { Integer(value) } }
  when Float
    -> { value_or_default(0.0) { Float(value) } }
  when Hash, 'ActiveSupport::HashWithIndifferentAccess'
    nil
  when Symbol
    -> { value.to_sym }
  else
    -> { value }
  end
end
value_or_default(default) { || ... } click to toggle source
# File lib/activeadmin_settings_cached/coercions.rb, line 52
def value_or_default(default)
  yield
rescue ArgumentError, TypeError
  default
end