Methods
Public Instance
Attributes
| previous_changes | [R] |
A hash of previous changes before the object was saved, in the same format as |
Public Instance methods
Reset the initial values after saving.
# File lib/sequel/plugins/dirty.rb 78 def after_save 79 super 80 reset_initial_values 81 end
Save the current changes so they are available after updating. This happens before after_save resets them.
# File lib/sequel/plugins/dirty.rb 85 def after_update 86 super 87 @previous_changes = column_changes 88 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 95 def column_change(column) 96 [initial_value(column), get_column_value(column)] if column_changed?(column) 97 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 117 def column_changed?(column) 118 initial_values.has_key?(column) 119 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 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
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
# 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
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'
# File lib/sequel/plugins/dirty.rb 152 def column_previously_was(column) 153 (pc = @previous_changes) && (val = pc[column]) && val[0] 154 end
Freeze internal data structures
# 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
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 169 def initial_value(column) 170 initial_values.fetch(column){get_column_value(column)} 171 end
A hash with column symbol keys and initial values.
initial_values # {:name => 'Initial'}
# File lib/sequel/plugins/dirty.rb 176 def initial_values 177 @initial_values ||= {} 178 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 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
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 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