Module: Purrrge::Scrubbable

Extended by:
ActiveSupport::Concern
Includes:
ActiveSupport::Callbacks
Defined in:
lib/purrrge/scrubbable.rb

Overview

Scrubbable is a concern that provides functionality for data scrubbing/anonymization. It allows the implementing class to define which fields should be scrubbed and how they should be scrubbed (either by setting to a specific value or using a custom method).

Usage:

class User
include Purrrge::Scrubbable

scrub_field :email, scrub_value: nil
scrub_field :name, scrub_value: "REDACTED"
scrub_field :phone, scrub_method: :anonymize_phone

def anonymize_phone(field)
  self[field] = "xxx-xxx-xxxx"
end
end

Instance Method Summary collapse

Instance Method Details

#[](field) ⇒ Object

Override the [] method to provide access to attributes This allows the gem to be database-agnostic



105
106
107
108
109
# File 'lib/purrrge/scrubbable.rb', line 105

def [](field)
  return unless respond_to?(field)

  send(field)
end

#[]=(field, value) ⇒ Object

Override the []= method to set attributes This allows the gem to be database-agnostic



113
114
115
116
117
# File 'lib/purrrge/scrubbable.rb', line 113

def []=(field, value)
  return unless respond_to?("#{field}=")

  send("#{field}=", value)
end

#scrub! ⇒ Boolean

Performs the actual scrubbing operation on all registered fields

Returns:

  • (Boolean) —

    Returns the result of the save operation



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/purrrge/scrubbable.rb', line 84

def scrub!
  run_callbacks :scrub do
    self.class.scrubbable_fields.each do |field_config|
      if field_config[:method]
        send(field_config[:method], field_config[:field])
      else
        self[field_config[:field]] = field_config[:value]
      end
    end

    # Automatically set scrubbed_at timestamp if the column exists
    if respond_to?(:scrubbed_at=) && self.class.respond_to?(:column_names) && self.class.column_names.include?("scrubbed_at")
      self.scrubbed_at = Time.now
    end

    save(validate: false)
  end
end