Module: AttrRedactor::InstanceMethods

Defined in:
lib/attr_redactor.rb

Instance Method Summary collapse

Instance Method Details

#redact(attribute, value) ⇒ Object

Redacts a value for the attribute specified using options evaluated in the current object’s scope

Example

class User
  attr_accessor :secret_key
  attr_redactor :data, key: :secret_key

  def initialize(secret_key)
    self.secret_key = secret_key
  end
end

@user = User.new('some-secret-key')
@user.redact(:data, '[email protected]')


273
274
275
276
# File 'lib/attr_redactor.rb', line 273

def redact(attribute, value)
  redacted_attributes[attribute.to_sym][:operation] = :redacting
  self.class.redact(attribute, value, evaluated_attr_redacted_options_for(attribute))
end

#redacted_attributesObject

Copies the class level hash of redacted attributes with virtual attribute names as keys and their corresponding options as values to the instance



281
282
283
# File 'lib/attr_redactor.rb', line 281

def redacted_attributes
  @redacted_attributes ||= self.class.redacted_attributes.dup
end

#unredact(attribute, redacted_value) ⇒ Object

Decrypts a value for the attribute specified using options evaluated in the current object’s scope

Example

class User
  attr_accessor :secret_key
  attr_redactor :data, key: :secret_key

  def initialize(secret_key)
    self.secret_key = secret_key
  end
end

@user = User.new('some-secret-key')
@user.unredact(:data, SOME_REDACTED_HASH)


253
254
255
256
# File 'lib/attr_redactor.rb', line 253

def unredact(attribute, redacted_value)
  redacted_attributes[attribute.to_sym][:operation] = :unredacting
  self.class.unredact(attribute, redacted_value, evaluated_attr_redacted_options_for(attribute))
end