Class: Safebox::Hash

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/safebox/hash.rb

Constant Summary collapse

Undefined =
BasicObject.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(password, encrypted_hash: {}) ⇒ Hash

Returns a new instance of Hash

Parameters:

  • password (String)
  • [Hash] (Hash)

    a customizable set of options


12
13
14
15
# File 'lib/safebox/hash.rb', line 12

def initialize(password, encrypted_hash: {})
  @password = password
  @encrypted_hash = encrypted_hash
end

Instance Attribute Details

#encrypted_hashObject (readonly)

Returns the value of attribute encrypted_hash


17
18
19
# File 'lib/safebox/hash.rb', line 17

def encrypted_hash
  @encrypted_hash
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if this hash is the same as another hash.

Returns:

  • (Boolean)

    true if this hash is the same as another hash.


90
91
92
# File 'lib/safebox/hash.rb', line 90

def ==(other)
  other.is_a?(Safebox::Hash) && other.password == password && other.to_h == to_h
end

#[](key) ⇒ Object

Returns decrypted value

Parameters:

  • key

Returns:

  • decrypted value


49
50
51
# File 'lib/safebox/hash.rb', line 49

def [](key)
  fetch(key, nil)
end

#[]=(key, value) ⇒ Object

Encrypts and stores a value on the given key.

Parameters:

  • key
  • value

57
58
59
# File 'lib/safebox/hash.rb', line 57

def []=(key, value)
  @encrypted_hash[key] = encrypt(value)
end

#each {|key, value| ... } ⇒ Object

Iterate through the entire store

Yields:

  • (key, value)

    yield each key and its decrypted value.


76
77
78
79
80
81
82
# File 'lib/safebox/hash.rb', line 76

def each
  unless block_given?
    enum_for(__method__)
  else
    keys.each { |key| yield key, fetch(key) }
  end
end

#fetch(key, default = Undefined) ⇒ Object

Behaves just as Hash#fetch, but values retrieved are decrypted.

Parameters:

  • key

Returns:

  • decrypted value


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/safebox/hash.rb', line 31

def fetch(key, default = Undefined)
  default_value = ! default.equal?(Undefined)

  if default_value && block_given?
    raise ArgumentError, "give default value or default block, not both"
  elsif has_key?(key)
    decrypt(@encrypted_hash[key])
  elsif default_value
    default
  elsif block_given?
    yield key
  else
    raise KeyError, "key not found: #{key.inspect}"
  end
end

#hashInteger

Return the hash of the Hash.

Returns:

  • (Integer)

    return the hash of the Hash.


85
86
87
# File 'lib/safebox/hash.rb', line 85

def hash
  [@encrypted_hash, @password].hash
end

#to_hHash

Returns a copy of the decrypted hash.

Returns:

  • (Hash)

    a copy of the decrypted hash.


20
21
22
# File 'lib/safebox/hash.rb', line 20

def to_h
  reduce({}) { |hash, (key, value)| hash.update(key => value) }
end

#update(other_hash) {|key, old_value, new_value| ... } ⇒ Safebox::Hash

Updates in place with another hash as source, same as Hash#update.

Yields:

  • (key, old_value, new_value)

    yields if block is given and there is a conflict, the return value is used as the new value.

Returns:


65
66
67
68
69
70
71
# File 'lib/safebox/hash.rb', line 65

def update(other_hash)
  other_hash.each do |key, value|
    value = yield key, self[key], value if block_given? && has_key?(key)
    self[key] = value
  end
  self
end