Class: Eventsimple::Types::EncryptedType
- Inherits:
-
Object
- Object
- Eventsimple::Types::EncryptedType
- Includes:
- Dry::Types::Builder, Dry::Types::Decorator, Dry::Types::Type
- Defined in:
- lib/eventsimple/types/encrypted_type.rb
Instance Method Summary collapse
- #decrypt(value) ⇒ Object
- #encrypt(value) ⇒ Object
-
#initialize(type, **options) ⇒ EncryptedType
constructor
A new instance of EncryptedType.
- #meta(data = nil) ⇒ Object
Constructor Details
#initialize(type, **options) ⇒ EncryptedType
Returns a new instance of EncryptedType.
12 13 14 15 16 |
# File 'lib/eventsimple/types/encrypted_type.rb', line 12 def initialize(type, **) super @type = type freeze end |
Instance Method Details
#decrypt(value) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/eventsimple/types/encrypted_type.rb', line 34 def decrypt(value) return value if value.nil? return value if value == '' || (value.is_a?(::String) && value.blank?) decrypted = begin ActiveRecord::Encryption::Encryptor.new.decrypt(value) rescue StandardError return value # Return original if decryption fails end deserialize_value(decrypted) end |
#encrypt(value) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/eventsimple/types/encrypted_type.rb', line 24 def encrypt(value) return value if value.nil? return value if value == '' || (value.is_a?(::String) && value.blank?) string_value = serialize_value(value) return string_value if ActiveRecord::Encryption::Encryptor.new.encrypted?(string_value) ActiveRecord::Encryption::Encryptor.new.encrypt(string_value) end |
#meta(data = nil) ⇒ Object
18 19 20 21 22 |
# File 'lib/eventsimple/types/encrypted_type.rb', line 18 def (data = nil) return { eventsimple: true } if data.nil? self.class.new(@type.(data)) end |