class DistyllModelProfile

Attributes

all_ids[R]
associations[R]
current_ids[R]
include_has_many[R]
model[R]
prior_ids[R]
record_count[R]

Public Class Methods

new(m, include_h_m = false) click to toggle source
# File lib/distyll.rb, line 58
def initialize(m, include_h_m = false)
  @model = m
  @include_has_many = include_h_m
  @record_count = m.count
  @all_ids = Set.new
  @prior_ids = Set.new
  @current_ids = Set.new
  set_associations
end

Public Instance Methods

copy_records() click to toggle source
# File lib/distyll.rb, line 97
def copy_records
  return nil if all_ids.blank?

  records = model.where(id: all_ids.to_a).load

  model.establish_connection("distyll")
  records.each { |record| model.new(record.attributes).save!(validate: false) }
  model.establish_connection(Rails.env)

  records
end
demote_current_ids() click to toggle source
# File lib/distyll.rb, line 68
def demote_current_ids
  @prior_ids = @current_ids
  @current_ids = Set.new
end
get_id_count() click to toggle source
# File lib/distyll.rb, line 84
def get_id_count
  @all_ids.count
end
get_new_associated_ids(a) click to toggle source
# File lib/distyll.rb, line 88
def get_new_associated_ids(a)
  if a.belongs_to?
    model.where(id: prior_ids.to_a).select(a.foreign_key).map { |r| r.send(a.foreign_key) }
  else
    # Polymorphism could slow us down here, causing us to pull more records than we want to.
    a.klass.where(a.foreign_key => prior_ids.to_a).select(:id).map { |r| r.send(:id) }
  end
end
load_ids(ids) click to toggle source
# File lib/distyll.rb, line 79
def load_ids(ids)
  @current_ids.merge(ids)
  @all_ids.merge(ids)
end
load_ids_by_timestamp(timestamp) click to toggle source
# File lib/distyll.rb, line 73
def load_ids_by_timestamp(timestamp)
  ids = model.where("created_at >= ?", timestamp).select(:id).map &:id
  @current_ids.merge(ids)
  @all_ids.merge(ids)
end

Private Instance Methods

set_associations() click to toggle source
# File lib/distyll.rb, line 111
def set_associations
  @associations = Array.new
  model.reflect_on_all_associations.each do |association|
    if association.through_reflection.nil?
      if association.belongs_to? || self.include_has_many
        @associations << association
      end
    end
  end
end