class CouchPotato::Extensions::EncryptedProperty

Public Class Methods

new(owner_clazz, name, options = {}) click to toggle source
# File lib/couch_potato/extensions/encrypted_property.rb, line 6
def initialize(owner_clazz, name, options = {})
  super
  @options = options
end

Public Instance Methods

build(object, json) click to toggle source
# File lib/couch_potato/extensions/encrypted_property.rb, line 11
def build(object, json)
  value = json[name.to_s].nil? ? json[name.to_sym] : json[name.to_s]
  object.send "#{name}=", decrypt(value)
  object.instance_variable_set("@encrypted_#{name}", value)
end
decrypt(value) click to toggle source
# File lib/couch_potato/extensions/encrypted_property.rb, line 39
def decrypt(value)
  if not value.nil?
    EzCrypto::Key.decrypt_with_password(@options[:password], @options[:salt], Base64.decode64(value))
  end
end
dirty?(object) click to toggle source
# File lib/couch_potato/extensions/encrypted_property.rb, line 17
def dirty?(object)
  object.send("#{name}_changed?")
end
encrypt(value) click to toggle source
# File lib/couch_potato/extensions/encrypted_property.rb, line 33
def encrypt(value)
  if not value.nil?
    value.empty? ? value : Base64.encode64(EzCrypto::Key.encrypt_with_password(@options[:password], @options[:salt], value))
  end
end
encrypted_value(object) click to toggle source
# File lib/couch_potato/extensions/encrypted_property.rb, line 45
def encrypted_value(object)
  object.instance_variable_get("@encrypted_#{name}")
end
serialize(json, object) click to toggle source
# File lib/couch_potato/extensions/encrypted_property.rb, line 21
def serialize(json, object)
  if dirty?(object) or encrypted_value(object).nil?
    json[name] = encrypt(object.send(name))
  else
    json[name] = encrypted_value(object)
  end
end
value(result, object) click to toggle source
# File lib/couch_potato/extensions/encrypted_property.rb, line 29
def value(result, object)
  result[name] = object.send(name)
end