Sha256: 8d987eda8937e48279e5e0d40fb80cd6ba208953043e411176d3ebf10bb72d8e

Contents?: true

Size: 774 Bytes

Versions: 30

Compression:

Stored size: 774 Bytes

Contents

# frozen_string_literal: true

# Allows you to use PostgreSQL transaction advisory locks for application-level mutexes
module Sequel::Plugins::Synchronize
  module ClassMethods
    # Watch Sequel::Synchronize#synchronize_with
    def synchronize_with(*args, &block)
      db.extension(:synchronize).synchronize_with(*args, &block)
    end
  end

  module InstanceMethods
    # Just like Sequel::Synchronize#synchronize_with,
    # but name, which is joined from args, is combined with table_name and primary_key
    def synchronize(*args, **options)
      self.class.synchronize_with(lock_key_for(args), **options) { yield(reload) }
    end

    private

    def lock_key_for(args)
      [self.class.table_name, self[primary_key], *args].flatten.join("-")
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
umbrellio-sequel-plugins-0.7.0.52 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.6.0.46 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.6.0 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.6.0.36 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.5.1.27 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.5.1.26 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.5.1.25 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.5.1.18 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.5.1.17 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.5.1 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.5.0 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.4.0.164 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.4.0.152 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.4.0.145 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.4.0.134 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.4.0.121 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.4.0.114 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.4.0.112 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.4.0.107 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.4.0.81 lib/sequel/plugins/synchronize.rb