Module: Ronin::DB::CLI::Importable Private
- Defined in:
- lib/ronin/db/cli/importable.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Allows a ModelCommand to add or import records from a file.
Class Method Summary collapse
-
.included(command) ⇒ Object
private
Adds the
--add
and--import
options to the command.
Instance Method Summary collapse
-
#add(value) ⇒ Object
private
Adds a value to the database.
-
#import_file(path) ⇒ Object
private
Imports the values from the given file.
-
#run ⇒ Object
private
Runs the command.
Class Method Details
.included(command) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds the --add
and --import
options to the command.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ronin/db/cli/importable.rb', line 36 def self.included(command) command.option :add, value: { type: String, usage: 'VALUE' }, desc: 'Adds a value to the database' command.option :import, value: { type: String, usage: 'FILE' }, desc: 'Imports the values from the FILE into the database' end |
Instance Method Details
#add(value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds a value to the database.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/ronin/db/cli/importable.rb', line 71 def add(value) record = model.import(value) unless record.valid? print_error "failed to import #{value}!" record.errors..each do || print_error " - #{}" end end end |
#import_file(path) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Imports the values from the given file.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/ronin/db/cli/importable.rb', line 89 def import_file(path) unless File.file?(path) print_error "no such file or directory: #{path}" exit(-1) end File.open(path) do |file| model.transaction do file.each_line(chomp: true) do |value| log_info "Importing #{value} ..." if verbose? add(value) end end end end |
#run ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Runs the command.
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ronin/db/cli/importable.rb', line 53 def run if [:add] db_connect add([:add]) elsif [:import] db_connect import_file([:import]) else super end end |