A hash of previous changes before the object was saved, in the same format as #. Note that this is not necessarily the same as the columns that were used in the update statement.
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 62: def column_change(column) 63: [initial_value(column), send(column)] if column_changed?(column) 64: end
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 84: def column_changed?(column) 85: initial_values.has_key?(column) 86: end
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 70: def column_changes 71: h = {} 72: initial_values.each do |column, value| 73: h[column] = [value, send(column)] 74: end 75: h 76: end
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 93 93: def initial_value(column) 94: initial_values.fetch(column){send(column)} 95: end
A hash with column symbol keys and initial values.
initial_values # {:name => 'Initial'}
# File lib/sequel/plugins/dirty.rb, line 100 100: def initial_values 101: @initial_values ||= {} 102: end
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 109 109: def reset_column(column) 110: if initial_values.has_key?(column) 111: send(:"#{column}=", initial_values[column]) 112: end 113: if missing_initial_values.include?(column) 114: values.delete(column) 115: end 116: end
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 124 124: def will_change_column(column) 125: changed_columns << column unless changed_columns.include?(column) 126: check_missing_initial_value(column) 127: 128: value = if initial_values.has_key?(column) 129: initial_values[column] 130: else 131: send(column) 132: end 133: 134: initial_values[column] = if value && value != true && value.respond_to?(:clone) 135: begin 136: value.clone 137: rescue TypeError 138: value 139: end 140: else 141: value 142: end 143: end
Reset the initial values when refreshing.
# File lib/sequel/plugins/dirty.rb, line 161 161: def _refresh(dataset) 162: super 163: reset_initial_values 164: end
Reset the initial values after saving.
# File lib/sequel/plugins/dirty.rb, line 148 148: def after_save 149: super 150: reset_initial_values 151: end
Save the current changes so they are available after updating. This happens before after_save resets them.
# File lib/sequel/plugins/dirty.rb, line 155 155: def after_update 156: super 157: @previous_changes = column_changes 158: end
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.
# File lib/sequel/plugins/dirty.rb, line 169 169: def change_column_value(column, value) 170: if (iv = initial_values).has_key?(column) 171: initial = iv[column] 172: super 173: if value == initial 174: changed_columns.delete(column) unless missing_initial_values.include?(column) 175: iv.delete(column) 176: end 177: else 178: check_missing_initial_value(column) 179: iv[column] = send(column) 180: super 181: end 182: end
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 reseting the column value can be handled correctly.
# File lib/sequel/plugins/dirty.rb, line 187 187: def check_missing_initial_value(column) 188: unless values.has_key?(column) || (miv = missing_initial_values).include?(column) 189: miv << column 190: end 191: end
Reset the initial values when initializing.
# File lib/sequel/plugins/dirty.rb, line 194 194: def initialize_set(h) 195: super 196: reset_initial_values 197: end
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 202 202: def missing_initial_values 203: @missing_initial_values ||= [] 204: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.