Class: KeyValueDB
Instance Method Summary
collapse
Methods inherited from JsonDB
#get_data, load, #reload, #save, #set_data
Constructor Details
#initialize(file_path, max_count = 999999) ⇒ KeyValueDB
Returns a new instance of KeyValueDB.
5
6
7
8
|
# File 'lib/key_value_db.rb', line 5
def initialize(file_path, max_count=999999)
super(file_path)
@max_count = max_count
end
|
Instance Method Details
#add(the_data) ⇒ Object
12
13
14
15
16
17
18
19
|
# File 'lib/key_value_db.rb', line 12
def add(the_data)
if @data_hash_arr.size >= @max_count
@data_hash_arr.shift
end
new_record = the_data
@data_hash_arr << new_record
new_record
end
|
#add_or_update_by_key(the_data, the_key) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/key_value_db.rb', line 20
def add_or_update_by_key(the_data, the_key)
found_index = -1
@data_hash_arr.each_with_index do |cur_data, i|
if cur_data[the_key]== the_data[the_key]
found_index = i
break
end
end
if found_index > -1
@data_hash_arr[found_index] = the_data
else
add(the_data)
end
the_data
end
|
#find(the_key, the_value) ⇒ Object
35
36
37
38
39
40
41
42
|
# File 'lib/key_value_db.rb', line 35
def find(the_key, the_value)
@data_hash_arr.each do |cur_data|
if cur_data[the_key] == the_value
return cur_data
end
end
return nil
end
|
#find_all(the_key, the_value) ⇒ Object
43
44
45
46
47
|
# File 'lib/key_value_db.rb', line 43
def find_all(the_key, the_value)
@data_hash_arr.select do |cur_data|
cur_data[the_key] == the_value
end
end
|
#set_max_count(max_count) ⇒ Object
9
10
11
|
# File 'lib/key_value_db.rb', line 9
def set_max_count(max_count)
@max_count = max_count
end
|