module Sequel::Plugins::Dirty::InstanceMethods

Attributes

previous_changes[R]

A hash of previous changes before the object was saved, in the same format as column_changes. Note that this is not necessarily the same as the columns that were used in the update statement.

Public Instance Methods

column_change(column) click to toggle source

An array with the initial value and the current value of the column, if the column has been changed. If the column has not been changed, returns nil.

column_change(:name) # => ['Initial', 'Current']
# File lib/sequel/plugins/dirty.rb, line 62
def column_change(column)
  [initial_value(column), get_column_value(column)] if column_changed?(column)
end
column_changed?(column) click to toggle source

Either true or false depending on whether the column has changed. Note that this is not exactly the same as checking if the column is in changed_columns, if the column was not set initially.

column_changed?(:name) # => true
# File lib/sequel/plugins/dirty.rb, line 84
def column_changed?(column)
  initial_values.has_key?(column)
end
column_changes() click to toggle source

A hash with column symbol keys and pairs of initial and current values for all changed columns.

column_changes # => {:name => ['Initial', 'Current']}
# File lib/sequel/plugins/dirty.rb, line 70
def column_changes
  h = {}
  initial_values.each do |column, value|
    h[column] = [value, get_column_value(column)]
  end
  h
end
freeze() click to toggle source

Freeze internal data structures

Calls superclass method
# File lib/sequel/plugins/dirty.rb, line 89
def freeze
  initial_values.freeze
  missing_initial_values.freeze
  @previous_changes.freeze if @previous_changes
  super
end
initial_value(column) click to toggle source

The initial value of the given column. If the column value has not changed, this will be the same as the current value of the column.

initial_value(:name) # => 'Initial'
# File lib/sequel/plugins/dirty.rb, line 101
def initial_value(column)
  initial_values.fetch(column){get_column_value(column)}
end
initial_values() click to toggle source

A hash with column symbol keys and initial values.

initial_values # {:name => 'Initial'}
# File lib/sequel/plugins/dirty.rb, line 108
def initial_values
  @initial_values ||= {}
end
reset_column(column) click to toggle source

Reset the column to its initial value. If the column was not set initial, removes it from the values.

reset_column(:name)
name # => 'Initial'
# File lib/sequel/plugins/dirty.rb, line 117
def reset_column(column)
  if initial_values.has_key?(column)
    set_column_value(:"#{column}=", initial_values[column])
  end
  if missing_initial_values.include?(column)
    values.delete(column)
  end
end
will_change_column(column) click to toggle source

Manually specify that a column will change. This should only be used if you plan to modify a column value in place, which is not recommended.

will_change_column(:name)
name.gsub(/i/i, 'o')
column_change(:name) # => ['Initial', 'onotoal']
# File lib/sequel/plugins/dirty.rb, line 132
def will_change_column(column)
  changed_columns << column unless changed_columns.include?(column)
  check_missing_initial_value(column)

  value = if initial_values.has_key?(column)
    initial_values[column]
  else
    get_column_value(column)
  end

  initial_values[column] = if value && value != true && value.respond_to?(:clone)
    begin
      value.clone
    rescue TypeError
      value
    end
  else
    value
  end
end

Private Instance Methods

_refresh_set_values(hash) click to toggle source

Reset the initial values when setting values.

Calls superclass method
# File lib/sequel/plugins/dirty.rb, line 156
def _refresh_set_values(hash)
  reset_initial_values
  super
end
after_save() click to toggle source

Reset the initial values after saving.

Calls superclass method
# File lib/sequel/plugins/dirty.rb, line 162
def after_save
  super
  reset_initial_values
end
after_update() click to toggle source

Save the current changes so they are available after updating. This happens before #after_save resets them.

Calls superclass method
# File lib/sequel/plugins/dirty.rb, line 169
def after_update
  super
  @previous_changes = column_changes
end
change_column_value(column, value) click to toggle source

When changing the column value, save the initial column value. If the column value is changed back to the initial value, update changed columns to remove the column.

Calls superclass method
# File lib/sequel/plugins/dirty.rb, line 177
def change_column_value(column, value)
  if (iv = initial_values).has_key?(column)
    initial = iv[column]
    super
    if value == initial
      changed_columns.delete(column) unless missing_initial_values.include?(column)
      iv.delete(column)
    end
  else
    check_missing_initial_value(column)
    iv[column] = get_column_value(column)
    super
  end
end
check_missing_initial_value(column) click to toggle source

If the values hash does not contain the column, make sure #missing_initial_values does so that it doesn't get deleted from changed_columns if changed back, and so that resetting the column value can be handled correctly.

# File lib/sequel/plugins/dirty.rb, line 195
def check_missing_initial_value(column)
  unless values.has_key?(column) || (miv = missing_initial_values).include?(column)
    miv << column
  end
end
initialize_copy(other) click to toggle source

Duplicate internal data structures

Calls superclass method
# File lib/sequel/plugins/dirty.rb, line 202
def initialize_copy(other)
  super
  @initial_values = other.initial_values.dup
  @missing_initial_values = other.send(:missing_initial_values).dup
  @previous_changes = other.previous_changes.dup if other.previous_changes
  self
end
initialize_set(h) click to toggle source

Reset the initial values when initializing.

Calls superclass method
# File lib/sequel/plugins/dirty.rb, line 211
def initialize_set(h)
  super
  reset_initial_values
end
missing_initial_values() click to toggle source

Array holding column symbols that were not present initially. This is necessary to differentiate between values that were not present and values that were present but equal to nil.

# File lib/sequel/plugins/dirty.rb, line 219
def missing_initial_values
  @missing_initial_values ||= []
end
reset_initial_values() click to toggle source

Clear the data structures that store the initial values.

# File lib/sequel/plugins/dirty.rb, line 224
def reset_initial_values
  @initial_values.clear if @initial_values
  @missing_initial_values.clear if @missing_initial_values
end