Module: AttrKeyring::ClassMethods

Defined in:
lib/attr_keyring.rb

Instance Method Summary collapse

Instance Method Details

#attr_encrypt(*attributes, encoder: nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/attr_keyring.rb', line 46

def attr_encrypt(*attributes, encoder: nil)
  self.encrypted_attributes ||= {}

  attributes.each do |attribute|
    encrypted_attributes[attribute.to_sym] = {encoder: encoder}

    define_attr_encrypt_writer(attribute)
    define_attr_encrypt_reader(attribute)
  end
end

#attr_keyring(keyring, options = {}) ⇒ Object



42
43
44
# File 'lib/attr_keyring.rb', line 42

def attr_keyring(keyring, options = {})
  self.keyring = Keyring.new(keyring, options)
end

#define_attr_encrypt_reader(attribute) ⇒ Object



63
64
65
66
67
# File 'lib/attr_keyring.rb', line 63

def define_attr_encrypt_reader(attribute)
  define_method(attribute) do
    attr_decrypt_column(attribute)
  end
end

#define_attr_encrypt_writer(attribute) ⇒ Object



57
58
59
60
61
# File 'lib/attr_keyring.rb', line 57

def define_attr_encrypt_writer(attribute)
  define_method(:"#{attribute}=") do |value|
    attr_encrypt_column(attribute, value)
  end
end

#inherited(subclass) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/attr_keyring.rb', line 34

def inherited(subclass)
  super

  subclass.encrypted_attributes = encrypted_attributes.dup
  subclass.keyring = keyring
  subclass.keyring_column_name = keyring_column_name
end