Class: YasstString

Inherits:
String
  • Object
show all
Defined in:
lib/yasst/yasst_string.rb

Overview

Decorator pattern for String

TODO: only way to tell an already encrypted new YasstString object that it is encrypted is by telling it so at initialization time. Is there a more elegant way of doing this without imposing overhead?

Instance Method Summary collapse

Constructor Details

#initialize(str = '', encrypted = false) ⇒ YasstString

Returns a new instance of YasstString.



8
9
10
11
# File 'lib/yasst/yasst_string.rb', line 8

def initialize(str = '', encrypted = false)
  super(str)
  @encrypted = encrypted
end

Instance Method Details

#decrypt(provider) ⇒ Object

Decrypt self using a Yasst::Provider



30
31
32
33
34
35
# File 'lib/yasst/yasst_string.rb', line 30

def decrypt(provider)
  raise Yasst::Error::AlreadyDecrypted,
        'File is already decrypted' unless encrypted?
  @encrypted = false
  replace(provider.decrypt(to_s))
end

#encrypt(provider) ⇒ Object

Encrypt self using a Yasst::Provider



15
16
17
18
19
20
# File 'lib/yasst/yasst_string.rb', line 15

def encrypt(provider)
  raise Yasst::Error::AlreadyEncrypted,
        'String is already encrypted' if encrypted?
  @encrypted = true
  replace(provider.encrypt(to_s))
end

#encrypted?Boolean

Whether or not the encrypt method has been called

Returns:

  • (Boolean)


24
25
26
# File 'lib/yasst/yasst_string.rb', line 24

def encrypted?
  @encrypted ||= false
end