Module: Sequel::Plugins::Dirty::InstanceMethods

Defined in:
lib/sequel/plugins/dirty.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#previous_changesObject (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.



55
56
57
# File 'lib/sequel/plugins/dirty.rb', line 55

def previous_changes
  @previous_changes
end

Instance Method Details

#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']


62
63
64
# File 'lib/sequel/plugins/dirty.rb', line 62

def column_change(column)
  [initial_value(column), send(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

Returns:

  • (Boolean)


84
85
86
# File 'lib/sequel/plugins/dirty.rb', line 84

def column_changed?(column)
  initial_values.has_key?(column)
end

#column_changesObject

A hash with column symbol keys and pairs of initial and current values for all changed columns.

column_changes # => {:name => ['Initial', 'Current']}


70
71
72
73
74
75
76
# File 'lib/sequel/plugins/dirty.rb', line 70

def column_changes
  h = {}
  initial_values.each do |column, value|
    h[column] = [value, send(column)]
  end
  h
end

#dupObject

Duplicate internal data structures



89
90
91
92
93
94
95
96
97
# File 'lib/sequel/plugins/dirty.rb', line 89

def dup 
  s = self
  super.instance_eval do
    @initial_values = s.initial_values.dup
    @missing_initial_values = s.send(:missing_initial_values).dup
    @previous_changes = s.previous_changes.dup if s.previous_changes
    self
  end
end

#freezeObject

Freeze internal data structures



100
101
102
103
104
105
# File 'lib/sequel/plugins/dirty.rb', line 100

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'


112
113
114
# File 'lib/sequel/plugins/dirty.rb', line 112

def initial_value(column)
  initial_values.fetch(column){send(column)}
end

#initial_valuesObject

A hash with column symbol keys and initial values.

initial_values # {:name => 'Initial'}


119
120
121
# File 'lib/sequel/plugins/dirty.rb', line 119

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'


128
129
130
131
132
133
134
135
# File 'lib/sequel/plugins/dirty.rb', line 128

def reset_column(column)
  if initial_values.has_key?(column)
    send(:"#{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']


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/sequel/plugins/dirty.rb', line 143

def will_change_column(column)
  changed_columns << column unless changed_columns.include?(column)
  check_missing_initial_value(column)

  value = if initial_values.has_key?(column)
    initial_values[column]
  else
    send(column)
  end

  initial_values[column] = if value && value != true && value.respond_to?(:clone)
    begin
      value.clone
    rescue TypeError
      value
    end
  else
    value
  end
end