On Wednesday, April 2, 2014 8:27:03 AM UTC-7, Oldřich Vetešník wrote:
>
> Hello Jeremy,
>
> we already discussed this somewhat but I'd like to bring it up again and
> try to spice it a notch.
>
> A User has an access_token (hash) field, which is unique and it is
> randomly generated. I know you said this is not a good combination :-) but
> we don't seem to find a better solution as we cannot guarantee a unique
> value due to race conditions. The retry mechanism actually seems quite
> elegant as you don't have to litter your code with begin/rescue loops in
> many places.
>
> I made a simple plugin that adds two methods (create_retry and save_retry)
> that do what you pointed to (transaction num_retries option + a few
> others). The hash is generated in the before_save hook.
>
> While this works, it is really not very flexible because you have only one
> location where you can define those random values. Sometimes it would be
> neat to be able to pass a block to those methods which would be executed
> before every try, effectively re-setting those fields.
>
> A quick demo:
>
> class User < Sequel::Model
> plugin :create_retry, num_retries: 2 # that is 3 tries
> end
>
> user = User.new(name: 'Dave', ...)
> user.save_retry do |user|
> user.access_token = some_random_hash() # this would be neat to call before
> every try (save)
> end
>
> User.create_retry(name: 'Dave', ...) do |user|
> user.access_token = some_random_hash()
> end
>
> I tried to implement this but then I realized the block is executed only
> once and not every time the try is executed. (See those yields in the
> plugin.)
>
> What do you think about all of this? Is it possible? Here is the whole
> thing: https://gist.github.com/ollie/9936087
>
Have the create_retry plugin also load the instance_hooks plugin, adding:
def self.apply(model, opts=OPTS)
model.plugin :instance_hooks
end
then instead of doing:
yield self if block_given?
do:
before_save_hook(&block) if block
Also, I would change create_retry to be:
def create_retry(values={}, &block)
new(values).save_retry(&block)
end
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.