Class: AttrMasker::Maskers::Replacing

Inherits:
Object
  • Object
show all
Defined in:
lib/attr_masker/maskers/replacing.rb

Overview

This default masker simply replaces any value with a fixed string.

opts is a Hash with the key :value that gives you the current attribute value.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(replacement: "*", alphanum_only: false) ⇒ Replacing

Returns a new instance of Replacing.



13
14
15
16
17
# File 'lib/attr_masker/maskers/replacing.rb', line 13

def initialize(replacement: "*", alphanum_only: false)
  replacement = "" if replacement.nil?
  @replacement = replacement
  @alphanum_only = alphanum_only
end

Instance Attribute Details

#alphanum_onlyObject (readonly)

Returns the value of attribute alphanum_only.



11
12
13
# File 'lib/attr_masker/maskers/replacing.rb', line 11

def alphanum_only
  @alphanum_only
end

#replacementObject (readonly)

Returns the value of attribute replacement.



11
12
13
# File 'lib/attr_masker/maskers/replacing.rb', line 11

def replacement
  @replacement
end

Instance Method Details

#call(value:, **_opts) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/attr_masker/maskers/replacing.rb', line 19

def call(value:, **_opts)
  return value unless value.is_a? String

  if alphanum_only
    value.gsub(/[[:alnum:]]/, replacement)
  else
    replacement * value.size
  end
end