This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
2023-03-30 05:44:00 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class UserSettings::Setting
|
|
|
|
attr_reader :name, :namespace, :in
|
|
|
|
|
|
|
|
def initialize(name, options = {})
|
|
|
|
@name = name.to_sym
|
|
|
|
@default_value = options[:default]
|
|
|
|
@namespace = options[:namespace]
|
|
|
|
@in = options[:in]
|
|
|
|
end
|
|
|
|
|
2023-08-14 09:52:45 -07:00
|
|
|
def inverse_of(name)
|
|
|
|
@inverse_of = name.to_sym
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def value_for(name, original_value)
|
|
|
|
value = begin
|
|
|
|
if original_value.nil?
|
|
|
|
default_value
|
|
|
|
else
|
|
|
|
original_value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if !@inverse_of.nil? && @inverse_of == name.to_sym
|
|
|
|
!value
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-30 05:44:00 -07:00
|
|
|
def default_value
|
|
|
|
if @default_value.respond_to?(:call)
|
|
|
|
@default_value.call
|
|
|
|
else
|
|
|
|
@default_value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def type
|
2023-03-30 22:33:17 -07:00
|
|
|
case default_value
|
|
|
|
when TrueClass, FalseClass
|
2023-03-30 05:44:00 -07:00
|
|
|
ActiveModel::Type::Boolean.new
|
|
|
|
else
|
|
|
|
ActiveModel::Type::String.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def type_cast(value)
|
|
|
|
if type.respond_to?(:cast)
|
|
|
|
type.cast(value)
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_a
|
|
|
|
[key, default_value]
|
|
|
|
end
|
|
|
|
|
|
|
|
def key
|
|
|
|
if namespace
|
|
|
|
"#{namespace}.#{name}".to_sym
|
|
|
|
else
|
|
|
|
name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|