Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Custom Validations Altering Variable
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  19 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
7H3LaughingMan  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 16:05
From: 7H3LaughingMan <austin.brk...@gmail.com>
Date: Sat, 7 Nov 2009 08:05:50 -0800 (PST)
Local: Sat 7 Nov 2009 16:05
Subject: Custom Validations Altering Variable
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. 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.)

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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marnen Laibow-Koser  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 17:14
From: Marnen Laibow-Koser <rails-mailing-l...@andreas-s.net>
Date: Sat, 7 Nov 2009 18:14:08 +0100
Local: Sat 7 Nov 2009 17:14
Subject: Re: Custom Validations Altering Variable

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
mar...@marnen.org
 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/.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 18:14
From: Eric <ericgh...@gmail.com>
Date: Sat, 7 Nov 2009 10:14:23 -0800 (PST)
Local: Sat 7 Nov 2009 18:14
Subject: Re: Custom Validations Altering Variable
I would put this in a callback in the controller. I suggest starting
at before_save and go from there.

-eric

On Nov 7, 8:05 am, 7H3LaughingMan <austin.brk...@gmail.com> wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marnen Laibow-Koser  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 18:22
From: Marnen Laibow-Koser <rails-mailing-l...@andreas-s.net>
Date: Sat, 7 Nov 2009 19:22:22 +0100
Local: Sat 7 Nov 2009 18:22
Subject: Re: Custom Validations Altering Variable

Eric wrote:
> I would put this in a callback in the controller. I suggest starting
> at before_save and go from there.

That would work, but I think it's conceptually wrong for what the OP
wants.

> -eric

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org
--
Posted via http://www.ruby-forum.com/.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 19:23
From: Eric <ericgh...@gmail.com>
Date: Sat, 7 Nov 2009 11:23:09 -0800 (PST)
Local: Sat 7 Nov 2009 19:23
Subject: Re: Custom Validations Altering Variable
I don't agree.

Best,
Eric

On Nov 7, 10:22 am, Marnen Laibow-Koser <rails-mailing-l...@andreas-


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
7H3LaughingMan  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 20:57
From: 7H3LaughingMan <austin.brk...@gmail.com>
Date: Sat, 7 Nov 2009 12:57:16 -0800 (PST)
Local: Sat 7 Nov 2009 20:57
Subject: Re: Custom Validations Altering Variable
Alright I have set it all up and it will convert it to the lowest form
(and back for debugging purposes), however whenever I try to edit and
save using @armor.update_attributes(params[:armor]) it fails for some
reason. Here is a link to show you what I did with the model...

http://pastebin.com/me807521


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marnen Laibow-Koser  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 21:30
From: Marnen Laibow-Koser <rails-mailing-l...@andreas-s.net>
Date: Sat, 7 Nov 2009 22:30:30 +0100
Local: Sat 7 Nov 2009 21:30
Subject: Re: Custom Validations Altering Variable

7H3LaughingMan wrote:
> Alright I have set it all up and it will convert it to the lowest form
> (and back for debugging purposes), however whenever I try to edit and
> save using @armor.update_attributes(params[:armor]) it fails for some
> reason.

Fails in what way?

> Here is a link to show you what I did with the model...

> http://pastebin.com/me807521

I'll check it out in detail later on.  On cursory inspection, however,
it looks like it has a lot of problems; I'll make some suggestions when
I'm not trying to type on my iPhone. :)

Perhaps Eric's before_save suggestion is the right idea after all.
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org

--
Posted via http://www.ruby-forum.com/.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
7H3LaughingMan  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 01:05
From: 7H3LaughingMan <austin.brk...@gmail.com>
Date: Sat, 7 Nov 2009 17:05:31 -0800 (PST)
Local: Sun 8 Nov 2009 01:05
Subject: Re: Custom Validations Altering Variable
I don't know how it fails but for some reason it refuses to update
when I edit it, I have it setup to where it goes back to the index
when you successfully edit something and when it doesn't work it goes
back to the edit page. It doesn't even throw any error messages.

On Nov 7, 3:30 pm, Marnen Laibow-Koser <rails-mailing-l...@andreas-


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marnen Laibow-Koser  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 02:59
From: Marnen Laibow-Koser <rails-mailing-l...@andreas-s.net>
Date: Sun, 8 Nov 2009 03:59:29 +0100
Local: Sun 8 Nov 2009 02:59
Subject: Re: Custom Validations Altering Variable

7H3LaughingMan wrote:
> I don't know how it fails but for some reason it refuses to update
> when I edit it, I have it setup to where it goes back to the index
> when you successfully edit something and when it doesn't work it goes
> back to the edit page. It doesn't even throw any error messages.

I'll pay special attention to that, then.  Are your tests passing?

> On Nov 7, 3:30 pm, Marnen Laibow-Koser <rails-mailing-l...@andreas-

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org
--
Posted via http://www.ruby-forum.com/.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
7H3LaughingMan  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 03:34
From: 7H3LaughingMan <austin.brk...@gmail.com>
Date: Sat, 7 Nov 2009 19:34:55 -0800 (PST)
Local: Sun 8 Nov 2009 03:34
Subject: Re: Custom Validations Altering Variable
Haven't really created any "proper" tests since I actually only have
one controller at the moment and I can test it via browser. All of my
methods work however whenever I do an update it doesn't touch the set
method to change the variable. (Tested with simple outputing text to
say that it was being used)

On Nov 7, 8:59 pm, Marnen Laibow-Koser <rails-mailing-l...@andreas-


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marnen Laibow-Koser  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 04:28
From: Marnen Laibow-Koser <rails-mailing-l...@andreas-s.net>
Date: Sun, 8 Nov 2009 05:28:42 +0100
Local: Sun 8 Nov 2009 04:28
Subject: Re: Custom Validations Altering Variable

7H3LaughingMan wrote:
> Haven't really created any "proper" tests since I actually only have
> one controller at the moment and I can test it via browser

Not an excuse.  You should write tests before writing any application
code at all.  That way you know your code does what you meant it to.
Read up on test-first development and apply the practice religiously.

> All of my
> methods work however whenever I do an update it doesn't touch the set
> method to change the variable. (Tested with simple outputing text to
> say that it was being used)

Right.  I don't think update_attribute would.

> On Nov 7, 8:59 pm, Marnen Laibow-Koser <rails-mailing-l...@andreas-

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org
--
Posted via http://www.ruby-forum.com/.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
7H3LaughingMan  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 17:59
From: 7H3LaughingMan <austin.brk...@gmail.com>
Date: Sun, 8 Nov 2009 09:59:40 -0800 (PST)
Local: Sun 8 Nov 2009 17:59
Subject: Re: Custom Validations Altering Variable
Alright I wrote a test and figured out what was wrong turned out that
I had the form submitting the data to the edit section of the
controller instead of the update section. I fixed it and it all works
out, when you use update_attributes it does use the override of the
set method.

Now then my only question now is where can I put the universal methods
that are going to be used by Models and Views, and Controllers /
Helpers if they need to be used by them. I read somewhere where you
would put it but since then I have forgotten and can't find it again.

On Nov 7, 10:28 pm, Marnen Laibow-Koser <rails-mailing-l...@andreas-


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marnen Laibow-Koser  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 04:27
From: Marnen Laibow-Koser <rails-mailing-l...@andreas-s.net>
Date: Mon, 9 Nov 2009 05:27:33 +0100
Local: Mon 9 Nov 2009 04:27
Subject: Re: Custom Validations Altering Variable

7H3LaughingMan wrote:
> Alright I wrote a test and figured out what was wrong turned out that
> I had the form submitting the data to the edit section of the
> controller instead of the update section.

("Method", not "section".)

> I fixed it and it all works
> out, when you use update_attributes it does use the override of the
> set method.

OK.  I couldn't remember for sure offhand.

> Now then my only question now is where can I put the universal methods
> that are going to be used by Models and Views, and Controllers /
> Helpers if they need to be used by them. I read somewhere where you
> would put it but since then I have forgotten and can't find it again.

What universal methods?  You don't need any for the case at hand.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org

--
Posted via http://www.ruby-forum.com/.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marnen Laibow-Koser  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 05:00
From: Marnen Laibow-Koser <rails-mailing-l...@andreas-s.net>
Date: Mon, 9 Nov 2009 06:00:18 +0100
Local: Mon 9 Nov 2009 05:00
Subject: Re: Custom Validations Altering Variable

As promised, I've refactored and improved (I hope!) your code.  The new
version is at http://pastebin.com/f4b92e46d .

One suggestion I didn't get to make in the source (because the method no
longer exists with that name): convertDNDtoInt is not a Ruby-style name.
The more Rubyish name would be convert_dnd_to_int.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org
--
Posted via http://www.ruby-forum.com/.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matt Jones  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 14:40
From: Matt Jones <al2o...@gmail.com>
Date: Mon, 9 Nov 2009 06:40:36 -0800 (PST)
Local: Mon 9 Nov 2009 14:40
Subject: Re: Custom Validations Altering Variable

On Nov 8, 12:59 pm, 7H3LaughingMan <austin.brk...@gmail.com> wrote:

> Now then my only question now is where can I put the universal methods
> that are going to be used by Models and Views, and Controllers /
> Helpers if they need to be used by them. I read somewhere where you
> would put it but since then I have forgotten and can't find it again.

Marmen's code goes a long way towards making this more reusable. The
only thing that I'd recommend that you look at if you end up using
many 'cost' attributes is a meta-definition helper for the fields, so
you could say:

dnd_money_field :cost

in the class level and get the cost= method and so on.

--Matt Jones


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
7H3LaughingMan  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 17:44
From: 7H3LaughingMan <austin.brk...@gmail.com>
Date: Mon, 9 Nov 2009 09:44:14 -0800 (PST)
Local: Mon 9 Nov 2009 17:44
Subject: Re: Custom Validations Altering Variable
Thanks for the help, I was already in the process of converting it
into it's own separate class and having it always loading
automatically. But right now I am having some issues mostly with
carrying numbers across (mostly downwards). The idea is to have
methods where I can get and set the individual values (pp, gp, sp,
cp).

However if the value is set to a number below 0 it would...
1. Go up a level subtract one.
2. Add 10 to itself
3. Continue

I would like for it to always go downwards and thought I could use
some recursion to keep these methods DRY. I don't want conversions
being done unless it needs to be done. A person can't take 10 cp and
magically turn it into 1 sp but if he purchases something that is 1 sp
they can use 10 cp since it's the same value. But with the current way
I was planning it would of had issues with infinite recursion and
yadda yadda yadda. But here are some examples...

1. 10 gp - 1 sp = 9 gp 9 sp
2. 200 cp - 2 gp = 0 cp
3. 10 sp - 1 pp = 0 cp (Not letting it get below 0, since normally
this wouldn't happen unless under certain cirmstances)

http://pastebin.com/d420d085a

I was going to add some options to where if it was set to something
below 0 that it would automatically subtract 1 from the one above it,
add 10 to itself, and continue doing the math while repeating if
necessary with recursion. However that would be in issue if the one
above it was 0 since if it was subtracted by 1 it would set the one
below it to -1 causing it to repeat indefinitely.

I hope this kind of makes since, and I am kind of stumped on doing
this without an insane amount of repeated coding.

FYI: I will probably add another class the is an extension called
BankMoney, since the Bank has no issue converting automatically
upwards or downwards and they also allow for negative ammounts.

On Nov 8, 11:00 pm, Marnen Laibow-Koser <rails-mailing-l...@andreas-


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marnen Laibow-Koser  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 19:45
From: Marnen Laibow-Koser <rails-mailing-l...@andreas-s.net>
Date: Mon, 9 Nov 2009 20:45:49 +0100
Local: Mon 9 Nov 2009 19:45
Subject: Re: Custom Validations Altering Variable

The easiest way to do this, I think, would be to define class Currency,
with subclasses (or perhaps instances) SilverPiece, GoldPiece, and so
on.  There's lots of info floating around on handling multiple
real-world currencies; you should be able to do D&D money the same way.

> I was going to add some options to where if it was set to something
> below 0 that it would automatically subtract 1 from the one above it,
> add 10 to itself, and continue doing the math while repeating if
> necessary with recursion. However that would be in issue if the one
> above it was 0 since if it was subtracted by 1 it would set the one
> below it to -1 causing it to repeat indefinitely.

> I hope this kind of makes since, and I am kind of stumped on doing
> this without an insane amount of repeated coding.

Use the power of a decent object model.  I'll try to post a quick
example if I have time.

> FYI: I will probably add another class the is an extension called
> BankMoney, since the Bank has no issue converting automatically
> upwards or downwards and they also allow for negative ammounts.

> On Nov 8, 11:00 pm, Marnen Laibow-Koser <rails-mailing-l...@andreas-

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org
--
Posted via http://www.ruby-forum.com/.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
7H3LaughingMan  
View profile   Translate to Translated (View Original)
 More options 10 Nov, 07:18
From: 7H3LaughingMan <austin.brk...@gmail.com>
Date: Mon, 9 Nov 2009 23:18:48 -0800 (PST)
Local: Tues 10 Nov 2009 07:18
Subject: Re: Custom Validations Altering Variable
http://pastie.org/691288

I did some fine tuning and came up with a rather interesting way of
keeping all of it to one class to represent the money. This is an
example of one of the sets, the others look like this with a few
changes. At the moment if it tries to be set to something below 0 it
will take it from itself and if necessary work it's way up first and
then down. I haven't been able to test this yet but I would love it if
you could take a look at it and see if there might be a way to cut it
down some more (The original was hideous!)

Hopefully if I keep it to this one class then I could finally take a
step forward and continue adding more and more to the database.

On Nov 9, 1:45 pm, Marnen Laibow-Koser <rails-mailing-l...@andreas-


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marnen Laibow-Koser  
View profile   Translate to Translated (View Original)
 More options 10 Nov, 13:02
From: Marnen Laibow-Koser <rails-mailing-l...@andreas-s.net>
Date: Tue, 10 Nov 2009 14:02:52 +0100
Local: Tues 10 Nov 2009 13:02
Subject: Re: Custom Validations Altering Variable

7H3LaughingMan wrote:
> http://pastie.org/691288

> I did some fine tuning and came up with a rather interesting way of
> keeping all of it to one class to represent the money. This is an
> example of one of the sets, the others look like this with a few
> changes. At the moment if it tries to be set to something below 0 it
> will take it from itself and if necessary work it's way up first and
> then down. I haven't been able to test this yet but I would love it if
> you could take a look at it and see if there might be a way to cut it
> down some more (The original was hideous!)

So is what you've just posted -- it's repetitive and unreadable.
There's no point in keeping it all in one class if the code quality
suffers.

> Hopefully if I keep it to this one class then I could finally take a
> step forward and continue adding more and more to the database.

The two have nothing to do with each other.  Don't be afraid to
introduce classes if it makes life simpler.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org
--
Posted via http://www.ruby-forum.com/.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google