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, key_provider: nil, **options) ⇒ EncryptedType
constructor
A new instance of EncryptedType.
- #meta(data = nil) ⇒ Object
Constructor Details
#initialize(type, key_provider: nil, **options) ⇒ EncryptedType
Returns a new instance of EncryptedType.
12 13 14 15 16 17 |
# File 'lib/eventsimple/types/encrypted_type.rb', line 12 def initialize(type, key_provider: nil, **) super @type = type @key_provider = key_provider freeze end |
Instance Method Details
#decrypt(value) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/eventsimple/types/encrypted_type.rb', line 39 def decrypt(value) return value if value.nil? return value if value == '' || (value.is_a?(::String) && value.blank?) decrypted = begin if @key_provider encryptor.decrypt(value, key_provider: @key_provider) else encryptor.decrypt(value) end rescue StandardError return value # Return original if decryption fails end deserialize_value(decrypted) end |
#encrypt(value) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/eventsimple/types/encrypted_type.rb', line 25 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 encryptor.encrypted?(string_value) if @key_provider encryptor.encrypt(string_value, key_provider: @key_provider) else encryptor.encrypt(string_value) end end |
#meta(data = nil) ⇒ Object
19 20 21 22 23 |
# File 'lib/eventsimple/types/encrypted_type.rb', line 19 def (data = nil) return { eventsimple: true } if data.nil? self.class.new(@type.(data), key_provider: @key_provider) end |