Hi --
On Sat, 17 Oct 2009, Bill Devaul wrote:
>
> Thanks for the help. I used the latter to come up with:
>
> @fields_to_normalize = %w{ address city country_id email_general fax
> name phone phone_local state_id vacation_end_on vacation_notice
> vacation_start_on zip_code }
>
> @fields_to_normalize.each do |attr|
> define_method("#{attr}=") do |value|
> @blanks ||= [0, "0", "", " "]
> write_attribute(attr, @blanks.include?(value) ? nil : value)
> end
> end
>
> Is it possible to do something more Rails-ish so it reads:
>
> normalize_fields :address, :city, :country_id
Sure -- just write the method :-) I would consider putting the blanks
in a constant.
class Whatever
BLANKS = [0, "0", "", " "]
def self.normalize_fields(*syms)
syms.each do |attr|
define_method("#{attr}=") do |value|
write_attribute(attr, BLANKS.include?(value) ? nil : value)
end
end
end
normalize_fields :address, :city, :country_id
end
David
--
The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com
David A. Black/Ruby Power and Light, LLC (http://www.rubypal.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
-~----------~----~----~----~------~----~------~--~---