module Sequel::Plugins::AssociationPks::InstanceMethods

Public Instance Methods

after_save() click to toggle source

After creating an object, if there are any saved association pks, call the related association pks setters.

Calls superclass method
# File lib/sequel/plugins/association_pks.rb, line 147
def after_save
  if assoc_pks = @_association_pks
    assoc_pks.each do |name, pks|
      instance_exec(pks, &model.association_reflection(name)[:pks_setter]) unless pks.empty?
    end
    @_association_pks = nil
  end
  super
end
refresh() click to toggle source

Clear the associated pks if explicitly refreshing.

Calls superclass method
# File lib/sequel/plugins/association_pks.rb, line 158
def refresh
  @_association_pks = nil
  super
end

Private Instance Methods

_association_pks_getter(opts) click to toggle source

Return the primary keys of the associated objects. If the receiver is a new object, return any saved pks, or an empty array if no pks have been saved.

# File lib/sequel/plugins/association_pks.rb, line 168
def _association_pks_getter(opts)
  delay = opts[:delay_pks]
  if new? && delay
    (@_association_pks ||= {})[opts[:name]] ||= []
  elsif delay == :always && @_association_pks && (objs = @_association_pks[opts[:name]])
    objs
  else
    instance_exec(&opts[:pks_getter])
  end
end
_association_pks_setter(opts, pks) click to toggle source

Update which objects are associated to the receiver. If the receiver is a new object, save the pks so the update can happen after the received has been saved.

# File lib/sequel/plugins/association_pks.rb, line 182
def _association_pks_setter(opts, pks)
  delay = opts[:delay_pks]
  if (new? && delay) || (delay == :always)
    modified!
    (@_association_pks ||= {})[opts[:name]] = pks
  else
    instance_exec(pks, &opts[:pks_setter])
  end
end
convert_cpk_array(opts, cpks) click to toggle source

If any of associated class's composite primary key column types is integer, typecast the appropriate values to integer before using them.

# File lib/sequel/plugins/association_pks.rb, line 194
def convert_cpk_array(opts, cpks)
  if klass = opts.associated_class and sch = klass.db_schema and (cols = sch.values_at(*klass.primary_key)).all? and (convs = cols.map{|c| c[:type] == :integer}).any?
    cpks.map do |cpk|
      cpk.zip(convs).map do |pk, conv|
        conv ? model.db.typecast_value(:integer, pk) : pk
      end
    end
  else
    cpks
  end
end
convert_pk_array(opts, pks) click to toggle source

If the associated class's primary key column type is integer, typecast all provided values to integer before using them.

# File lib/sequel/plugins/association_pks.rb, line 208
def convert_pk_array(opts, pks)
  if klass = opts.associated_class and sch = klass.db_schema and col = sch[klass.primary_key] and col[:type] == :integer
    pks.map{|pk| model.db.typecast_value(:integer, pk)}
  else
    pks
  end
end