class Doppelserver::Data

A glorified hash.

Public Class Methods

new() click to toggle source

@data will be COLLECTION : { 0: { HASH }, 1: { HASH }, … }

with the number autoincrementing by default

@next_keys is the hash of COLLECTION names and the next auto-

incrementing value

And now it's all empty.

# File lib/doppelserver/data.rb, line 14
def initialize
  @data = {}
  @next_keys = {}
end

Public Instance Methods

add(collection, data) click to toggle source

Adds the collection if that name (1st param) doesn't exist. Adds data (2nd param) to the collection regardless.

# File lib/doppelserver/data.rb, line 73
def add(collection, data)
  unless collection?(collection)
    @data[collection] = {}
    @next_keys[collection] = 0
  end
  next_key = @next_keys[collection]
  @data[collection][next_key.to_s] = data
  @next_keys[collection] += 1
  next_key.to_s
end
clear() click to toggle source

Clears all internal data.

# File lib/doppelserver/data.rb, line 22
def clear
  @data = {}
  @next_keys = {}
end
collection?(name) click to toggle source

Returns the collection called parameter name.

# File lib/doppelserver/data.rb, line 30
def collection?(name)
  @data.key?(name)
end
collection_key?(collection, key) click to toggle source

Returns boolean, true if collection 1st param) exists and has key (2nd param).

# File lib/doppelserver/data.rb, line 38
def collection_key?(collection, key)
  collection?(collection) && @data[collection].key?(key)
end
delete(collection, key) click to toggle source

Deletes the data at key (2nd param) from collection (1st param).

# File lib/doppelserver/data.rb, line 100
def delete(collection, key)
  @data[collection].delete(key)
end
export()
Alias for: hash
get_collection(name) click to toggle source

Returns collection named parameter name.

# File lib/doppelserver/data.rb, line 57
def get_collection(name)
  @data[name]
end
get_value(collection, key) click to toggle source

Returns the value from the collection at first parameter at key of the second parameter.

# File lib/doppelserver/data.rb, line 65
def get_value(collection, key)
  @data[collection][key]
end
hash() click to toggle source
# File lib/doppelserver/data.rb, line 42
def hash
  { 'data' => @data,
    'next_keys' => @next_keys }
end
Also aliased as: export
hash=(hash) click to toggle source
# File lib/doppelserver/data.rb, line 48
def hash=(hash)
  @data = hash['data']
  @next_keys = hash['next_keys']
end
Also aliased as: import
import(hash)
Alias for: hash=
update?(collection, id, data) click to toggle source

Updates collection to new data.

# File lib/doppelserver/data.rb, line 87
def update?(collection, id, data)
  data.each_key do |key|
    return false unless @data[collection][id].key?(key)
  end
  data.each do |key, value|
    @data[collection][id][key] = value
  end
  true
end