Thanks to your help David I now have:
# lib/normalizer.rb
module Normalizer
BLANKS = [0, "0", "", " "]
def self.included(base)
base.class_eval do
def self.normalize(*syms)
syms.each do |attr|
define_method("#{attr}=") do |value|
write_attribute(attr, BLANKS.include?(value) ? nil : value)
end
end
end
end
end
end
(I struggled with the above until I read that class_eval is needed to
make the inclusion happen. I wonder if a change to class_eval to yield
the included module would feel more natural.)
and
# models/model.rb
include Normalizer
normalize :name, :address, :city, :country_id
Looks great and tests pass!
Thanks,
Bill
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---