class Gamefic::Vault
An array wrapper that exposes a protected interface. The array is always returned frozen. It can only be modified through add
and delete
. The vault can be “locked” to prevent existing elements from being deleted.
Public Class Methods
new()
click to toggle source
# File lib/gamefic/vault.rb, line 9 def initialize @set = Set.new @array = [] @lock_index = nil end
Public Instance Methods
add(object)
click to toggle source
@param object [Object]
# File lib/gamefic/vault.rb, line 21 def add object @array = @set.add(object).to_a object end
array()
click to toggle source
@return [Array]
# File lib/gamefic/vault.rb, line 16 def array @array.freeze end
deletable?(object)
click to toggle source
@return [Boolean] True if the object is deletable (i.e., not locked).
# File lib/gamefic/vault.rb, line 48 def deletable? object @lock_index.to_i <= @array.find_index(object).to_i end
delete(object)
click to toggle source
@param object [Object] @return [Boolean] True if object was deleted
# File lib/gamefic/vault.rb, line 28 def delete object return false unless deletable?(object) && @set.delete?(object) @array = @set.to_a.freeze true end
lock()
click to toggle source
Lock the current elements in the vault.
After the vault is locked, calling delete
on a locked element will leave the element in the array and return false. Elements added after the lock can be deleted.
# File lib/gamefic/vault.rb, line 41 def lock return @lock_index if @lock_index @lock_index = @array.length end