Class: Mousevc::Persistence

Inherits:
Object
  • Object
show all
Defined in:
lib/mousevc/persistence.rb

Overview

Note:

The Persistance class only stores data during the application life cycle. Once the application quits the data may still exist in memory if not explicitly cleared, however it should not be relied upon to be their for subsequent executions.

The Persistance class enables storage and retrieval of models. While it is intended for storing models it can store any value as internally it stores the value in a Ruby Hash.

Constant Summary collapse

@@models =

Internal data storage

{}

Class Method Summary collapse

Class Method Details

.clear(*args) ⇒ Object

Allows clearing of all or some of the models currently stored. Clears all data and models stored if no keys are provided

Parameters:

  • args (Symbol)

    a list of keys to clear from the currently stored models



23
24
25
26
27
28
29
# File 'lib/mousevc/persistence.rb', line 23

def self.clear(*args)
	if args.empty?
		@@models.clear
	else
		args.each {|arg| @@models.delete(arg)}
	end
end

.get(key) ⇒ Mousevc::Model, Any

Get a stored model by key

Parameters:

  • key (Symbol)

    the key under which the model is stored

Returns:



37
38
39
# File 'lib/mousevc/persistence.rb', line 37

def self.get(key)
	@@models[key]
end

.set(key, value) ⇒ Object

Set the value of a storage key

Parameters:

  • key (Symbol)

    the key under which the model is stored

Raises:



47
48
49
50
# File 'lib/mousevc/persistence.rb', line 47

def self.set(key, value)
	raise Error.new("Cannot persist, a value already exists at: #{key}") unless @@models[key].nil?
	@@models.store(key, value)
end