Class: DistyllModelProfile

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(m) ⇒ DistyllModelProfile

Returns a new instance of DistyllModelProfile.



63
64
65
66
67
68
69
70
# File 'lib/distyll.rb', line 63

def initialize(m)
  @model = m
  @record_count = m.count
  @all_ids = Array.new
  @last_ids = Array.new
  @new_ids = Array.new
  set_associations
end

Instance Attribute Details

#all_idsObject (readonly)

Returns the value of attribute all_ids.



61
62
63
# File 'lib/distyll.rb', line 61

def all_ids
  @all_ids
end

#associationsObject (readonly)

Returns the value of attribute associations.



61
62
63
# File 'lib/distyll.rb', line 61

def associations
  @associations
end

#last_idsObject (readonly)

Returns the value of attribute last_ids.



61
62
63
# File 'lib/distyll.rb', line 61

def last_ids
  @last_ids
end

#modelObject (readonly)

Returns the value of attribute model.



61
62
63
# File 'lib/distyll.rb', line 61

def model
  @model
end

#new_idsObject (readonly)

Returns the value of attribute new_ids.



61
62
63
# File 'lib/distyll.rb', line 61

def new_ids
  @new_ids
end

#record_countObject (readonly)

Returns the value of attribute record_count.



61
62
63
# File 'lib/distyll.rb', line 61

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).load

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

  records
end

#demote_new_idsObject



72
73
74
75
# File 'lib/distyll.rb', line 72

def demote_new_ids
  @last_ids = @new_ids
  @new_ids = Array.new
end

#get_id_countObject



88
89
90
91
# File 'lib/distyll.rb', line 88

def get_id_count
  @all_ids = @all_ids.uniq || []
  @all_ids.count
end

#get_new_associated_ids(a) ⇒ Object



93
94
95
# File 'lib/distyll.rb', line 93

def get_new_associated_ids(a)
  model.where(id: last_ids).select(a.foreign_key).map { |r| r.send(a.foreign_key) }
end

#load_ids(ids) ⇒ Object



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

def load_ids(ids)
  @new_ids += ids
  @all_ids += ids
end

#load_ids_by_timestamp(timestamp) ⇒ Object



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

def load_ids_by_timestamp(timestamp)
  ids = model.where("created_at >= ?", timestamp).select(:id).map &:id
  @new_ids += ids
  @all_ids += ids
end