Module: EncryptedAttributes::AR23

Defined in:
lib/encrypted_attributes/ar_23.rb

Instance Method Summary collapse

Instance Method Details

#define_attribute_methods(*args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/encrypted_attributes/ar_23.rb', line 12

def define_attribute_methods(*args)
  self.encrypted_attributes.each do |column|
    define_read_method("read_encrypted_#{column}", column, columns_hash[column])
    evaluate_attribute_method "write_encrypted_#{column}=", "def write_encrypted_#{column}=(new_value);write_attribute('#{column}', new_value);end"

    define_method(column) do
      encrypted_value = self.send("read_encrypted_#{column}")

      if encrypted_value
        object_from_yaml(EncryptedAttributes.decrypt(encrypted_value)).freeze
      else
        encrypted_value
      end
    end

    define_method("#{column}=") do |new_value|
      if new_value
        if self.class.serialized_attributes.has_key?(column)
          new_value = new_value.to_yaml
        end
        new_value = EncryptedAttributes.encrypt(new_value)
      end

      self.send("write_encrypted_#{column}=", new_value)
    end
  end

  super
end

#encrypt(column_name) ⇒ Object



7
8
9
10
# File 'lib/encrypted_attributes/ar_23.rb', line 7

def encrypt(column_name)
  @encrypted_attributes = [] unless defined? @encrypted_columns
  @encrypted_attributes << column_name.to_s
end

#encrypted_attributesObject



3
4
5
# File 'lib/encrypted_attributes/ar_23.rb', line 3

def encrypted_attributes
  @encrypted_attributes
end