7H3LaughingMan wrote:
> I would like to know if it is possible to alter a variable during the
> process of a custom validation. I know that it is no longer validation
> when your altering data but in my opinion there are times when you
> need alter it for simplicity sakes.

That's not what validations are for.  You want custom setter methods.

> A good example would be for a
> world wide auction house type application and to to convert currency
> to one single format, when you validate you check to see if both the
> amount is alright along with the currency type. (This is assuming you
> can type in what type of currency it is, and for this example is
> possible.)

With real-world currencies, it is my understanding that this is a bad 
idea.

> 
> The application I am developing allows the user to type in multiple
> currencies at one (This is a D20 RPG, and people type in something
> along the line of 12 pp 145 gp 78 sp 21 cp) and so far the validation
> process involves converting it all to the lowest currency type.

Again, don't do this in the validator.  What you want is something like 
this:

CONVERSIONS = {:sp => 10, :gp => 100, :pp => 1000}

def balance=(string)
  # parse the string somehow to get the various amounts, then:

  bal = cp
  bal += gp * CONVERSIONS[:gp]
  # likewise for other currencies

  self[:balance] = bal
end

There's room for refactoring here, but you get the idea.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
 But
> for the sake of being DRY I would only need to add another line to set
> the variable, but instead it is looking like I would have to repeat
> the whole method and add in the single line and change it that way on
> creation and updates.

-- 
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to