module BikePOA module Helpers module ForceField FORCED_FIELDS = {} def self.included(base) base.send :extend, ClassMethods end def []=(property, value) transform = FORCED_FIELDS[property.to_sym] value = transform.call(value) if transform super(property, value) end module ClassMethods def force_integer(field) FORCED_FIELDS[field.to_sym] = lambda { |value| Integer(value) } end def force_utf8(field) FORCED_FIELDS[field.to_sym] = lambda do |value| new_value = value.encode('utf-8','iso-8859-1') (new_value.length == value.length)? new_value : value end end end end end end