On Jul 14, 1:14 pm, cult hero <[email protected]> wrote:
> Okay, I thought this would be relatively straightforward but I managed
> to surprise myself with a "SystemStackError: stack level too deep"
> error.
>
> What is the bast practice for "filtering" data in a setter method of a
> model? I presumed simply overriding the setter method would be best,
> but I'm not even sure how to do that.
>
> For instance, I want to make sure usernames are always forced down to
> lowercase in a particular model. So I tried this:
>
> def username=(username)
>       username.downcase!
>       self.set :username => username
> end
>
> Without looking at the docs I had presumed "set" worked independently
> of the getter/setter methods. Although I'm not positive now, that
> seems to be the case (which created an infinite loop of sorts).
>
> This might be my ignorance of Ruby at work, but it seems that creating
> an alias is the only way to do this.
>
> alias :old_username= :username=
>
> def username=(username)
>       username.downcase!
>       self.old_username = username
> end
>
> This just doesn't seem right at all.
>
> So:
>
> 1. What's the "proper" way to override an existing setter method in
> Sequel?

spox has the right idea, though I'd use:

  def username=(u)
    super(u.downcase)
  end

> 2. Should I be filtering at the setter level or should I be using a
> hook or something else to accomplish this similar to validations?

It depends on the app, though I think overriding the setter method is
the best way.  However, it's not the most full proof.  It won't work
if you do the following:

  model[:username] = 'USER'
  model.values[:username] = 'USER'

Using validations can catch those cases, if that is important to you.
I would discourage using either of those two methods to set attribute
values, though.

Jeremy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sequel-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/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to