Class: SharedSettings::Setting

Inherits:
Object
  • Object
show all
Defined in:
lib/shared_settings/setting.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, serialized_value, encrypted) ⇒ Setting

Returns a new instance of Setting.



23
24
25
26
27
28
29
30
31
32
33
34
# 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

Instance Attribute Details

#encryptedObject (readonly)

Returns the value of attribute encrypted.



3
4
5
# File 'lib/shared_settings/setting.rb', line 3

def encrypted
  @encrypted
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/shared_settings/setting.rb', line 3

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/shared_settings/setting.rb', line 3

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



3
4
5
# File 'lib/shared_settings/setting.rb', line 3

def value
  @value
end

Class Method Details

.deserialize_value(value, type) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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

Instance Method Details

#to_hObject



36
37
38
39
40
41
42
43
# File 'lib/shared_settings/setting.rb', line 36

def to_h
  {
    name: name,
    type: type,
    value: value,
    encrypted: encrypted
  }
end