Module: Sequel::Plugins::Dirty::InstanceMethods
- Defined in:
- lib/sequel/plugins/dirty.rb
Instance Attribute Summary collapse
- 
  
    
      #previous_changes  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    A hash of previous changes before the object was saved, in the same format as #column_changes. 
Instance Method Summary collapse
- 
  
    
      #after_save  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Reset the initial values after saving. 
- 
  
    
      #after_update  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Save the current changes so they are available after updating. 
- 
  
    
      #column_change(column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    An array with the initial value and the current value of the column, if the column has been changed. 
- 
  
    
      #column_changed?(column)  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    Either true or false depending on whether the column has changed. 
- 
  
    
      #column_changes  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    A hash with column symbol keys and pairs of initial and current values for all changed columns. 
- 
  
    
      #column_previously_changed?(column, opts = OPTS)  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    Whether the column was previously changed. 
- 
  
    
      #column_previously_was(column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    The previous value of the column, which is the initial value of the column before the object was previously saved. 
- 
  
    
      #freeze  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Freeze internal data structures. 
- 
  
    
      #initial_value(column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    The initial value of the given column. 
- 
  
    
      #initial_values  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    A hash with column symbol keys and initial values. 
- 
  
    
      #reset_column(column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Reset the column to its initial value. 
- 
  
    
      #will_change_column(column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Manually specify that a column will change. 
Instance Attribute Details
#previous_changes ⇒ Object (readonly)
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.
| 71 72 73 | # File 'lib/sequel/plugins/dirty.rb', line 71 def previous_changes @previous_changes end | 
Instance Method Details
#after_save ⇒ Object
Reset the initial values after saving.
| 74 75 76 77 | # File 'lib/sequel/plugins/dirty.rb', line 74 def after_save super reset_initial_values end | 
#after_update ⇒ Object
Save the current changes so they are available after updating. This happens before after_save resets them.
| 81 82 83 84 | # File 'lib/sequel/plugins/dirty.rb', line 81 def after_update super @previous_changes = column_changes end | 
#column_change(column) ⇒ Object
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']
| 91 92 93 | # File 'lib/sequel/plugins/dirty.rb', line 91 def column_change(column) [initial_value(column), get_column_value(column)] if column_changed?(column) end | 
#column_changed?(column) ⇒ Boolean
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
| 113 114 115 | # File 'lib/sequel/plugins/dirty.rb', line 113 def column_changed?(column) initial_values.has_key?(column) end | 
#column_changes ⇒ Object
A hash with column symbol keys and pairs of initial and current values for all changed columns.
column_changes # => {:name => ['Initial', 'Current']}
| 99 100 101 102 103 104 105 | # File 'lib/sequel/plugins/dirty.rb', line 99 def column_changes h = {} initial_values.each do |column, value| h[column] = [value, get_column_value(column)] end h end | 
#column_previously_changed?(column, opts = OPTS) ⇒ Boolean
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
| 128 129 130 131 132 133 134 135 136 137 138 139 140 | # File 'lib/sequel/plugins/dirty.rb', line 128 def column_previously_changed?(column, opts=OPTS) return false unless (pc = @previous_changes) && (val = pc[column]) if opts.has_key?(:from) return false unless val[0] == opts[:from] end if opts.has_key?(:to) return false unless val[1] == opts[:to] end true end | 
#column_previously_was(column) ⇒ Object
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'
| 148 149 150 | # File 'lib/sequel/plugins/dirty.rb', line 148 def column_previously_was(column) (pc = @previous_changes) && (val = pc[column]) && val[0] end | 
#freeze ⇒ Object
Freeze internal data structures
| 153 154 155 156 157 158 | # File 'lib/sequel/plugins/dirty.rb', line 153 def freeze initial_values.freeze missing_initial_values.freeze @previous_changes.freeze if @previous_changes super end | 
#initial_value(column) ⇒ Object
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'
| 165 166 167 | # File 'lib/sequel/plugins/dirty.rb', line 165 def initial_value(column) initial_values.fetch(column){get_column_value(column)} end | 
#initial_values ⇒ Object
A hash with column symbol keys and initial values.
initial_values # {:name => 'Initial'}
| 172 173 174 | # File 'lib/sequel/plugins/dirty.rb', line 172 def initial_values @initial_values ||= {} end | 
#reset_column(column) ⇒ Object
Reset the column to its initial value. If the column was not set initial, removes it from the values.
reset_column(:name)
name # => 'Initial'
| 181 182 183 184 185 186 187 188 | # File 'lib/sequel/plugins/dirty.rb', line 181 def reset_column(column) if initial_values.has_key?(column) set_column_value(:"#{column}=", initial_values[column]) end if missing_initial_values.include?(column) values.delete(column) end end | 
#will_change_column(column) ⇒ Object
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']
| 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | # File 'lib/sequel/plugins/dirty.rb', line 196 def will_change_column(column) _add_changed_column(column) check_missing_initial_value(column) value = if initial_values.has_key?(column) initial_values[column] else get_column_value(column) end initial_values[column] = if value && value != true && value.respond_to?(:clone) begin value.clone rescue TypeError value end else value end end |