module Sequel::Plugins::Dirty::InstanceMethods
Attributes
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
Reset the initial values after saving.
# File lib/sequel/plugins/dirty.rb 65 def after_save 66 super 67 reset_initial_values 68 end
Save the current changes so they are available after updating. This happens before after_save
resets them.
# File lib/sequel/plugins/dirty.rb 72 def after_update 73 super 74 @previous_changes = column_changes 75 end
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 82 def column_change(column) 83 [initial_value(column), get_column_value(column)] if column_changed?(column) 84 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 104 def column_changed?(column) 105 initial_values.has_key?(column) 106 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 90 def column_changes 91 h = {} 92 initial_values.each do |column, value| 93 h[column] = [value, get_column_value(column)] 94 end 95 h 96 end
Freeze internal data structures
# File lib/sequel/plugins/dirty.rb 109 def freeze 110 initial_values.freeze 111 missing_initial_values.freeze 112 @previous_changes.freeze if @previous_changes 113 super 114 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 121 def initial_value(column) 122 initial_values.fetch(column){get_column_value(column)} 123 end
A hash with column symbol keys and initial values.
initial_values # {:name => 'Initial'}
# File lib/sequel/plugins/dirty.rb 128 def initial_values 129 @initial_values ||= {} 130 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 137 def reset_column(column) 138 if initial_values.has_key?(column) 139 set_column_value(:"#{column}=", initial_values[column]) 140 end 141 if missing_initial_values.include?(column) 142 values.delete(column) 143 end 144 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 152 def will_change_column(column) 153 _add_changed_column(column) 154 check_missing_initial_value(column) 155 156 value = if initial_values.has_key?(column) 157 initial_values[column] 158 else 159 get_column_value(column) 160 end 161 162 initial_values[column] = if value && value != true && value.respond_to?(:clone) 163 begin 164 value.clone 165 rescue TypeError 166 value 167 end 168 else 169 value 170 end 171 end
Private Instance Methods
Reset initial values when clearing changed columns
# File lib/sequel/plugins/dirty.rb 176 def _clear_changed_columns(reason) 177 reset_initial_values if reason == :initialize || reason == :refresh 178 super 179 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 184 def change_column_value(column, value) 185 if (iv = initial_values).has_key?(column) 186 initial = iv[column] 187 super 188 if value == initial 189 _changed_columns.delete(column) unless missing_initial_values.include?(column) 190 iv.delete(column) 191 end 192 else 193 check_missing_initial_value(column) 194 iv[column] = get_column_value(column) 195 super 196 end 197 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 resetting the column value can be handled correctly.
# File lib/sequel/plugins/dirty.rb 202 def check_missing_initial_value(column) 203 unless values.has_key?(column) || (miv = missing_initial_values).include?(column) 204 miv << column 205 end 206 end
Duplicate internal data structures
# File lib/sequel/plugins/dirty.rb 209 def initialize_copy(other) 210 super 211 @initial_values = Hash[other.initial_values] 212 @missing_initial_values = other.send(:missing_initial_values).dup 213 @previous_changes = Hash[other.previous_changes] if other.previous_changes 214 self 215 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 220 def missing_initial_values 221 @missing_initial_values ||= [] 222 end
Clear the data structures that store the initial values.
# File lib/sequel/plugins/dirty.rb 225 def reset_initial_values 226 @initial_values.clear if @initial_values 227 @missing_initial_values.clear if @missing_initial_values 228 end