On Nov 19, 8:43 pm, Gavin Kistner <[email protected]> wrote:
> def create_frob( pk, values )
>   Frob[ :id=>pk ] || Frob.new( values.merge :id=>pk )
> end
>
> create_frob( "foo", :name=>"Forble" )
> #=> Error: method id= doesn't exist or access is restricted to it
>
> Oops. Try again.
>
> def create_frob( pk, values )
>   Frob.unrestrict_primary_key
>   f = Frob[ :id=>pk ] || Frob.new( values.merge :id=>pk )
>   Frob.restrict_primary_key
>   f
> end
>
> Ew...that just won't do. And it maybe restricts when it shouldn't.
>
> module Sequel::Model::ClassMethods
>    def unrestrict_primary_key
>       if block_given?
>          original_value = @restrict_primary_key
>         �...@restrict_primary_key = false
>          result = yield
>         �...@restrict_primary_key = original_value
>          result
>       else
>         �...@restrict_primary_key = false
>       end
>    end
> end
>
> def create_frob( pk, values )
>   Frob.unrestrict_primary_key do
>     Frob[ :id=>pk ] || Frob.new( values.merge :id=>pk )
>   end
> end
>
> Ahhhhh. I think that's much better.
>
> What do you think? (Of the idea, not the exact code.) Like Dir.chdir, it 
> ensures a particular, necessary set of conditions are met, and then returns 
> the state to as it was beforehand.

I don't think it's a good idea. For one, it isn't thread-safe, so at
the very least if you wanted something like that, you need to wrap it
in a mutex.

How about this instead:

  def create_frob( pk, values )
    unless frob = Frob[pk]
      frob = Frob.new(values)
      frob.id = pk
    end
    frob
  end

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


Reply via email to