Class: Diffit::Tracker

Inherits:
Object
  • Object
show all
Defined in:
lib/diffit/tracker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timestamp) ⇒ Diffit::Tracker

Instantiates a Diffit::Tracker with provided timestamp.

Parameters:

  • timestamp (Time, DateTime, Date, Fixnum)

    date, time or timestamp.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/diffit/tracker.rb', line 12

def initialize(timestamp)
  if timestamp.respond_to?(:to_datetime)
    @timestamp = timestamp.to_datetime
  elsif timestamp.respond_to?(:to_i)
    @timestamp = Time.at(timestamp.to_i).to_datetime
  else
    raise ArgumentError, "#{timestamp.inspect} is not a timestamp!"
  end

  @tracked = []
  @changes = Diffit::Changes.new(self.timestamp)
  @fetched = false
end

Instance Attribute Details

#timestampObject (readonly)

Returns the value of attribute timestamp.



4
5
6
# File 'lib/diffit/tracker.rb', line 4

def timestamp
  @timestamp
end

Instance Method Details

#allDiffit::Tracker

Appends all changes.

Returns:



63
64
65
66
67
# File 'lib/diffit/tracker.rb', line 63

def all
  copy = self.clone
  copy.all!
  copy
end

#all!self

Appends all changes to ‘self`.

Returns:

  • (self)

    self.



72
73
74
75
76
77
78
# File 'lib/diffit/tracker.rb', line 72

def all!
  @changes.cleanup!
  handle_all.group_by { |row| row[:table_name] }.each do |t, records|
    @changes.append t.classify, records
  end
  self
end

#append(*objects) ⇒ Diffit::Tracker

Appends provided objects.

Parameters:

  • object (ActiveRecord::Relation, ActiveRecord::Base, Array(ActiveRecord::Base), Array(ActiveRecord::Relation))

Returns:



35
36
37
38
39
# File 'lib/diffit/tracker.rb', line 35

def append(*objects)
  copy = self.clone
  copy.append!(*objects)
  copy
end

#append!(*objects) ⇒ self

Appends provided objects to ‘self`.

Parameters:

  • object (ActiveRecord::Relation, ActiveRecord::Base, Array(ActiveRecord::Base), Array(ActiveRecord::Relation))

Returns:

  • (self)

    self



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/diffit/tracker.rb', line 45

def append!(*objects)
  objects.flatten!
  objects.each do |object|
    if accepts?(object)
      @tracked << object
    else
      raise ArgumentError, 'Expected ActiveRecord::Base or ActiveRecord::Relation'
    end
  end

  @changes.cleanup!
  @fetched = false
  self
end

#changesObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/diffit/tracker.rb', line 80

def changes
  return @changes if @fetched

  @tracked.each do |object|
    if record?(object)
      @changes.append object.model_name.name, handle_one(object)
    elsif relation?(object)
      model = object.respond_to?(:model) ? object.model : object.class
      changes = case Diffit.strategy
                when :join     then handle_relation_with_join(object)
                when :subquery then handle_relation_with_subquery(object)
                end

      @changes.append model.name, changes
    end
  end

  @fetched = true
  @changes.prepare!
  @changes
end

#initialize_clone(other) ⇒ Object



26
27
28
29
# File 'lib/diffit/tracker.rb', line 26

def initialize_clone(other)
  @tracked = Array.new(@tracked)
  @changes = Diffit::Changes.new(self.timestamp)
end