Class: KStor::Crypto::ArmoredHash

Inherits:
ArmoredValue show all
Defined in:
lib/kstor/crypto/armored_value.rb

Overview

A Hash that can be easily serialized to ASCII chars.

Uses JSON as intermediary data format.

Direct Known Subclasses

KDFParams

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ArmoredValue

from_binary, #initialize, #to_ascii, #to_binary

Constructor Details

This class inherits a constructor from KStor::Crypto::ArmoredValue

Class Method Details

.from_hash(hash) ⇒ KStor::Crypto::ArmoredHash

Create from Ruby Hash.

Parameters:

  • hash (Hash)

    a Ruby Hash.

Returns:



52
53
54
# File 'lib/kstor/crypto/armored_value.rb', line 52

def self.from_hash(hash)
  from_binary(hash.to_json)
end

Instance Method Details

#[](key) ⇒ Any?

Access value for this key.

Parameters:

  • key (String)

    what to lookup

Returns:

  • (Any, nil)

    value



67
68
69
# File 'lib/kstor/crypto/armored_value.rb', line 67

def [](key)
  to_hash[key]
end

#[]=(key, val) ⇒ Object

Set value for a key.

Parameters:

  • key (String)

    hash key

  • val (Any)

    hash value



75
76
77
78
79
# File 'lib/kstor/crypto/armored_value.rb', line 75

def []=(key, val)
  h = to_hash
  h[key] = val
  @value = ASCIIArmor.encode(h.to_json)
end

#to_hashHash

Convert to Ruby Hash.

Returns:

  • (Hash)

    new Ruby Hash



59
60
61
# File 'lib/kstor/crypto/armored_value.rb', line 59

def to_hash
  JSON.parse(to_binary)
end