Exposing serialised fields – meta programming way

class SomeClass < ActiveRecord::Base belongs_to :user def self.serialize(attr_name, class_name = Object, exposed_fields = []) super(attr_name, class_name) serialized_attr_accessor attr_name, exposed_fields end def self.serialized_attr_accessor(attr_name, *args) args.first.each do |method_name| eval ” def #{method_name} (self[:#{attr_name}] || {})[:#{method_name}] end def #{method_name}=(value) self[:#{attr_name}] ||= {} self[:#{attr_name}][:#{method_name}] = value end attr_accessible :#{method_name} ” end end serialize :other_data, Hash, %w(some other values you

Cleanup Big mongodb Collection

Recently i  have come across one small problem that i needed to fix. I had many records in a DB that i do not needed. I could not delete the entire collection, as i needed some of the records to be left alone. I have come up with this script, which allows me to delete records

Romanian Phone Number validator

Recently i had to implement a Romanian Phone Number validator… and i have managed to implement it as a method of the jQuery Validation Plugin. here is the whole method $.validator.addMethod(“phoneRO”, function(phone_number, element) { phone_number = phone_number.replace(/\(|\)|\s+|-/g, “”); return this.optional(element) || phone_number.length > 9 && phone_number.match(/^(?:(?:(?:00\s?|\+)40\s?|0)(?:7\d{2}\s?\d{3}\s?\d{3}|(21|31)\d{1}\s?\d{3}\s?\d{3}|((2|3)[3-7]\d{1})\s?\d{3}\s?\d{3}|(8|9)0\d{1}\s?\d{3}\s?\d{3}))$/); }, “Please specify a valid romanian phone number”); The

sphinx mysql command line

I’ve been searched for a method on how to work using sphinx from console, I needed to access the interface in order to see what’s indexed, how is stored and  also test my searches. After my google searches i’ve ended up with this. mysql -h –prompt ‘SphinxQL>’ –port 9312