Class: Distyll

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

Class Method Summary collapse

Class Method Details

.run(base_models, created_since) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/distyll.rb', line 3

def self.run(base_models, created_since)
  @model_profiles = Hash.new

  base_models.each do |model|
    if @model_profiles[model].nil?
      base_profile = DistyllModelProfile.new(model, true)
      base_profile.load_ids_by_timestamp(created_since)
      @model_profiles[model] = base_profile
    end
  end

  prior_count = -1
  while prior_count != current_count
    prior_count = current_count
    @model_profiles.each_value &:demote_current_ids

    # .dup is necessary here because of Ruby 1.9's "RuntimeError: can't add a new
    #   key into hash during iteration" encountered in self.find_or_store_profile.
    @model_profiles.dup.each_value do |profile|
      unless profile.prior_ids.blank?
        profile.associations.each do |a|
          # We DO want to make the associated profile continue to traverse has_manies if
          #   (a) The current profile traverses has_manies AND
          #   (b) The association we're about to traverse is a has_many.
          contagious_has_many = profile.include_has_many && !(a.belongs_to? || a.has_and_belongs_to_many?)

          find_or_store_profile(a.klass, contagious_has_many).load_ids(profile.get_new_associated_ids(a))
        end
      end
    end
  end

  @model_profiles.each_value do |profile|
    profile.copy_records
  end
end