Wednesday, December 7, 2016

Hash deep find and compact in rails

One of the applications I've worked recently has a lot of tree like structures represented internally as hashes. This is a very convenient way of handling such structures and rails provides good support to convert between object, hash and string (json) representations of these structures.

Two problems that are recurrent when working with these structures are finding a key in the tree and compacting (removing null or empty values) from the tree. Below there are 2 code fragments that do that.


def hash_deep_find(obj, key)
  if obj.respond_to?(:key?) && obj.key?(key)
    return obj[key]
  elsif obj.respond_to?(:keys)
    obj.keys.each do |child_key|
      return hash_deep_find(obj[child_key], key)
    end  endend


def hash_deep_compact(obj)
  if obj.is_a?(Hash)
    obj = obj.delete_if { |k, v| v.blank?}
    obj.keys.each do |key|
      hash_deep_compact(obj[key])
    end  endend


As to not change the application structure, these methods were implemented as methods of an Utils class already present in the application. Alternatively, one could simply extend the behavior of Hash (either in the Hash class itself or a class that extends Hash) adding these 2 methods.

Solving ORA-28001: the password has expired

It always happens when you are in a hurry and it makes you really upset. It's nothing to get desperate, though. To solve it, follow the steps below:

Connect as sysdba to the database:

> sudo su - oracle

> sqlplus sys as sysdba


Set password life time to unlimited in Oracle:

SQL> ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED;

Profile altered.


Reset the password for the locked user:

SQL> ALTER USER <user_name> IDENTIFIED BY <password>;

User altered.


Unlock the user account:

SQL> ALTER USER user_name ACCOUNT UNLOCK;

User altered.


Check if the user account is actually unlocked:

SQL>  SELECT USERNAME,ACCOUNT_STATUS FROM DBA_USERS WHERE USERNAME='<user_name>'


ACCOUNT_STATUS should be OPEN.