Module: SDCMeta::DataStorage

Included in:
SDC::Data
Defined in:
lib/include/DataStorage.rb

Constant Summary collapse

SYMBOL_PREFIX =

If an actual symbol is not needed, e.g. for one-time draw routines

"_UNKNOWN_"

Instance Method Summary collapse

Instance Method Details

#clear_containersObject



33
34
35
36
37
# File 'lib/include/DataStorage.rb', line 33

def clear_containers
	@container_list.each do |container|
		container.clear
	end
end

#create_loading_method(name, obj_class, load_method) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/include/DataStorage.rb', line 39

def create_loading_method(name, obj_class, load_method)
	define_singleton_method("load_#{name}") do |index = nil, file_index: nil, filename: nil|
		if !index then
			if !file_index && !filename then
				raise ArgumentError.new("Neither file_index nor filename were given.")
			elsif !file_index then
				index = (SYMBOL_PREFIX + filename).to_sym
			elsif !filename then
				index = file_index.to_sym
			else
				index = file_index.to_sym
			end
		end
		if !@container_list[name][index] then
			if !file_index && !filename then
				raise ArgumentError.new("Neither file_index nor filename were given.")
			elsif !filename then
				filename = @filenames[file_index]
				raise RuntimeError.new("File index #{file_index} has no associated filename.")
			elsif file_index && file_name then
				add_filename(filename, index: file_index)
			end

			obj = obj_class.new
			obj.method(load_method).call(filename)
			self.singleton_method("add_#{name}".to_sym).call(obj, index: index)
		end

		return @container_list[name][index]
	end
end

#define_new_data_type(name, as_hash: true, plural: name.to_s + "s") ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/include/DataStorage.rb', line 7

def define_new_data_type(name, as_hash: true, plural: name.to_s + "s")
	@container_list = {} if !@container_list
	@plural_list = {} if !@plural_list

	instance_variable_set("@#{plural}", (as_hash ? {} : []))
	data = instance_variable_get("@#{plural}")
	define_singleton_method("add_#{name}") do |obj, index: nil|
		if !index then
			if as_hash then
				raise("No index for hash data type given") 
			else
				index = data.size
			end
		end
		data[index] = obj
		return index
	end

	define_singleton_method(plural) do
		return data
	end

	@plural_list[name] = plural
	@container_list[name] = instance_variable_get("@#{plural}")
end