class SharedSettings::Setting

Attributes

encrypted[R]
name[R]
type[R]
value[R]

Public Class Methods

deserialize_value(value, type) click to toggle source
# File lib/shared_settings/setting.rb, line 5
def self.deserialize_value(value, type)
  case type.to_sym
  when :string
    value
  when :number
    value.include?('.') ? value.to_f : value.to_i
  when :boolean
    value == '1'
  when :range
    # Ranges will _always_ become two-dot ranges
    lower, upper = value.split(',').map(&:to_i)

    lower..upper
  else
    raise ArgumentError, "Unable to deserialize `#{type}` type"
  end
end
new(name, type, serialized_value, encrypted) click to toggle source
# File lib/shared_settings/setting.rb, line 23
def initialize(name, type, serialized_value, encrypted)
  @name = name.to_sym
  @type = type.to_sym
  @encrypted = encrypted

  if encrypted
    decrypted_value = decrypt_value(serialized_value)
    @value = self.class.deserialize_value(decrypted_value, type)
  else
    @value = self.class.deserialize_value(serialized_value, type)
  end
end

Public Instance Methods

to_h() click to toggle source
# File lib/shared_settings/setting.rb, line 36
def to_h
  {
    name: name,
    type: type,
    value: value,
    encrypted: encrypted
  }
end

Private Instance Methods

decrypt_value(string_value) click to toggle source
# File lib/shared_settings/setting.rb, line 47
def decrypt_value(string_value)
  encrypter = SharedSettings::Utilities::Encryption.new(encryption_key)
  iv, cipher_text = string_value.split('|')

  encrypter.decrypt(iv, cipher_text)
end
encryption_key() click to toggle source
# File lib/shared_settings/setting.rb, line 54
def encryption_key
  SharedSettings.configuration.encryption_key
end