On Mar 28, 2007, at 3:01 PM, Xavier Noria wrote:
> All floats that come from the front-end should be parsed robustly in
> my app, where "robustly" is by the definition the method I paste
> below :-).
>
> The non-DRY way to do this is to add explicit code in each relevant
> point in the actions where any of those possible values are expected.
> I thought I could monkey patch AR::Base in environment.rb to be able
> to declare in my models somthing like
>
> parse_as_float :foo, :bar
>
> which would generate accessors that would call parse_float before
> delegating to write_attribute.
For the archives, I finally wrote wrappers for the relevant
ActiveRecord::ConnectionAdapters::Column methods. The application
works automatically the way we need without touching a single line of
code.
class ActiveRecord::ConnectionAdapters::Column
class << self
alias :original_value_to_decimal :value_to_decimal
def value_to_decimal(v)
if v.is_a?(String)
# We try first our parsing because the original method always
# returns a BigDecimal and there''s no way AFAIK to know
whether
# the constructor ignored part of the string. For example
"1,3"
# gives 1, whereas we want 1.3.
MyAppUtils.parse_decimal(v)
else
original_value_to_decimal(v)
end
end
# This method is called both when dates are set in the model, and
# when dates are loaded from the database. So we let the original
# parser do its job, and give a chance to ours if it fails.
alias :original_string_to_date :string_to_date
def string_to_date(v)
original_string_to_date(v) || MyAppUtils.parse_date(v)
end
end
end
-- fxn
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---