Class: Stud::Secret

Inherits:
Object
  • Object
show all
Defined in:
lib/stud/secret.rb

Instance Method Summary collapse

Constructor Details

#initialize(secret_value) ⇒ Secret

Initialize a new secret with a given value.

value - anything you want to keep secret from loggers, etc.



13
14
15
16
17
18
19
20
# File 'lib/stud/secret.rb', line 13

def initialize(secret_value)
  # Redefine the 'value' method on this instance. This exposes no instance
  # variables to be accidentally leaked by things like awesome_print, etc.
  # This makes any #value call return the secret value.
  (class << self; self; end).class_eval do
    define_method(:value) { secret_value }
  end
end

Instance Method Details

#to_sObject Also known as: inspect

Emit simply “<secret>” when printed or logged.



23
24
25
# File 'lib/stud/secret.rb', line 23

def to_s
  return "<secret>"
end

#valueObject

Get the secret value.



30
31
32
33
# File 'lib/stud/secret.rb', line 30

def value
  # Nothing, this will be filled in by Secret.new
  # But we'll still document this so rdoc/yard know the method exists.
end