Module: Transformable::ClassMethods

Defined in:
lib/transformable.rb

Instance Method Summary collapse

Instance Method Details

#clean(attr, options = {}, &blk) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/transformable.rb', line 11

def clean(attr, options={}, &blk)

  skip_nil = options.fetch(:skip_nil) { true }

  define_method "#{attr}=" do |value|
	if value.nil? and skip_nil
	  new_value = value
	else
	  new_value = yield(value)
	end
	instance_variable_set("@#{attr}", new_value)
    write_attribute(attr, new_value) if defined? ActiveRecord and defined? ActiveRecord::Base and is_a? ActiveRecord::Base
  end
end