Sha256: cde3661127e67f27f092303ee2f7c3f7deeb6493ae3ecb1d1d2d14bebcd45a59

Contents?: true

Size: 1.33 KB

Versions: 20

Compression:

Stored size: 1.33 KB

Contents

module Sequel
  module Plugins
    # The update_primary_key plugin allows you to modify an object's
    # primary key and then save the record.  Sequel does not work
    # correctly with primary key modifications by default.  Sequel
    # is designed to work with surrogate primary keys that never need to be
    # modified, but this plugin makes it work correctly with natural
    # primary keys that may need to be modified. Example:
    #
    #   album = Album[1]
    #   album.id = 2
    #   album.save
    # 
    # Usage:
    #
    #   # Make all model subclasses support primary key updates
    #   # (called before loading subclasses)
    #   Sequel::Model.plugin :update_primary_key
    #
    #   # Make the Album class support primary key updates
    #   Album.plugin :update_primary_key
    module UpdatePrimaryKey
      module ClassMethods
        # Cache the pk_hash when loading records
        def call(h)
          r = super(h)
          r.pk_hash
          r
        end
      end

      module InstanceMethods
        # Clear the pk_hash and object dataset cache, and recache
        # the pk_hash
        def after_update
          super
          @pk_hash = nil
          pk_hash
        end

        # Cache the pk_hash instead of generating it every time
        def pk_hash
          @pk_hash ||= super
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
sequel-3.46.0 lib/sequel/plugins/update_primary_key.rb
sequel-3.45.0 lib/sequel/plugins/update_primary_key.rb
sequel-3.44.0 lib/sequel/plugins/update_primary_key.rb
sequel-3.43.0 lib/sequel/plugins/update_primary_key.rb
sequel-3.42.0 lib/sequel/plugins/update_primary_key.rb
sequel-3.41.0 lib/sequel/plugins/update_primary_key.rb
sequel-3.40.0 lib/sequel/plugins/update_primary_key.rb
sequel-3.39.0 lib/sequel/plugins/update_primary_key.rb
sequel-3.38.0 lib/sequel/plugins/update_primary_key.rb
sequel-3.37.0 lib/sequel/plugins/update_primary_key.rb
sequel-3.36.1 lib/sequel/plugins/update_primary_key.rb
sequel-3.36.0 lib/sequel/plugins/update_primary_key.rb
sequel-3.35.0 lib/sequel/plugins/update_primary_key.rb
sequel-3.34.1 lib/sequel/plugins/update_primary_key.rb
sequel-3.34.0 lib/sequel/plugins/update_primary_key.rb
sequel-3.33.0 lib/sequel/plugins/update_primary_key.rb
sequel-3.32.0 lib/sequel/plugins/update_primary_key.rb
sequel-3.31.0 lib/sequel/plugins/update_primary_key.rb
sequel-3.30.0 lib/sequel/plugins/update_primary_key.rb
sequel-3.29.0 lib/sequel/plugins/update_primary_key.rb