# Where would you put this parsing?

**URL:** <https://www.ruby-forum.com/t/where-would-you-put-this-parsing/90409>\
**Category:** Rails\
**Created:** [March 28, 2007, 3:02pm UTC](https://www.ruby-forum.com/t/where-would-you-put-this-parsing/90409 "2007-03-28T15:02:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [March 28, 2007, 3:02pm UTC](https://www.ruby-forum.com/t/where-would-you-put-this-parsing/90409/1 "2007-03-28T15:02:50Z")

</div>

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.

But that smeels like view-stuff in the model layer. That’s not a  
problem for me, unless there is a cleaner solution. What do you think?

– fxn

def self.parse\_float(n)  
return 0.0 if n.blank?  
n = n.dup  
n.strip!

```
 # take sign and delete it, if any
 s = n[0] == ?- ? -1 : 1
 n.sub!(/^[-+]/, '')

 # we assume a dot or comma followed by zero or up to two digits

```

at the  
# end of the string is the decimal part  
d = n.sub!(/.,$/, ‘’) ? $1 : “0”

```
 # in the rest of the number any non-digit is ignored
 n.gsub!(/\D/, '')

 # done
 return (s*("#{n}.#{d}".to_f) rescue 0.0)

```

end

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [March 28, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/where-would-you-put-this-parsing/90409/2 "2007-03-28T22:51:35Z")

</div>

On Mar 28, 2007, at 3:01 PM, Xavier N. wrote:

> 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
