Class: Kioku

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

Overview

require ‘profile’

Instance Method Summary collapse

Constructor Details

#initialize(filepath, serialization_type = 'yaml') ⇒ Kioku

opens up a database file and loads the content, or creates one first if it doesn’t exist yet



10
11
12
13
14
# File 'lib/kioku.rb', line 10

def initialize filepath, serialization_type='yaml'
	@filepath = filepath
	@serialization_type = serialization_type
	init_database
end

Instance Method Details

#[](key) ⇒ Object

a shorthand method for the get method



34
35
36
# File 'lib/kioku.rb', line 34

def [](key)
	get key
end

#[]=(key, data) ⇒ Object

a shorthand method for the set method



24
25
26
# File 'lib/kioku.rb', line 24

def []=(key, data)
	set key, data
end

#allObject

returns all keys which are actually in the database



61
62
63
# File 'lib/kioku.rb', line 61

def all
	@data_base.keys.dup || []
end

#clearObject

remove all datasets from the database



46
47
48
49
# File 'lib/kioku.rb', line 46

def clear
	@data_base = {}
	update_database
end

#delete(key) ⇒ Object

delete the key and all associated data



39
40
41
42
43
# File 'lib/kioku.rb', line 39

def delete key
	data = @data_base.delete key
	update_database
	data
end

#destroyObject

deletes the current database file and locks the database-object so it can’t be used anymore



53
54
55
56
57
58
# File 'lib/kioku.rb', line 53

def destroy
	File.delete @filepath
	@data_base = nil
	@data_base.freeze
	self.freeze
end

#get(key) ⇒ Object

return the data corresponding to the given key.



29
30
31
# File 'lib/kioku.rb', line 29

def get key
	@data_base[ key ]
end

#include?(key) ⇒ Boolean

ask the database if exactly this key is already used in it.

Returns:

  • (Boolean)


75
76
77
# File 'lib/kioku.rb', line 75

def include? key
	@data_base.has_key? key
end

#search(pattern) ⇒ Object

returns all keys in the database that matches the given pattern-string



66
67
68
69
70
71
72
# File 'lib/kioku.rb', line 66

def search pattern
	results = all.map {|key|
		key if key.to_s =~ /#{pattern}/i
	}
	results.delete nil
	results
end

#set(key, data) ⇒ Object

add any new content to the database, identified by the given key. returns false if the key is already existing.



18
19
20
21
# File 'lib/kioku.rb', line 18

def set key, data
	@data_base[ key ] = data
	update_database
end