class KeyValueDB

Public Class Methods

new(file_path, max_count=999999) click to toggle source
Calls superclass method JsonDB::new
# File lib/key_value_db.rb, line 5
def initialize(file_path, max_count=999999)
    super(file_path)
    @max_count = max_count
end

Public Instance Methods

add(the_data) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/key_value_db.rb, line 9
def set_max_count(max_count)
    @max_count = max_count
end