Module: Sequel::Plugins::Kinko::ClassMethods

Defined in:
lib/sequel/plugins/kinko.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#kinko_encrypted_columnsObject (readonly)

Returns the value of attribute kinko_encrypted_columns.



15
16
17
# File 'lib/sequel/plugins/kinko.rb', line 15

def kinko_encrypted_columns
  @kinko_encrypted_columns
end

#kinko_index_normalizersObject (readonly)

Returns the value of attribute kinko_index_normalizers.



15
16
17
# File 'lib/sequel/plugins/kinko.rb', line 15

def kinko_index_normalizers
  @kinko_index_normalizers
end

#kinko_indexed_columnsObject (readonly)

Returns the value of attribute kinko_indexed_columns.



15
16
17
# File 'lib/sequel/plugins/kinko.rb', line 15

def kinko_indexed_columns
  @kinko_indexed_columns
end

Instance Method Details

#blind_index_for(column, value) ⇒ Object



50
51
52
53
54
55
# File 'lib/sequel/plugins/kinko.rb', line 50

def blind_index_for(column, value)
  normalize = @kinko_index_normalizers.fetch(column) do
    raise ArgumentError, "no blind index registered for #{column.inspect}"
  end
  ::Kinko::BlindIndex.compute(value, normalize:)
end

#kinko_blind_index(column, normalize: :none) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sequel/plugins/kinko.rb', line 37

def kinko_blind_index(column, normalize: :none)
  @kinko_indexed_columns << column
  @kinko_index_normalizers[column] = normalize

  define_method("#{column}=") do |value|
    if value.nil?
      self[column] = nil
    else
      self[column] = ::Kinko::BlindIndex.compute(value, normalize:)
    end
  end
end

#kinko_encrypt(column, type: nil, index: nil, normalize: :none) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sequel/plugins/kinko.rb', line 17

def kinko_encrypt(column, type: nil, index: nil, normalize: :none)
  @kinko_encrypted_columns << column
  @kinko_index_normalizers[index] = normalize if index

  define_method(column) do
    raw = self[column]
    raw.nil? ? nil : ::Kinko::Cipher.decrypt(raw, type: type)
  end

  define_method("#{column}=") do |value|
    if value.nil?
      self[column] = nil
      self[index] = nil if index
    else
      self[column] = ::Kinko::Cipher.encrypt(value)
      self[index] = ::Kinko::BlindIndex.compute(value, normalize:) if index
    end
  end
end