Class: Diffit::Changes

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/diffit/changes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timestamp) ⇒ Diffit::Changes

Instantiates a Diffit::Changes with provided timestamp.

Parameters:

  • timestamp (Time, DateTime, Date, Fixnum)

    date, time or timestamp.



13
14
15
16
# File 'lib/diffit/changes.rb', line 13

def initialize(timestamp)
  @timestamp = timestamp
  @records = []
end

Instance Attribute Details

#recordsObject (readonly)

Returns the value of attribute records.



7
8
9
# File 'lib/diffit/changes.rb', line 7

def records
  @records
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



6
7
8
# File 'lib/diffit/changes.rb', line 6

def timestamp
  @timestamp
end

Instance Method Details

#append(model, data) ⇒ self

Appends provided data to self.

Parameters:

  • model (String)

    model name.

  • data (Array(Hash))

    data to append.

Returns:

  • (self)

    self



23
24
25
26
27
28
29
# 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!Object



92
93
94
95
# File 'lib/diffit/changes.rb', line 92

def cleanup!
  @records.clear
  @length = nil
end

#eachEnumerator, Array(Diffit::Record)

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

Returns:

  • (Enumerator)

    if no block is given.

  • (Array(Diffit::Record))

    otherwise



49
50
51
52
53
54
55
# 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?Boolean

Are there any changes?

Returns:

  • (Boolean)

    existence of changes.



34
35
36
# File 'lib/diffit/changes.rb', line 34

def empty?
  @records.empty?
end

#lengthFixnum

Number of changes.

Returns:

  • (Fixnum)

    number of changes



41
42
43
# File 'lib/diffit/changes.rb', line 41

def length
  @length ||= @records.inject(0) { |v,r| v += r.changes.length }
end

#prepare!Object



86
87
88
89
90
# 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_hHash Also known as: to_hash

A Hash representation of self.

Returns:

  • (Hash)

    the object converted to hash.



73
74
75
# File 'lib/diffit/changes.rb', line 73

def to_h
  {timestamp: timestamp.to_i, changes: @records.map(&:to_h)}
end

#to_jsonString

A JSON representation of self.

Returns:

  • (String)

    the object converted to JSON.



82
83
84
# File 'lib/diffit/changes.rb', line 82

def to_json
  to_h.to_json
end

#to_sString Also known as: to_str

A short String representation of self.

Returns:

  • (String)

    the object converted to string.



60
61
62
63
64
65
66
# 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