class AttrMasker::Performer::Base

Public Instance Methods

mask() click to toggle source
# File lib/attr_masker/performer.rb, line 7
def mask
  # Do not want production environment to be masked!
  #
  if Rails.env.production?
    unless ENV["FORCE_MASK"]
      msg = "Attempted to run in production environment."
      raise AttrMasker::Error, msg
    end
  end

  all_models.each do |klass|
    next if klass.masker_attributes.empty?

    mask_class(klass)
  end
end

Private Instance Methods

mask_class(klass) click to toggle source

Mask all objects of a class in batches to not run out of memory!

# File lib/attr_masker/performer.rb, line 29
def mask_class(klass)
  progressbar_for_model(klass) do |bar|
    if klass.all.unscoped.respond_to?(:find_each)
      klass.all.unscoped.find_each(batch_size: 1000) do |model|
        mask_object model
        bar.increment
      end
    else
      klass.all.unscoped.each do |model|
        mask_object model
        bar.increment
      end
    end
  end
end
mask_object(instance) click to toggle source

For each masker attribute, mask it, and save it!

# File lib/attr_masker/performer.rb, line 48
def mask_object(instance)
  klass = instance.class

  updates = klass.masker_attributes.values.reduce({}) do |acc, attribute|
    next acc unless attribute.should_mask?(instance)

    attribute.mask(instance)
    acc.merge! attribute.masked_attributes_new_values(instance)
  end

  make_update instance, updates unless updates.empty?
end
progressbar_for_model(klass) { |bar| ... } click to toggle source
# File lib/attr_masker/performer.rb, line 61
def progressbar_for_model(klass)
  bar = ProgressBar.create(
    title: klass.name,
    total: klass.unscoped.count,
    throttle_rate: 0.1,
    format: "%t %c/%C (%j%%) %B %E",
  )

  yield bar
ensure
  bar.finish
end