Class: SharedSettings::Setting
- Inherits:
-
Object
- Object
- SharedSettings::Setting
- Defined in:
- lib/shared_settings/setting.rb
Instance Attribute Summary collapse
-
#encrypted ⇒ Object
readonly
Returns the value of attribute encrypted.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(name, type, serialized_value, encrypted) ⇒ Setting
constructor
A new instance of Setting.
- #to_h ⇒ Object
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
#encrypted ⇒ Object (readonly)
Returns the value of attribute encrypted.
3 4 5 |
# File 'lib/shared_settings/setting.rb', line 3 def encrypted @encrypted end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/shared_settings/setting.rb', line 3 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
3 4 5 |
# File 'lib/shared_settings/setting.rb', line 3 def type @type end |
#value ⇒ Object (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_h ⇒ Object
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 |