Sha256: 3e929dc20c0c615eb1191c69f1ba226221ed15b736ce8cc0d660107a18005b93

Contents?: true

Size: 747 Bytes

Versions: 58

Compression:

Stored size: 747 Bytes

Contents

# frozen_string_literal: true

# Sequel analog for `ActiveRecord::Base#dup` method
module Sequel::Plugins::Duplicate
  module ClassMethods
    # Returns a copy of current model
    #
    # @param model [Sequel::Model] source object
    # @param new_attrs [Hash] attributes to override
    #
    # @return [Sequel::Model]
    def duplicate(model, **new_attrs)
      pk = *primary_key
      attrs = model.values.reject { |key, *| pk.include?(key) }
      new(**attrs, **new_attrs)
    end
  end

  module InstanceMethods
    # Returns a copy of current model
    #
    # @param new_attrs [Hash] attributes to override
    #
    # @return [Sequel::Model]
    def duplicate(**new_attrs)
      self.class.duplicate(self, **new_attrs)
    end
  end
end

Version data entries

58 entries across 58 versions & 1 rubygems

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