module Gembase
@author NTBBloodbath
Attributes
Public Class Methods
@!method add_object
(db, category, key, value)
Create an object inside a category @param db [String] the name of your DataBase file @param category [String] the name of your category @param key [String] the key name of your object @param value [String, Integer, Boolean] the value of your key
@example Add object 'bloodbath' with value 'premium'
add_object('example', 'users', 'bloodbath', 'premium')
@deprecated Use {#create_object} instead.
# File lib/gembase.rb, line 177 def self.add_object(db, category, key, value) begin raise Errors::invalid_param('db', 'String', 'Gembase.add_object') until db.is_a?(String) rescue => e e end begin raise Errors::invalid_param('category', 'String', 'Gembase.add_object') until category.is_a?(String) rescue => e e end begin raise Errors::invalid_param('key', 'String', 'Gembase.add_object') until key.is_a?(String) rescue => e e end puts "\n>> [WARNING] | add_object Method is deprecated. Please use create_object Method instead.".colorize(:red) if @dbf == 0 @dbo @data @data[category.to_s].store(key.to_s, value) end end
@!method change_object
(db, key, new_value, category=nil, subcategory=nil)
Change the value of an existing object @param db [String] the name of your DataBase file @param key [String] the key name of your object @param new_value [String, Integer, Boolean] the new value of your key @param category [String] the name of your category @param subcategory [String] the name of your subcategory
@example Change key server value from localhost:8080 to example_host.com outside categories
change_object('example', 'server', 'example_host.com')
@example Change key users-limit value from 5 to 10 into users category
change_object('example', 'users-limit', '10', 'users')
@example Change key bloodbath value from true to false into premium subcategory
change_object('example', 'bloodbath", false, 'users', 'category')
@since 0.5.0
# File lib/gembase.rb, line 364 def self.change_object(db, key, new_value, category=nil, subcategory=nil) begin raise Errors::invalid_param('db', 'String', 'Gembase.change_object') until db.is_a?(String) rescue => e e end begin raise Errors::invalid_param('key', 'String', 'Gembase.change_object') until key.is_a?(String) rescue => e e end if @dbf == 0 if category.to_s.length.zero? @dbo @data @data[key.to_s] = new_value File.write(@dbo, @data.to_yaml) else begin raise Errors::invalid_param('category', 'String', 'Gembase.change_object') until category.is_a?(String) rescue => e e end if subcategory.to_s.length.zero? @dbo @data @data[category.to_s][key.to_s] = new_value File.write(@dbo, @data.to_yaml) else begin raise Errors::invalid_param('subcategory', 'String', 'Gembase.change_object') until subcategory.is_a?(String) rescue => e e end @dbo @data @data[category.to_s][subcategory.to_s][key.to_s] = new_value File.write(@dbo, @data.to_yaml) end end end end
@!method create_category
(db, category)
Create a new Category inside our DataBase @param db [String] the name of your DataBase file @param category [String] the name of your category
@example Creating the users category
create_category('example', 'users')
# File lib/gembase.rb, line 102 def self.create_category(db, category) begin raise Errors::invalid_param('db', 'String', 'Gembase.create_category') until db.is_a?(String) rescue => e e end begin raise Errors::invalid_param('category', 'String', 'Gembase.create_category') until category.is_a?(String) rescue => e e end category_name = { category.to_s => {} } if @dbf == 0 if @path.eql?('root') File.open("#{db}.rudb", 'a+') do |f| f.write(category_name.to_yaml) end else File.open("#{@path}/#{db}.rudb", 'a+') do |f| f.write(category_name.to_yaml) end end end end
@!method create_db
(db)
Create a new DataBase file with the given name @param db [String] the name for your DataBase file
@example Create a DB called example
create_db('example') #=> 'example.rudb'
# File lib/gembase.rb, line 68 def self.create_db(db) begin raise Errors::invalid_param('db', 'String', 'Gembase.create_db') until db.is_a?(String) rescue => e e end if @path.eql?('root') @dbf = %x(find #{db}.erudb 2>/dev/null | wc -l).to_i # Identify if there's an encrypted db file else @dbf = %x(find #{@path}/#{db}.erudb 2>/dev/null | wc -l).to_i end if @dbf == 0 if @path.eql?('root') FileUtils.touch("#{db}.rudb") @dbo = File.open("#{db}.rudb") else FileUtils.touch("#{@path}/#{db}.rudb") @dbo = File.open("#{@path}/#{db}.rudb") end end File.write(@dbo, YAML.dump({})) @data = YAML.load_file(@dbo, {}) end
@!method create_object
(db, key, value, category=nil, subcategory=nil)
Create an object outside a category @param db [String] the name of your DataBase file @param key [String] the key name of your object @param value [String, Integer, Boolean] the value of your key @param category [String] the name of your category @param subcategory [String] the name of your subcategory
@example Create object 'server' with value 'localhost:8080'. Without categories.
create_object('example', 'server', 'localhost:8080')
@example Create object 'users-limit' with value 5 in 'users' category.
create_object('example', 'users-limit', 5, 'users')
@example Create object 'bloodbath' with value true in 'premium' subcategory.
create_object('example', 'bloodbath', true, 'users', 'premium')
# File lib/gembase.rb, line 220 def self.create_object(db, key, value, category=nil, subcategory=nil) begin raise Errors::invalid_param('db', 'String', 'Gembase.create_object') until db.is_a?(String) rescue => e e end begin raise Errors::invalid_param('key', 'String', 'Gembase.create_object') until key.is_a?(String) rescue => e e end if @dbf == 0 if category.to_s.length.zero? @dbo @data @data.store(key.to_s, value) File.write(@dbo, @data.to_yaml) else begin raise Errors::invalid_param('category', 'String', 'Gembase.create_object') until category.is_a?(String) rescue => e e end if subcategory.to_s.length.zero? @dbo @data @data @data[category.to_s].store(key.to_s, value) File.write(@dbo, @data.to_yaml) else begin raise Errors::invalid_param('subcategory', 'String', 'Gembase.create_object') until subcategory.is_a?(String) rescue => e e end @dbo @data @data[category.to_s][subcategory.to_s].store(key.to_s, value) File.write(@dbo, @data.to_yaml) end end end end
@!method decrypt_db
(db)
Decrypt your database. @param db [String] the name of your DataBase file
@example Decrypting example db
decrypt_db('example')
@note You'll need to decrypt your db to modify it.
@since 1.0.0
# File lib/gembase.rb, line 496 def self.decrypt_db(db) begin raise Errors::invalid_param('db', 'String', 'Gembase.decrypt_db') until db.is_a?(String) rescue => e e end if @encrypted != 0 puts "\n>> Decrypting your database...".colorize(:light_black) if @path.eql('root') system("yaml_vault decrypt #{db}.erudb -o #{db}.rudb") else system("yaml_vault encrypt #{@path}/#{db}.erudb -o #{@path}/#{db}.rudb") end elsif @encrypted == 0 puts('Your database isn\'t encrypted. If you want to encrypt it, use encrypt_db method.'.colorize(:light_black)) end end
@!method delete_db
(db)
Delete your database file. @param db [String] the name of your DataBase file
@example Deleting example db
delete_rb('example')
@since 1.0.0
# File lib/gembase.rb, line 560 def self.delete_db(db) begin raise Errors::invalid_param('db', 'String', 'Gembase.delete_db') until db.is_a?(String) rescue => e e end if @protect_db.eql?('true') Errors::protected_db(db, 'delete') elsif @protect_db.eql?('false') if @path.eql?('root') system("rm #{db}.rudb") else system("rm #{@path}/#{db}.rudb") end end end
@!method delete_object
(db, key, category=nil, subcategory=nil)
Delete an object inside/outside a category. @param db [String] the name of your DataBase file @param key [String] the key name of your object @param category [String] the name of your category @param subcategory [String] the name of your subcategory
@example Deleting object inside a category
delete_object('example', 'users-limit', 'users')
@example Deleting object inside a subcategory
delete_object('example', 'bloodbath', 'users', 'premium')
@example Deleting object outside a category
delete_object('example', 'server')
@note Category is an optional parameter. Use this only when you want to delete an object inside a category.
# File lib/gembase.rb, line 283 def self.delete_object(db, key, category=nil, subcategory=nil) begin raise Errors::invalid_param('db', 'String', 'Gembase.delete_object') until db.is_a?(String) rescue => e e end begin raise Errors::invalid_param('key', 'String', 'Gembase.delete_object') until key.is_a?(String) rescue => e e end if @dbf == 0 if category.to_s.length.zero? @dbo @data @data.delete(key.to_s) if @path.eql?('root') File.open("#{db}.rudb", 'w+') do |f| f.write(@data.to_yaml) end else File.open("#{@path}/#{db}.rudb", 'w+') do |f| f.write(@data.to_yaml) end end else if subcategory.to_s.length.zero? begin raise Errors::invalid_param('category', 'String', 'Gembase.delete_object') until category.is_a?(String) rescue => e e end @dbo @data @data[category.to_s].delete(key.to_s) File.open("#{db}.rudb", 'w+') do |f| f.write(@data.to_yaml) end else begin raise Errors::invalid_param('subcategory', 'String', 'Gembase.delete_object') until subcategory.is_a?(String) rescue => e e end @dbo @data @data[category.to_s][subcategory.to_s].delete(key.to_s) if @path.eql?('root') File.open("#{db}.rudb", 'w+') do |f| f.write(@data.to_yaml) end else File.open("#{@path}/#{db}.rudb", 'w+') do |f| f.write(@data.to_yaml) end end end end end end
@!method encrypt_db
(db)
Encrypt your database. @param db [String] the name of your DataBase file
@example Encrypting example db
encrypt_db('example') #=> example.erudb
@note You'll need to establish a passphrase to encrypt/decrypt.
@since 1.0.0
# File lib/gembase.rb, line 459 def self.encrypt_db(db) begin raise Errors::invalid_param('db', 'String', 'Gembase.encrypt_db') until db.is_a?(String) rescue => e e end @encrypted = @dbf if @protect_db.eql?('true') if @encrypted == 0 puts("\n>> Encrypting your database...".colorize(:light_black), "\n>> Recomendations: set a secure password with more than 8 characters and save\n it in a secure site".colorize(:light_black), "\n") if @path.eql?('root') system("yaml_vault encrypt #{db}.rudb -o #{db}.erudb") system("rm #{db}.rudb") else system("yaml_vault encrypt #{@path}/#{db}.rudb -o #{@path}/#{db}.erudb") system("rm #{@path}/#{db}.rudb") end elsif @encrypted != 0 puts("\n>> Your database #{db} is already encrypted. If you want to decrypt it\n use decrypt_db method.".colorize(:light_black)) end elsif @protect_db.eql?('false') Errors::protected_db(db, 'encrypt') end end
@!method nested_category
(db, parent_category, nested_category
)
Create a new nested category @param db [String] the nme of your DataBase file @param parent_category [String] the name of your parent category @param nested_category [String] the name of the nested category
@example Creating a premium nested category
nested_category('example', 'users', 'premium')
@since 0.5.0
# File lib/gembase.rb, line 142 def self.nested_category(db, parent_category, nested_category) begin raise Errors::invalid_param('db', 'String', 'Gembase.nested_category') until db.is_a?(String) rescue => e e end begin raise Errors::invalid_param('parent_category', 'String', 'Gembase.nested_category') until parent_category.is_a?(String) rescue => e e end begin raise Errors::invalid_param('nested_category', 'String', 'Gembase.nested_category') until nested_category.is_a?(String) rescue => e e end @data @data[parent_category.to_s].store(nested_category.to_s, {}) File.write(@dbo, @data.to_yaml) end
@!method parse(db)
Parse the DataBase structure to modify it @param db [String] the name of your DataBase file
@example Parsing the DB called 'example'
parse('example')
@note You can use these two methods (parse and generate) instead of YAML vanilla methods (load and to_yaml).
# File lib/gembase.rb, line 417 def self.parse(db) begin raise Errors::invalid_param('db', 'String', 'Gembase.parse') until db.is_a?(String) rescue => e e end if @dbf == 0 @dbo @data end end
@!method regenerate(db)
Regenerate the DataBase stucture to YAML structure @param db [String] the name of your DataBase file
@example Regenerating the DB called 'example'
regenerate('example')
# File lib/gembase.rb, line 436 def self.regenerate(db) begin raise Errors::invalid_param('db', 'String', 'Gembase.regenerate') until db.is_a?(String) rescue => e e end if @dbf == 0 @dbo @data.to_yaml end end
@!method rename_db
(old_name, new_name)
Rename your database file. @param old_name [String] the old name of your DataBase file @param new_name [String] the new name of your DataBase file
@example Renaming the example database
rename_db('example', 'example-new')
@since 1.0.0
# File lib/gembase.rb, line 524 def self.rename_db(old_name, new_name) begin raise Errors::invalid_param('old_name', 'String', 'Gembase.rename_db') until old_name.is_a?(String) rescue => e e end begin raise Errors::invalid_param('new_name', 'String', 'Gembase.rename_db') until new_name.is_a?(String) rescue => e e end if %x(find "#{old_name}.rudb" | wc -l).to_i == 0 Errors::custom_error(">> [RuntimeError] | The database file #{old_name} doesn't exist or\n has been already renamed.") else if @protect_db.eql?('true') Errors::protected_db(old_name, 'rename') elsif @protect_db.eql?('false') if @path.eql?('root') system("mv #{old_name}.rudb #{new_name}.rudb") else system("mv #{@path}/#{old_name}.rudb #{@path}/#{new_name}.rudb") end end end end
@!method restart_db
(db)
Restart your database file. @param db [String] the name of your DataBase file
@example Restarting example db
restart_db('example')
@since 1.0.0
# File lib/gembase.rb, line 586 def self.restart_db(db) begin raise Errors::invalid_param('db', 'String', 'Gembase.delete_db') until db.is_a?(String) rescue => e e end if @protect_db.eql?('true') Errors::protected_db(db, 'restart') elsif @protect_db.eql?('false') if @path.eql?('root') system("rm #{db}.rudb") system("touch #{db}.rudb") else system("rm #{@path}/#{db}.rudb") system("touch #{@path}/#{db}.rudb") end end end
@!method settings(db, protect_db, path)
Settings for your database manager @param db [String] name of your DataBase file @param protect_db [Boolean] protect your db. Crypt and more. @param path [String] path to your database files
@example Enable the db protection
settings('example' true, 'root')
@example Changing to a custom path
settings('example, true, './res')
@note You must need to put “root” if you don't want to use custom directories instead of the root directory of your project. @note You must need to write a password in your terminal when you start the file if you protected your db. It'll be required to encrypt and decrypt your db, save this!. @note You must use this method at the beginning of your code for your db to work. Obligatory method.
@since 1.0.0
# File lib/gembase.rb, line 31 def self.settings(db, protect_db, path) begin raise Errors::invalid_param('db', 'String', 'Gembase.settings') until db.is_a?(String) rescue => e e end begin raise Errors::invalid_param('protect_db', 'Boolean', 'Gembase.settings') until protect_db.is_a?(Boolean) rescue => e e end begin raise Errors::invalid_param('path', 'String', 'Gembase.settings') until path.is_a?(String) rescue => e e end @path = path @protect_db = protect_db.to_s if @protect_db == 'true' system('clear') puts(%x(figlet -c Logger).colorize(:red), '====================================================================================='.colorize(:light_black), "\n", ">> [SecurityEnabled] | Now you can remove, modify, encrypt and rename your db.".colorize(:light_red)) elsif @protect_db == 'false' system('clear') puts(%x(figlet -c Logger).colorize(:red), '====================================================================================='.colorize(:light_black)) puts "\n>> [SecurityBreach] | If you don't protect your db, it'll be vulnerable to hackers. \n You must need to enable protection and use Gembase.encrypt".colorize(:light_red) end end
@!method working_db
Lists your working/active database files and display her names.
@example List your working databases
working_db
@note It only works in your terminal. That's an db monitor and doesn't affect your databases.
Warning: don't use this if you don't have any db file in your work directory to avoid errors.
@since 1.0.0
# File lib/gembase.rb, line 616 def self.working_db puts("\n", '====================================================================================='.colorize(:light_black), "\n\n", %x(figlet -c Viewer).colorize(:red), '====================================================================================='.colorize(:light_black)) files = 'find . -wholename "*.rudb" && find . -wholename "*.erudb"' count = 'find . -wholename "*.rudb" | wc -l && find . -wholename "*.erudb" | wc -l' @working_file = %x(#{files}).sub("\n", ', ').delete_suffix("\n").sub(',', '') @arr_files = Array(@working_file.split(' ')) @working_number = %x(#{count}).sub("\n", ', ').delete_suffix("\n").sub(',', '') @arr_count = Array(@working_number.split(' ')) @working_numberf = @arr_count[0].to_i + @arr_count[1].to_i puts "\n> Working databases: ".colorize(:light_red) + "#{@working_numberf.to_s.colorize(:light_black)}" + "\n • Unencrypted databases: ".colorize(:light_red) + "#{@arr_count[0].colorize(:light_black)}" + "\n • Encrypted databases: ".colorize(:light_red) + "#{@arr_count[1].colorize(:light_black)}\n\n" @min = 0 @max = @working_numberf.to_i loop do @protected = @arr_files[@min].end_with?('.erudb') puts "• #{@arr_files[@min].colorize(:light_black)} \n Protected? #{@protected.to_s.colorize(:red)} \n Size: #{%x(ls -sh #{@arr_files[@min]}).delete_suffix("\n").sub(' ', '').delete_suffix("#{@arr_files[@min]}").colorize(:red)} \n\n" @min += 1 if @min >= @max @min = 0 break end end Services::closing end