module Sequel::Plugins::Dirty::InstanceMethods

Attributes

previous_changes[R]

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

after_save() click to toggle source

Reset the initial values after saving.

Calls superclass method
   # File lib/sequel/plugins/dirty.rb
74 def after_save
75   super
76   reset_initial_values
77 end
after_update() click to toggle source

Save the current changes so they are available after updating. This happens before after_save resets them.

Calls superclass method
   # File lib/sequel/plugins/dirty.rb
81 def after_update
82   super
83   @previous_changes = column_changes
84 end
column_change(column) click to toggle source

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
91 def column_change(column)
92   [initial_value(column), get_column_value(column)] if column_changed?(column)
93 end
column_changed?(column) click to toggle source

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
113 def column_changed?(column)
114   initial_values.has_key?(column)
115 end
column_changes() click to toggle source

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
 99 def column_changes
100   h = {}
101   initial_values.each do |column, value|
102     h[column] = [value, get_column_value(column)]
103   end
104   h
105 end
column_previously_changed?(column, opts=OPTS) click to toggle source

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
128 def column_previously_changed?(column, opts=OPTS)
129   return false unless (pc = @previous_changes) && (val = pc[column])
130 
131   if opts.has_key?(:from)
132     return false unless val[0] == opts[:from]
133   end
134 
135   if opts.has_key?(:to)
136     return false unless val[1] == opts[:to]
137   end
138 
139   true
140 end
column_previously_was(column) click to toggle source

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
148 def column_previously_was(column)
149   (pc = @previous_changes) && (val = pc[column]) && val[0]
150 end
freeze() click to toggle source

Freeze internal data structures

Calls superclass method
    # File lib/sequel/plugins/dirty.rb
153 def freeze
154   initial_values.freeze
155   missing_initial_values.freeze
156   @previous_changes.freeze if @previous_changes
157   super
158 end
initial_value(column) click to toggle source

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
165 def initial_value(column)
166   initial_values.fetch(column){get_column_value(column)}
167 end
initial_values() click to toggle source

A hash with column symbol keys and initial values.

initial_values # {:name => 'Initial'}
    # File lib/sequel/plugins/dirty.rb
172 def initial_values
173   @initial_values ||= {}
174 end
reset_column(column) click to toggle source

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
181 def reset_column(column)
182   if initial_values.has_key?(column)
183     set_column_value(:"#{column}=", initial_values[column])
184   end
185   if missing_initial_values.include?(column)
186     values.delete(column)
187   end
188 end
will_change_column(column) click to toggle source

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
196 def will_change_column(column)
197   _add_changed_column(column)
198   check_missing_initial_value(column)
199 
200   value = if initial_values.has_key?(column)
201     initial_values[column]
202   else
203     get_column_value(column)
204   end
205 
206   initial_values[column] = if value && value != true && value.respond_to?(:clone)
207     begin
208       value.clone
209     rescue TypeError
210       value
211     end
212   else
213     value
214   end
215 end

Private Instance Methods

_clear_changed_columns(reason) click to toggle source

Reset initial values when clearing changed columns

Calls superclass method
    # File lib/sequel/plugins/dirty.rb
220 def _clear_changed_columns(reason)
221   reset_initial_values if reason == :initialize || reason == :refresh
222   super
223 end
change_column_value(column, value) click to toggle source

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.

Calls superclass method
    # File lib/sequel/plugins/dirty.rb
228 def change_column_value(column, value)
229   if (iv = initial_values).has_key?(column)
230     initial = iv[column]
231     super
232     if value == initial
233       _changed_columns.delete(column) unless missing_initial_values.include?(column)
234       iv.delete(column)
235     end
236   else
237     check_missing_initial_value(column)
238     iv[column] = get_column_value(column)
239     super
240   end
241 end
check_missing_initial_value(column) click to toggle source

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
246 def check_missing_initial_value(column)
247   unless values.has_key?(column) || (miv = missing_initial_values).include?(column)
248     miv << column
249   end
250 end
initialize_copy(other) click to toggle source

Duplicate internal data structures

Calls superclass method
    # File lib/sequel/plugins/dirty.rb
253 def initialize_copy(other)
254   super
255   @initial_values = Hash[other.initial_values]
256   @missing_initial_values = other.send(:missing_initial_values).dup
257   @previous_changes = Hash[other.previous_changes] if other.previous_changes
258   self
259 end
missing_initial_values() click to toggle source

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
264 def missing_initial_values
265   @missing_initial_values ||= []
266 end
reset_initial_values() click to toggle source

Clear the data structures that store the initial values.

    # File lib/sequel/plugins/dirty.rb
269 def reset_initial_values
270   @initial_values.clear if @initial_values
271   @missing_initial_values.clear if @missing_initial_values
272 end