Module: Gembase

Defined in:
lib/gembase.rb

Overview

Author:

  • NTBBloodbath

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataObject

Returns the value of attribute data.



12
13
14
# File 'lib/gembase.rb', line 12

def data
  @data
end

#dboObject

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

Deprecated.

Use #create_object instead.

Examples:

Add object ‘bloodbath’ with value ‘premium’

add_object('example', 'users', 'bloodbath', 'premium')


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

Examples:

Change key server value from localhost:8080 to example_host.com outside categories

change_object('example', 'server', 'example_host.com')

Change key users-limit value from 5 to 10 into users category

change_object('example', 'users-limit', '10', 'users')

Change key bloodbath value from true to false into premium subcategory

change_object('example', 'bloodbath", false, 'users', 'category')

Since:

  • 0.5.0



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

Examples:

Creating the users category

create_category('example', 'users')


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

Examples:

Create a DB called example

create_db('example') #=> 'example.rudb'


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

Examples:

Create object ‘server’ with value ‘localhost:8080’. Without categories.

create_object('example', 'server', 'localhost:8080')

Create object ‘users-limit’ with value 5 in ‘users’ category.

create_object('example', 'users-limit', 5, 'users')

Create object ‘bloodbath’ with value true in ‘premium’ subcategory.

create_object('example', 'bloodbath', true, 'users', 'premium')


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

Note:

You’ll need to decrypt your db to modify it.

Examples:

Decrypting example db

decrypt_db('example')

Since:

  • 1.0.0



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

Examples:

Deleting example db

delete_rb('example')

Since:

  • 1.0.0



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

Note:

Category is an optional parameter. Use this only when you want to delete an object inside a category.

Examples:

Deleting object inside a category

delete_object('example', 'users-limit', 'users')

Deleting object inside a subcategory

delete_object('example', 'bloodbath', 'users', 'premium')

Deleting object outside a category

delete_object('example', 'server')


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

Note:

You’ll need to establish a passphrase to encrypt/decrypt.

Examples:

Encrypting example db

encrypt_db('example') #=> example.erudb

Since:

  • 1.0.0



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

Examples:

Creating a premium nested category

nested_category('example', 'users', 'premium')

Since:

  • 0.5.0



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

Note:

You can use these two methods (parse and generate) instead of YAML vanilla methods (load and to_yaml).

Examples:

Parsing the DB called ‘example’

parse('example')


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

Examples:

Regenerating the DB called ‘example’

regenerate('example')


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

Examples:

Renaming the example database

rename_db('example', 'example-new')

Since:

  • 1.0.0



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

Examples:

Restarting example db

restart_db('example')

Since:

  • 1.0.0



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

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.

Examples:

Enable the db protection

settings('example' true, 'root')

Changing to a custom path

settings('example, true, './res')

Since:

  • 1.0.0



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_dbObject

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.

Examples:

List your working databases

working_db

Since:

  • 1.0.0



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

Parameters:

  • db (String)

    the name of your DataBase file

  • category (String)

    the name of your category

  • key (String)

    the key name of your object

  • value (String, Integer, Boolean)

    the value of your key



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

Parameters:

  • db (String)

    the name of your DataBase file

  • key (String)

    the key name of your object

  • new_value (String, Integer, Boolean)

    the new value of your key

  • category (String) (defaults to: nil)

    the name of your category

  • subcategory (String) (defaults to: nil)

    the name of your subcategory



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

Parameters:

  • db (String)

    the name of your DataBase file

  • category (String)

    the name of your category



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

Parameters:

  • db (String)

    the name for your DataBase file



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

Parameters:

  • db (String)

    the name of your DataBase file

  • key (String)

    the key name of your object

  • value (String, Integer, Boolean)

    the value of your key

  • category (String) (defaults to: nil)

    the name of your category

  • subcategory (String) (defaults to: nil)

    the name of your subcategory



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.

Parameters:

  • db (String)

    the name of your DataBase file



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.

Parameters:

  • db (String)

    the name of 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.

Parameters:

  • db (String)

    the name of your DataBase file

  • key (String)

    the key name of your object

  • category (String) (defaults to: nil)

    the name of your category

  • subcategory (String) (defaults to: nil)

    the name of your subcategory



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.

Parameters:

  • db (String)

    the name of your DataBase file



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

Parameters:

  • db (String)

    the nme of your DataBase file

  • parent_category (String)

    the name of your parent category

  • nested_category (String)

    the name of the 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

Parameters:

  • db (String)

    the name of your DataBase file



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

Parameters:

  • db (String)

    the name of your DataBase file



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.

Parameters:

  • old_name (String)

    the old name of your DataBase file

  • new_name (String)

    the new name of 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.

Parameters:

  • db (String)

    the name of 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

Parameters:

  • db (String)

    name of your DataBase file

  • protect_db (Boolean)

    protect your db. Crypt and more.

  • path (String)

    path to your database files



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_dbObject

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