Class: DistyllModelProfile
- Inherits:
-
Object
- Object
- DistyllModelProfile
- Defined in:
- lib/distyll.rb
Instance Attribute Summary collapse
-
#all_ids ⇒ Object
readonly
Returns the value of attribute all_ids.
-
#associations ⇒ Object
readonly
Returns the value of attribute associations.
-
#current_ids ⇒ Object
readonly
Returns the value of attribute current_ids.
-
#include_has_many ⇒ Object
readonly
Returns the value of attribute include_has_many.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#prior_ids ⇒ Object
readonly
Returns the value of attribute prior_ids.
-
#record_count ⇒ Object
readonly
Returns the value of attribute record_count.
Instance Method Summary collapse
- #copy_records ⇒ Object
- #demote_current_ids ⇒ Object
- #get_id_count ⇒ Object
- #get_new_associated_ids(a) ⇒ Object
-
#initialize(m, include_h_m = false) ⇒ DistyllModelProfile
constructor
A new instance of DistyllModelProfile.
- #load_ids(ids) ⇒ Object
- #load_ids_by_timestamp(timestamp) ⇒ Object
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_ids ⇒ Object (readonly)
Returns the value of attribute all_ids.
56 57 58 |
# File 'lib/distyll.rb', line 56 def all_ids @all_ids end |
#associations ⇒ Object (readonly)
Returns the value of attribute associations.
56 57 58 |
# File 'lib/distyll.rb', line 56 def associations @associations end |
#current_ids ⇒ Object (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_many ⇒ Object (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 |
#model ⇒ Object (readonly)
Returns the value of attribute model.
56 57 58 |
# File 'lib/distyll.rb', line 56 def model @model end |
#prior_ids ⇒ Object (readonly)
Returns the value of attribute prior_ids.
56 57 58 |
# File 'lib/distyll.rb', line 56 def prior_ids @prior_ids end |
#record_count ⇒ Object (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_records ⇒ Object
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_ids ⇒ Object
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_count ⇒ Object
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 () ids = model.where("created_at >= ?", ).select(:id).map &:id @current_ids.merge(ids) @all_ids.merge(ids) end |