Class: ActiveRecord::Bulkoperation::Util::FlushDirtyObjects

Inherits:
TransactionObject show all
Defined in:
lib/activerecord_bulkoperation/util/flush_dirty_objects.rb

Defined Under Namespace

Classes: DirtyObjects

Instance Method Summary collapse

Methods inherited from TransactionObject

#after_commit, get

Constructor Details

#initializeFlushDirtyObjects

Returns a new instance of FlushDirtyObjects.



20
21
22
23
# File 'lib/activerecord_bulkoperation/util/flush_dirty_objects.rb', line 20

def initialize
  @scheduled_merges    = ActiveRecord::Bulkoperation::Util::EntityHash.new
  @scheduled_deletions = ActiveRecord::Bulkoperation::Util::EntityHash.new
end

Instance Method Details

#add_delete(object) ⇒ Object



65
66
67
68
# File 'lib/activerecord_bulkoperation/util/flush_dirty_objects.rb', line 65

def add_delete(object)
  fail 'nil object' unless object
  ( @scheduled_deletions[ object.class] ||= []) << object
end

#add_insert_on_missing(keys, object) ⇒ Object



55
56
57
58
# File 'lib/activerecord_bulkoperation/util/flush_dirty_objects.rb', line 55

def add_insert_on_missing(keys,object)
  fail 'nil object' unless object
  ( ( @scheduled_merges[ object.class] ||= DirtyObjects.new).insert_on_missing[keys] ||= Set.new ) << object
end

#add_merge(object) ⇒ Object



60
61
62
63
# File 'lib/activerecord_bulkoperation/util/flush_dirty_objects.rb', line 60

def add_merge(object)
  fail 'nil object' unless object
  ( @scheduled_merges[ object.class] ||= DirtyObjects.new).insert_or_update << object
end

#after_rollbackObject



37
38
39
# File 'lib/activerecord_bulkoperation/util/flush_dirty_objects.rb', line 37

def after_rollback
  close
end

#after_rollback_to_savepointObject



33
34
35
# File 'lib/activerecord_bulkoperation/util/flush_dirty_objects.rb', line 33

def after_rollback_to_savepoint
  close
end

#before_commitObject



25
26
27
# File 'lib/activerecord_bulkoperation/util/flush_dirty_objects.rb', line 25

def before_commit
  flush
end

#before_create_savepointObject



29
30
31
# File 'lib/activerecord_bulkoperation/util/flush_dirty_objects.rb', line 29

def before_create_savepoint
  flush
end

#closeObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/activerecord_bulkoperation/util/flush_dirty_objects.rb', line 41

def close

  @scheduled_merges.values.each do |objects|
    if objects
      objects.insert_or_update.each { |record| record.on_closed_scheduled_operation }
      objects.insert_on_missing.values.each { |array| array.each{ |record| record.on_closed_scheduled_operation } }
    end
  end
  @scheduled_merges.clear

  @scheduled_deletions.values.each { |array| array and array.each { |record| record.on_closed_scheduled_operation } }
  @scheduled_deletions.clear
end

#flush(args = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/activerecord_bulkoperation/util/flush_dirty_objects.rb', line 70

def flush(args = {})
  fail 'nil object' unless args

  affected = 0

  @scheduled_deletions.each_pair_in_detail_hierarchy do |k, v|
    affected += k.delete_group(v, args)
    @scheduled_deletions[k] = nil
    v.each { |record| record.on_closed_scheduled_operation }
  end

  @scheduled_merges.each_pair_in_master_hierarchy do |k, v|

    affected += k.merge_group( v.insert_or_update, args )

    v.insert_on_missing.each_pair do |keys,records|

      affected += k.insert_on_missing_group( keys, records, args )
    end

    @scheduled_merges[k] = nil
    v.insert_or_update.each  { |record| record.on_closed_scheduled_operation }
    v.insert_on_missing.values.each { |array| array.each{ |record| record.on_closed_scheduled_operation } }
  end

  affected
end

#flush_record_class(record_class, args = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/activerecord_bulkoperation/util/flush_dirty_objects.rb', line 98

def flush_record_class(record_class, args = {})
  fail 'nil object' unless record_class
  fail 'nil object' unless args

  if list = @scheduled_deletions[ record_class]
    record_class.delete_group(list, args)
    @scheduled_deletions[ record_class] = nil
    list.each { |record| record.on_closed_scheduled_operation }
  end

  if objects = @scheduled_merges[ record_class]

    record_class.merge_group( objects.insert_or_update, args )

    objects.insert_on_missing.each_pair do |keys,records|

      record_class.insert_on_missing_group( keys, records, args )
    end

    @scheduled_merges[record_class] = nil
    objects.insert_or_update.each  { |record| record.on_closed_scheduled_operation }
    objects.insert_on_missing.values.each { |array| array.each{ |record| record.on_closed_scheduled_operation } }
  end
end