module Sequel::Plugins::SplitValues

  1. lib/sequel/plugins/split_values.rb

The split_values plugin splits the values hash retreived from the database, and moves keys from the values hash that are not columns in the model’s dataset to a separate hash. This makes it so the values hash only stores columns from the model’s dataset.

Among other things, this allows you to save model objects even if they were retrieved with additional columns, and have equality comparisons with other instances not care about non-column values.

Example:

class Album < Sequel::Model
  plugin :split_values
end
a1 = Album[1]
a2 = Album.select_append(Sequel.as(true, :exists))[1]
a1.name # => 'Album Name'
a2.name # => 'Album Name'
a1[:exists] # => nil
a2[:exists] # => true
a1 == a2 # => true
a2.values # => {:id=>1, :name=>'Album Name'}
a2.save # Works

Usage:

# Make all model subclass instances split values
# (called before loading subclasses)
Sequel::Model.plugin :split_values

# Make the Album class split values
Album.plugin :split_values