Class: Diffit::Tracker
- Inherits:
-
Object
- Object
- Diffit::Tracker
- Defined in:
- lib/diffit/tracker.rb
Instance Attribute Summary collapse
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
Instance Method Summary collapse
-
#all ⇒ Diffit::Tracker
Appends all changes.
-
#all! ⇒ self
Appends all changes to ‘self`.
-
#append(*objects) ⇒ Diffit::Tracker
Appends provided objects.
-
#append!(*objects) ⇒ self
Appends provided objects to ‘self`.
- #changes ⇒ Object
-
#initialize(timestamp) ⇒ Diffit::Tracker
constructor
Instantiates a Diffit::Tracker with provided timestamp.
- #initialize_clone(other) ⇒ Object
Constructor Details
#initialize(timestamp) ⇒ Diffit::Tracker
Instantiates a Diffit::Tracker with provided timestamp.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/diffit/tracker.rb', line 12 def initialize() if .respond_to?(:to_datetime) @timestamp = .to_datetime elsif .respond_to?(:to_i) @timestamp = Time.at(.to_i).to_datetime else raise ArgumentError, "#{.inspect} is not a timestamp!" end @tracked = [] @changes = Diffit::Changes.new(self.) @fetched = false end |
Instance Attribute Details
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
4 5 6 |
# File 'lib/diffit/tracker.rb', line 4 def @timestamp end |
Instance Method Details
#all ⇒ Diffit::Tracker
Appends all changes.
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`.
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.
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`.
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 |
#changes ⇒ Object
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 |