config/initializers/mongoid_extension.rb
# Set fields that should be encrypted
Set fields that should be encrypted
Set fields that should be encrypted
def define_setter_and_getter(field_name)
        if self.fields[field_name.to_s].nil?
        elsif self.fields[field_name.to_s].options[:type] == String
          define_method("#{field_name}=") do |value|
            if not value.nil? and value[0..(PRIFIX.size - 1)] == PRIFIX
              self[field_name] = value
            elsif not value.nil? and value[0..(PRIFIX.size - 1)] != PRIFIX
              self[field_name] = PRIFIX + Crypt.encrypt(value)
            end
          end
          define_method("#{field_name}") do  
            value = self[field_name]
            if not value.nil? and value[0..(PRIFIX.size - 1)] == PRIFIX
              return Crypt.decrypt(value[PRIFIX.size..-1])
            else
              return self[field_name]
            end
          end
          define_method("#{field_name}_encrypted") do
              return self[field_name]
          end
        else
          puts "field: #{field_name} is not String!"
        end
      end
def define_setter_and_getter ( field_name ) if self . fields [ field_name . to_s ] . nil? elsif self . fields [ field_name . to_s ] . options [ :type ] == String define_method ( "#{field_name}=" ) do | value | if not value . nil? and value [ 0 .. ( PRIFIX . size - 1 ) ] == PRIFIX self [ field_name ] = value elsif not value . nil? and value [ 0 .. ( PRIFIX . size - 1 ) ] != PRIFIX self [ field_name ] = PRIFIX + Crypt . encrypt ( value ) end end define_method ( "#{field_name}" ) do value = self [ field_name ] if not value . nil? and value [ 0 .. ( PRIFIX . size - 1 ) ] == PRIFIX return Crypt . decrypt ( value [ PRIFIX . size .. - 1 ] ) else return self [ field_name ] end end define_method ( "#{field_name}_encrypted" ) do return self [ field_name ] end else puts "field: #{field_name} is not String!" end end
Set fields that should be encrypted
Set fields that should be encrypted
# if field does not exists # puts "field: #{field_name} does not exists!" # puts "redefine setter and getter for field #{field_name}"
field_name
<not_specific>