Class: ActiveRecord::Encryption::EncryptedAttributeType
- Inherits:
-
ActiveModel::Type::Value
- Object
- ActiveModel::Type::Value
- ActiveRecord::Encryption::EncryptedAttributeType
- Includes:
- ActiveModel::Type::Helpers::Mutable
- Defined in:
- lib/active_record/encryption/encrypted_attribute_type.rb
Overview
An ActiveModel::Type::Value that encrypts/decrypts strings of text.
This is the central piece that connects the encryption system with encrypts
declarations in the model classes. Whenever you declare an attribute as encrypted, it configures an EncryptedAttributeType
for that attribute.
Instance Attribute Summary collapse
-
#cast_type ⇒ Object
readonly
Returns the value of attribute cast_type.
-
#scheme ⇒ Object
readonly
Returns the value of attribute scheme.
Instance Method Summary collapse
- #cast(value) ⇒ Object
- #changed_in_place?(raw_old_value, new_value) ⇒ Boolean
- #deserialize(value) ⇒ Object
- #encrypted?(value) ⇒ Boolean
-
#initialize(scheme:, cast_type: ActiveModel::Type::String.new, previous_type: false, default: nil) ⇒ EncryptedAttributeType
constructor
Options.
-
#previous_types ⇒ Object
:nodoc:.
- #serialize(value) ⇒ Object
- #support_unencrypted_data? ⇒ Boolean
Constructor Details
#initialize(scheme:, cast_type: ActiveModel::Type::String.new, previous_type: false, default: nil) ⇒ EncryptedAttributeType
Options
-
:scheme
- AScheme
with the encryption properties for this attribute. -
:cast_type
- A type that will be used to serialize (before encrypting) and deserialize (after decrypting). ActiveModel::Type::String by default.
23 24 25 26 27 28 29 |
# File 'lib/active_record/encryption/encrypted_attribute_type.rb', line 23 def initialize(scheme:, cast_type: ActiveModel::Type::String.new, previous_type: false, default: nil) super() @scheme = scheme @cast_type = cast_type @previous_type = previous_type @default = default end |
Instance Attribute Details
#cast_type ⇒ Object (readonly)
Returns the value of attribute cast_type.
13 14 15 |
# File 'lib/active_record/encryption/encrypted_attribute_type.rb', line 13 def cast_type @cast_type end |
#scheme ⇒ Object (readonly)
Returns the value of attribute scheme.
13 14 15 |
# File 'lib/active_record/encryption/encrypted_attribute_type.rb', line 13 def scheme @scheme end |
Instance Method Details
#cast(value) ⇒ Object
31 32 33 |
# File 'lib/active_record/encryption/encrypted_attribute_type.rb', line 31 def cast(value) cast_type.cast(value) end |
#changed_in_place?(raw_old_value, new_value) ⇒ Boolean
51 52 53 54 |
# File 'lib/active_record/encryption/encrypted_attribute_type.rb', line 51 def changed_in_place?(raw_old_value, new_value) old_value = raw_old_value.nil? ? nil : deserialize(raw_old_value) old_value != new_value end |
#deserialize(value) ⇒ Object
35 36 37 |
# File 'lib/active_record/encryption/encrypted_attribute_type.rb', line 35 def deserialize(value) cast_type.deserialize decrypt(value) end |
#encrypted?(value) ⇒ Boolean
47 48 49 |
# File 'lib/active_record/encryption/encrypted_attribute_type.rb', line 47 def encrypted?(value) with_context { encryptor.encrypted? value } end |
#previous_types ⇒ Object
:nodoc:
56 57 58 59 |
# File 'lib/active_record/encryption/encrypted_attribute_type.rb', line 56 def previous_types # :nodoc: @previous_types ||= {} # Memoizing on support_unencrypted_data so that we can tweak it during tests @previous_types[support_unencrypted_data?] ||= build_previous_types_for(previous_schemes_including_clean_text) end |
#serialize(value) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/active_record/encryption/encrypted_attribute_type.rb', line 39 def serialize(value) if serialize_with_oldest? serialize_with_oldest(value) else serialize_with_current(value) end end |
#support_unencrypted_data? ⇒ Boolean
61 62 63 |
# File 'lib/active_record/encryption/encrypted_attribute_type.rb', line 61 def support_unencrypted_data? ActiveRecord::Encryption.config.support_unencrypted_data && scheme.support_unencrypted_data? && !previous_type? end |