Class: DistyllModelProfile

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(m, include_h_m = false) ⇒ DistyllModelProfile

Returns a new instance of DistyllModelProfile.



58
59
60
61
62
63
64
65
66
# 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

Instance Attribute Details

#all_idsObject (readonly)

Returns the value of attribute all_ids.



56
57
58
# File 'lib/distyll.rb', line 56

def all_ids
  @all_ids
end

#associationsObject (readonly)

Returns the value of attribute associations.



56
57
58
# File 'lib/distyll.rb', line 56

def associations
  @associations
end

#current_idsObject (readonly)

Returns the value of attribute current_ids.



56
57
58
# File 'lib/distyll.rb', line 56

def current_ids
  @current_ids
end

#include_has_manyObject (readonly)

Returns the value of attribute include_has_many.



56
57
58
# File 'lib/distyll.rb', line 56

def include_has_many
  @include_has_many
end

#modelObject (readonly)

Returns the value of attribute model.



56
57
58
# File 'lib/distyll.rb', line 56

def model
  @model
end

#prior_idsObject (readonly)

Returns the value of attribute prior_ids.



56
57
58
# File 'lib/distyll.rb', line 56

def prior_ids
  @prior_ids
end

#record_countObject (readonly)

Returns the value of attribute record_count.



56
57
58
# File 'lib/distyll.rb', line 56

def record_count
  @record_count
end

Instance Method Details

#copy_recordsObject



97
98
99
100
101
102
103
104
105
106
107
# 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_idsObject



68
69
70
71
# File 'lib/distyll.rb', line 68

def demote_current_ids
  @prior_ids = @current_ids
  @current_ids = Set.new
end

#get_id_countObject



84
85
86
# File 'lib/distyll.rb', line 84

def get_id_count
  @all_ids.count
end

#get_new_associated_ids(a) ⇒ Object



88
89
90
91
92
93
94
95
# 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) ⇒ Object



79
80
81
82
# File 'lib/distyll.rb', line 79

def load_ids(ids)
  @current_ids.merge(ids)
  @all_ids.merge(ids)
end

#load_ids_by_timestamp(timestamp) ⇒ Object



73
74
75
76
77
# 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