Module: Yast2::SecretAttributes::ClassMethods

Defined in:
library/general/src/lib/yast2/secret_attributes.rb

Overview

Class methods for the mixin

Instance Method Summary collapse

Instance Method Details

#secret_attr(name) ⇒ Object

Similar to .attr_accessor but with additional mechanisms to prevent exposing the internal value of the attribute

Examples:

class TheClass
  include Yast2::SecretAttributes

  attr_accessor :name
  secret_attr :password
end

one_object = TheClass.new
one_object.name = "Aa"
one_object.password = "42"

one_object.password # => "42"
one_object.inspect # => "#<TheClass:0x0f8 @password=<secret>, @name=\"Aa"\">"


68
69
70
71
72
73
74
75
76
77
78
# File 'library/general/src/lib/yast2/secret_attributes.rb', line 68

def secret_attr(name)
  define_method(:"#{name}") do
    attribute = instance_variable_get(:"@#{name}")
    attribute ? attribute.value : nil
  end

  define_method(:"#{name}=") do |value|
    instance_variable_set(:"@#{name}", Attribute.new(value))
    value
  end
end