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