Sha256: 778bd3949dde7fef85ecdc0f41aa9da85675c09f49133dfd45317ec0fbdfb09d

Contents?: true

Size: 754 Bytes

Versions: 28

Compression:

Stored size: 754 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(...)
      db.extension(:synchronize).synchronize_with(...)
    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

28 entries across 28 versions & 1 rubygems

Version Path
umbrellio-sequel-plugins-0.17.0 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.16.1 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.16.0.239 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.16.0.238 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.16.0.235 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.16.0.234 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.16.0.233 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.16.0 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.16.0.211 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.15.0.198 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.14.0.192 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.14.0.189 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.14.0.188 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.14.0.187 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.14.0 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.13.0.185 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.13.0.172 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.13.0 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.12.0.169 lib/sequel/plugins/synchronize.rb
umbrellio-sequel-plugins-0.12.0 lib/sequel/plugins/synchronize.rb