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
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 69 def column_change(column) 70 [initial_value(column), get_column_value(column)] if column_changed?(column) 71 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 91 def column_changed?(column) 92 initial_values.has_key?(column) 93 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 77 def column_changes 78 h = {} 79 initial_values.each do |column, value| 80 h[column] = [value, get_column_value(column)] 81 end 82 h 83 end
Freeze internal data structures
# File lib/sequel/plugins/dirty.rb 96 def freeze 97 initial_values.freeze 98 missing_initial_values.freeze 99 @previous_changes.freeze if @previous_changes 100 super 101 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 108 def initial_value(column) 109 initial_values.fetch(column){get_column_value(column)} 110 end
A hash with column symbol keys and initial values.
initial_values # {:name => 'Initial'}
# File lib/sequel/plugins/dirty.rb 115 def initial_values 116 @initial_values ||= {} 117 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 124 def reset_column(column) 125 if initial_values.has_key?(column) 126 set_column_value(:"#{column}=", initial_values[column]) 127 end 128 if missing_initial_values.include?(column) 129 values.delete(column) 130 end 131 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 139 def will_change_column(column) 140 _add_changed_column(column) 141 check_missing_initial_value(column) 142 143 value = if initial_values.has_key?(column) 144 initial_values[column] 145 else 146 get_column_value(column) 147 end 148 149 initial_values[column] = if value && value != true && value.respond_to?(:clone) 150 begin 151 value.clone 152 rescue TypeError 153 value 154 end 155 else 156 value 157 end 158 end
Private Instance Methods
Reset initial values when clearing changed columns
# File lib/sequel/plugins/dirty.rb 163 def _clear_changed_columns(reason) 164 reset_initial_values if reason == :initialize || reason == :refresh 165 super 166 end
Reset the initial values after saving.
# File lib/sequel/plugins/dirty.rb 169 def after_save 170 super 171 reset_initial_values 172 end
Save the current changes so they are available after updating. This happens before after_save
resets them.
# File lib/sequel/plugins/dirty.rb 176 def after_update 177 super 178 @previous_changes = column_changes 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