| Module | Sequel::Plugins::Dirty::InstanceMethods |
| In: |
lib/sequel/plugins/dirty.rb
|
| 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. |
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, line 62
62: def column_change(column)
63: [initial_value(column), send(column)] if column_changed?(column)
64: 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, line 84
84: def column_changed?(column)
85: initial_values.has_key?(column)
86: 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, line 70
70: def column_changes
71: h = {}
72: initial_values.each do |column, value|
73: h[column] = [value, send(column)]
74: end
75: h
76: end
Freeze internal data structures
# File lib/sequel/plugins/dirty.rb, line 105
105: def freeze
106: initial_values.freeze
107: missing_initial_values.freeze
108: @previous_changes.freeze if @previous_changes
109: super
110: 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, line 93
93: def initial_value(column)
94: initial_values.fetch(column){send(column)}
95: end
A hash with column symbol keys and initial values.
initial_values # {:name => 'Initial'}
# File lib/sequel/plugins/dirty.rb, line 100
100: def initial_values
101: @initial_values ||= {}
102: 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, line 117
117: def reset_column(column)
118: if initial_values.has_key?(column)
119: send("#{column}=""#{column}=", initial_values[column])
120: end
121: if missing_initial_values.include?(column)
122: values.delete(column)
123: end
124: end
Reset the initial values when setting values.
# File lib/sequel/plugins/dirty.rb, line 127
127: def set_values(hash)
128: reset_initial_values
129: super
130: 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, line 138
138: def will_change_column(column)
139: changed_columns << column unless changed_columns.include?(column)
140: check_missing_initial_value(column)
141:
142: value = if initial_values.has_key?(column)
143: initial_values[column]
144: else
145: send(column)
146: end
147:
148: initial_values[column] = if value && value != true && value.respond_to?(:clone)
149: begin
150: value.clone
151: rescue TypeError
152: value
153: end
154: else
155: value
156: end
157: end