Class: MassEncryption::Batch
- Inherits:
-
Object
- Object
- MassEncryption::Batch
- Defined in:
- lib/mass_encryption/batch.rb
Constant Summary collapse
- DEFAULT_BATCH_SIZE =
1000
Instance Attribute Summary collapse
-
#from_id ⇒ Object
readonly
Returns the value of attribute from_id.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#track ⇒ Object
readonly
Returns the value of attribute track.
-
#tracks_count ⇒ Object
readonly
Returns the value of attribute tracks_count.
Instance Method Summary collapse
- #encrypt_later(auto_enqueue_next: false) ⇒ Object
- #encrypt_now ⇒ Object
-
#initialize(klass:, from_id:, size: DEFAULT_BATCH_SIZE, track: 0, tracks_count: 1) ⇒ Batch
constructor
A new instance of Batch.
- #klass ⇒ Object
- #next ⇒ Object
- #present? ⇒ Boolean
- #records ⇒ Object
- #to_s ⇒ Object
- #validate_encrypting_is_allowed ⇒ Object
Constructor Details
#initialize(klass:, from_id:, size: DEFAULT_BATCH_SIZE, track: 0, tracks_count: 1) ⇒ Batch
Returns a new instance of Batch.
8 9 10 11 12 13 14 |
# File 'lib/mass_encryption/batch.rb', line 8 def initialize(klass:, from_id:, size: DEFAULT_BATCH_SIZE, track: 0, tracks_count: 1) @class_name = klass.name # not storing class as instance variable as it causes stack overflow error with json serialization @from_id = from_id @size = size @track = track @tracks_count = tracks_count end |
Instance Attribute Details
#from_id ⇒ Object (readonly)
Returns the value of attribute from_id.
2 3 4 |
# File 'lib/mass_encryption/batch.rb', line 2 def from_id @from_id end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
2 3 4 |
# File 'lib/mass_encryption/batch.rb', line 2 def size @size end |
#track ⇒ Object (readonly)
Returns the value of attribute track.
2 3 4 |
# File 'lib/mass_encryption/batch.rb', line 2 def track @track end |
#tracks_count ⇒ Object (readonly)
Returns the value of attribute tracks_count.
2 3 4 |
# File 'lib/mass_encryption/batch.rb', line 2 def tracks_count @tracks_count end |
Instance Method Details
#encrypt_later(auto_enqueue_next: false) ⇒ Object
31 32 33 |
# File 'lib/mass_encryption/batch.rb', line 31 def encrypt_later(auto_enqueue_next: false) MassEncryption::BatchEncryptionJob.perform_later(self, auto_enqueue_next: auto_enqueue_next) end |
#encrypt_now ⇒ Object
20 21 22 23 24 25 |
# File 'lib/mass_encryption/batch.rb', line 20 def encrypt_now if klass.encrypted_attributes.present? && present? validate_encrypting_is_allowed encrypt_records end end |
#klass ⇒ Object
16 17 18 |
# File 'lib/mass_encryption/batch.rb', line 16 def klass @class_name.constantize end |
#next ⇒ Object
41 42 43 |
# File 'lib/mass_encryption/batch.rb', line 41 def next self.class.new(klass: klass, from_id: next_track_records.last.id + 1, size: size, track: track, tracks_count: tracks_count) end |
#present? ⇒ Boolean
35 36 37 38 39 |
# File 'lib/mass_encryption/batch.rb', line 35 def present? # we deliberately load the association to avoid 2 queries when checking if there are records # before encrypting in +MassEncryption::BatchEncryptionJob+ records.present? end |
#records ⇒ Object
45 46 47 |
# File 'lib/mass_encryption/batch.rb', line 45 def records @records ||= klass.where("id >= ?", determine_from_id).order(id: :asc).limit(size) end |
#to_s ⇒ Object
49 50 51 |
# File 'lib/mass_encryption/batch.rb', line 49 def to_s "<#{klass}> from: #{from_id} size: #{size} (track=#{track}, tracks_count=#{tracks_count}) | #{records.first.id} - #{records.last.id}" end |
#validate_encrypting_is_allowed ⇒ Object
27 28 29 |
# File 'lib/mass_encryption/batch.rb', line 27 def validate_encrypting_is_allowed raise ActiveRecord::Encryption::Errors::Configuration, "can't mass encrypt while in protected mode" if ActiveRecord::Encryption.context.frozen_encryption? end |