RubyMoney/money — pull request #167 by alup (2012-05-03 12:06:43+00:00)
Title: Alternative String#to_money! function

There are cases in which an exception should be raised to notify higher level handlers.
For example, suppose we have set the following in a ```Product``` model:

```
composed_of :price,
:class_name => "Money",
:mapping => [%w(price_cents cents), %w(currency currency_as_string)],
:constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) },
:converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }
```

when we use an assignment like the following:

```
product.price = "no_numeric_character"
product.save
```

this would wrongly result ```price_cents``` set to ```0```

In this case the correct way would be to use ```to_money!``` instead of ```to_money``` in the converter:

```
composed_of :price,
:class_name => "Money",
:mapping => [%w(price_cents cents), %w(currency currency_as_string)],
:constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) },
:converter => Proc.new { |value|
if value.respond_to?(:to_money!)
value.to_money!
elsif value.respond_to?(:to_money)
value.to_money
else
raise(ArgumentError, "Can't convert #{value.class} to Money")
end
}
```

This way we can ensure that an ArgumentError will be raised when a non numeric string is given.