跳過內容 跳過搜尋

ActiveModel::Type::Value 用於加密/解密文字字串。

這是將加密系統與模型類別中 encrypts 宣告連接的核心部分。無論何時將屬性宣告為加密時,它會為該屬性設定 EncryptedAttributeType

方法
C
D
E
N
S
包含模組

屬性

[R] cast_type
[R] scheme

類別公有方法

new(scheme:, cast_type: ActiveModel::Type::String.new, previous_type: false, default: nil)

選項

  • :scheme - 具有此屬性加密屬性的 Scheme

  • :cast_type - 序列化(加密前)和反序列化(解密後)時會用到的類型。預設為 ActiveModel::Type::String

# File activerecord/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

執行個體公有方法

cast(value)

# File activerecord/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)

# File activerecord/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)

# File activerecord/lib/active_record/encryption/encrypted_attribute_type.rb, line 35
def deserialize(value)
  cast_type.deserialize decrypt(value)
end

encrypted?(value)

# File activerecord/lib/active_record/encryption/encrypted_attribute_type.rb, line 47
def encrypted?(value)
  with_context { encryptor.encrypted? value }
end

serialize(value)

# File activerecord/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?()

# File activerecord/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