Module: Gembase
- Defined in:
- lib/gembase.rb
Overview
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#dbo ⇒ Object
Returns the value of attribute dbo.
Class Method Summary collapse
-
.add_object(db, category, key, value) ⇒ Object
deprecated
Deprecated.
Use #create_object instead.
- .change_object(db, key, new_value, category = nil, subcategory = nil) ⇒ Object
- .create_category(db, category) ⇒ Object
- .create_db(db) ⇒ Object
- .create_object(db, key, value, category = nil, subcategory = nil) ⇒ Object
- .decrypt_db(db) ⇒ Object
- .delete_db(db) ⇒ Object
- .delete_object(db, key, category = nil, subcategory = nil) ⇒ Object
- .encrypt_db(db) ⇒ Object
- .nested_category(db, parent_category, nested_category) ⇒ Object
- .parse(db) ⇒ Object
- .regenerate(db) ⇒ Object
- .rename_db(old_name, new_name) ⇒ Object
- .restart_db(db) ⇒ Object
- .settings(db, protect_db, path) ⇒ Object
- .working_db ⇒ Object
Instance Method Summary collapse
-
#add_object(db, category, key, value) ⇒ Object
Create an object inside a category.
-
#change_object(db, key, new_value, category = nil, subcategory = nil) ⇒ Object
Change the value of an existing object.
-
#create_category(db, category) ⇒ Object
Create a new Category inside our DataBase.
-
#create_db(db) ⇒ Object
Create a new DataBase file with the given name.
-
#create_object(db, key, value, category = nil, subcategory = nil) ⇒ Object
Create an object outside a category.
-
#decrypt_db(db) ⇒ Object
Decrypt your database.
-
#delete_db(db) ⇒ Object
Delete your database file.
-
#delete_object(db, key, category = nil, subcategory = nil) ⇒ Object
Delete an object inside/outside a category.
-
#encrypt_db(db) ⇒ Object
Encrypt your database.
-
#nested_category(db, parent_category, nested_category) ⇒ Object
Create a new nested category.
-
#parse(db) ⇒ Object
Parse the DataBase structure to modify it.
-
#regenerate(db) ⇒ Object
Regenerate the DataBase stucture to YAML structure.
-
#rename_db(old_name, new_name) ⇒ Object
Rename your database file.
-
#restart_db(db) ⇒ Object
Restart your database file.
-
#settings(db, protect_db, path) ⇒ Object
Settings for your database manager.
-
#working_db ⇒ Object
Lists your working/active database files and display her names.
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
12 13 14 |
# File 'lib/gembase.rb', line 12 def data @data end |
#dbo ⇒ Object
Returns the value of attribute dbo.
12 13 14 |
# File 'lib/gembase.rb', line 12 def dbo @dbo end |
Class Method Details
.add_object(db, category, key, value) ⇒ Object
Use #create_object instead.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# 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 |
.change_object(db, key, new_value, category = nil, subcategory = nil) ⇒ Object
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# 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 |
.create_category(db, category) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# 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 |
.create_db(db) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# 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 |
.create_object(db, key, value, category = nil, subcategory = nil) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# 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 |
.decrypt_db(db) ⇒ Object
You’ll need to decrypt your db to modify it.
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 |
# 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 |
.delete_db(db) ⇒ Object
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 |
# 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 |
.delete_object(db, key, category = nil, subcategory = nil) ⇒ Object
Category is an optional parameter. Use this only when you want to delete an object inside a category.
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# 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 |
.encrypt_db(db) ⇒ Object
You’ll need to establish a passphrase to encrypt/decrypt.
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
# 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 |
.nested_category(db, parent_category, nested_category) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# 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 |
.parse(db) ⇒ Object
You can use these two methods (parse and generate) instead of YAML vanilla methods (load and to_yaml).
417 418 419 420 421 422 423 424 425 426 427 428 |
# 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 |
.regenerate(db) ⇒ Object
436 437 438 439 440 441 442 443 444 445 446 447 |
# 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 |
.rename_db(old_name, new_name) ⇒ Object
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 |
# 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 |
.restart_db(db) ⇒ Object
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
# 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 |
.settings(db, protect_db, path) ⇒ Object
You must need to put “root” if you don’t want to use custom directories instead of the root directory of your project.
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!.
You must use this method at the beginning of your code for your db to work. Obligatory method.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# 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 |
.working_db ⇒ Object
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.
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 |
# 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 |
Instance Method Details
#add_object(db, category, key, value) ⇒ Object
Create an object inside a category
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# 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 |
#change_object(db, key, new_value, category = nil, subcategory = nil) ⇒ Object
Change the value of an existing object
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# 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 |
#create_category(db, category) ⇒ Object
Create a new Category inside our DataBase
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# 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 |
#create_db(db) ⇒ Object
Create a new DataBase file with the given name
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# 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 |
#create_object(db, key, value, category = nil, subcategory = nil) ⇒ Object
Create an object outside a category
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# 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 |
#decrypt_db(db) ⇒ Object
Decrypt your database.
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 |
# 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 |
#delete_db(db) ⇒ Object
Delete your database file.
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 |
# 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 |
#delete_object(db, key, category = nil, subcategory = nil) ⇒ Object
Delete an object inside/outside a category.
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# 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 |
#encrypt_db(db) ⇒ Object
Encrypt your database.
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
# 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 |
#nested_category(db, parent_category, nested_category) ⇒ Object
Create a new nested category
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# 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 |
#parse(db) ⇒ Object
Parse the DataBase structure to modify it
417 418 419 420 421 422 423 424 425 426 427 428 |
# 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 |
#regenerate(db) ⇒ Object
Regenerate the DataBase stucture to YAML structure
436 437 438 439 440 441 442 443 444 445 446 447 |
# 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 |
#rename_db(old_name, new_name) ⇒ Object
Rename your database file.
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 |
# 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 |
#restart_db(db) ⇒ Object
Restart your database file.
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
# 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 |
#settings(db, protect_db, path) ⇒ Object
Settings for your database manager
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# 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 |
#working_db ⇒ Object
Lists your working/active database files and display her names.
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 |
# 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 |