跳至内容 跳至搜索

一个 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