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
Source
# File lib/sequel/plugins/dirty.rb 78 def after_save 79 super 80 reset_initial_values 81 end
Reset the initial values after saving.
Source
# File lib/sequel/plugins/dirty.rb 85 def after_update 86 super 87 @previous_changes = column_changes 88 end
Save the current changes so they are available after updating. This happens before after_save resets them.
Source
# File lib/sequel/plugins/dirty.rb 95 def column_change(column) 96 [initial_value(column), get_column_value(column)] if column_changed?(column) 97 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']
Source
# File lib/sequel/plugins/dirty.rb 117 def column_changed?(column) 118 initial_values.has_key?(column) 119 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
Source
# File lib/sequel/plugins/dirty.rb 103 def column_changes 104 h = {} 105 initial_values.each do |column, value| 106 h[column] = [value, get_column_value(column)] 107 end 108 h 109 end
A hash with column symbol keys and pairs of initial and current values for all changed columns.
column_changes # => {:name => ['Initial', 'Current']}
Source
# File lib/sequel/plugins/dirty.rb 132 def column_previously_changed?(column, opts=OPTS) 133 return false unless (pc = @previous_changes) && (val = pc[column]) 134 135 if opts.has_key?(:from) 136 return false unless val[0] == opts[:from] 137 end 138 139 if opts.has_key?(:to) 140 return false unless val[1] == opts[:to] 141 end 142 143 true 144 end
Whether the column was previously changed. Options:
- :from
-
If given, the previous initial value of the column must match this
- :to
-
If given, the previous changed value of the column must match this
update(name: 'Current') previous_changes # => {:name=>['Initial', 'Current']} column_previously_changed?(:name) # => true column_previously_changed?(:id) # => false column_previously_changed?(:name, from: 'Initial', to: 'Current') # => true column_previously_changed?(:name, from: 'Foo', to: 'Current') # => false
Source
# File lib/sequel/plugins/dirty.rb 152 def column_previously_was(column) 153 (pc = @previous_changes) && (val = pc[column]) && val[0] 154 end
The previous value of the column, which is the initial value of the column before the object was previously saved.
initial_value(:name) # => 'Initial' update(name: 'Current') column_previously_was(:name) # => 'Initial'
Source
# File lib/sequel/plugins/dirty.rb 157 def freeze 158 initial_values.freeze 159 missing_initial_values.freeze 160 @previous_changes.freeze if @previous_changes 161 super 162 end
Freeze internal data structures
Source
# File lib/sequel/plugins/dirty.rb 169 def initial_value(column) 170 initial_values.fetch(column){get_column_value(column)} 171 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'
Source
# File lib/sequel/plugins/dirty.rb 176 def initial_values 177 @initial_values ||= {} 178 end
A hash with column symbol keys and initial values.
initial_values # {:name => 'Initial'}
Source
# File lib/sequel/plugins/dirty.rb 185 def reset_column(column) 186 if initial_values.has_key?(column) 187 set_column_value(:"#{column}=", initial_values[column]) 188 end 189 if missing_initial_values.include?(column) 190 values.delete(column) 191 end 192 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'
Source
# File lib/sequel/plugins/dirty.rb 200 def will_change_column(column) 201 _add_changed_column(column) 202 check_missing_initial_value(column) 203 204 value = if initial_values.has_key?(column) 205 initial_values[column] 206 else 207 get_column_value(column) 208 end 209 210 initial_values[column] = if value && value != true && value.respond_to?(:clone) 211 begin 212 value.clone 213 rescue TypeError 214 value 215 end 216 else 217 value 218 end 219 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']
Private Instance Methods
Source
# File lib/sequel/plugins/dirty.rb 224 def _clear_changed_columns(reason) 225 reset_initial_values if reason == :initialize || reason == :refresh 226 super 227 end
Reset initial values when clearing changed columns
Source
# File lib/sequel/plugins/dirty.rb 232 def change_column_value(column, value) 233 if (iv = initial_values).has_key?(column) 234 initial = iv[column] 235 super 236 if value == initial 237 _changed_columns.delete(column) unless missing_initial_values.include?(column) 238 iv.delete(column) 239 end 240 else 241 if db_schema[column] 242 check_missing_initial_value(column) 243 iv[column] = get_column_value(column) 244 end 245 246 super 247 end 248 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.
Source
# File lib/sequel/plugins/dirty.rb 253 def check_missing_initial_value(column) 254 unless values.has_key?(column) || (miv = missing_initial_values).include?(column) 255 miv << column 256 end 257 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.
Source
# File lib/sequel/plugins/dirty.rb 260 def initialize_copy(other) 261 super 262 @initial_values = Hash[other.initial_values] 263 @missing_initial_values = other.send(:missing_initial_values).dup 264 @previous_changes = Hash[other.previous_changes] if other.previous_changes 265 self 266 end
Duplicate internal data structures
Source
# File lib/sequel/plugins/dirty.rb 271 def missing_initial_values 272 @missing_initial_values ||= [] 273 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.
Source
# File lib/sequel/plugins/dirty.rb 276 def reset_initial_values 277 @initial_values.clear if @initial_values 278 @missing_initial_values.clear if @missing_initial_values 279 end
Clear the data structures that store the initial values.