class Diffit::Changes

Attributes

records[R]
timestamp[R]

Public Class Methods

new(timestamp) click to toggle source

Instantiates a Diffit::Changes with provided timestamp.

@param timestamp [Time, DateTime, Date, Fixnum] date, time or timestamp. @return [Diffit::Changes] Diffit::Changes

# File lib/diffit/changes.rb, line 13
def initialize(timestamp)
  @timestamp = timestamp
  @records = []
end

Public Instance Methods

append(model, data) click to toggle source

Appends provided data to ‘self`.

@param model [String] model name. @param data [Array(Hash)] data to append. @return [self] self

# File lib/diffit/changes.rb, line 23
def append(model, data)
  data.group_by { |row| row[:record_id] }.each do |record_id, changes|
    @records << Record.new(model, record_id, changes.map { |c| c.slice(:column_name, :value, :changed_at)})
  end
  @length = nil
  self
end
cleanup!() click to toggle source
# File lib/diffit/changes.rb, line 92
def cleanup!
  @records.clear
  @length = nil
end
each() { |c| ... } click to toggle source

Calls the given block once for each record in the collection.

@return [Enumerator] if no block is given. @return [Array(Diffit::Record)] otherwise

# File lib/diffit/changes.rb, line 49
def each
  if block_given?
    @records.each { |c| yield c }
  else
    @records.enum_for(:each)
  end
end
empty?() click to toggle source

Are there any changes?

@return [Boolean] existence of changes.

# File lib/diffit/changes.rb, line 34
def empty?
  @records.empty?
end
length() click to toggle source

Number of changes.

@return [Fixnum] number of changes

# File lib/diffit/changes.rb, line 41
def length
  @length ||= @records.inject(0) { |v,r| v += r.changes.length }
end
prepare!() click to toggle source
# File lib/diffit/changes.rb, line 86
def prepare!
  @records.sort_by! { |record| record.last_changed_at }
  @records.uniq! { |record| [record.model, record.record_id] }
  nil
end
to_h() click to toggle source

A ‘Hash` representation of `self`.

@return [Hash] the object converted to hash.

# File lib/diffit/changes.rb, line 73
def to_h
  {timestamp: timestamp.to_i, changes: @records.map(&:to_h)}
end
Also aliased as: to_hash
to_hash()
Alias for: to_h
to_json() click to toggle source

A JSON representation of ‘self`.

@return [String] the object converted to JSON.

# File lib/diffit/changes.rb, line 82
def to_json
  to_h.to_json
end
to_s() click to toggle source

A short ‘String` representation of `self`.

@return [String] the object converted to string.

# File lib/diffit/changes.rb, line 60
def to_s
  sprintf '#<%s:%#0x @timestamp: %s @changes: {%d}>',
    self.class.to_s,
    self.object_id,
    @timestamp.strftime('%d/%b/%Y:%H:%M:%S %z'),
    length
end
Also aliased as: to_str
to_str()
Alias for: to_s