Class: Ronin::DB::CLI::ModelCommand Private
- Includes:
- CommandKit::Options::Verbose, Core::CLI::Logging, DatabaseOptions
- Defined in:
- lib/ronin/db/cli/model_command.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
A base-command for database models commands.
Direct Known Subclasses
Commands::Asn, Commands::Certs, Commands::Creds, Commands::Emails, Commands::Hosts, Commands::Ips, Commands::OpenPorts, Commands::Oses, Commands::Passwords, Commands::People, Commands::PhoneNumbers, Commands::Ports, Commands::Services, Commands::Software, Commands::StreetAddresses, Commands::Urls, Commands::WebVulns
Constant Summary
Constants included from URIMethods
Instance Attribute Summary collapse
-
#query_method_calls ⇒ Array<(Symbol), (Symbol, Array), (Symbol, Hash), (Symbol, Array, Hash)>
readonly
private
The query method calls to chain together.
Class Method Summary collapse
-
.model_file(new_model_file = nil) ⇒ String
private
Sets or gets the model file to require.
-
.model_name(new_model_name = nil) ⇒ String
private
Sets or gets the model name to lookup.
Instance Method Summary collapse
-
#db_connect ⇒ Object
private
Connects to the database.
-
#initialize(**kwargs) ⇒ ModelCommand
constructor
private
Initializes the command.
-
#list ⇒ Object
private
Queries and lists records.
-
#load_model ⇒ Class<ActiveRecord::Base>
private
The model to query.
-
#model ⇒ Class<ActiveRecord::Base>
private
The model to query.
-
#print_record(record) ⇒ Object
private
Prints the given record.
-
#query ⇒ ActiveRecord::Relation, ActiveRecord::QueryMethods::WhereChain
private
Builds a new query by chaining together the method calls defined by #query_method_calls.
-
#run ⇒ Object
private
Runs the command.
Methods included from DatabaseOptions
Methods included from URIMethods
#normalize_adapter, #normalize_sqlite3_path, #parse_uri
Constructor Details
#initialize(**kwargs) ⇒ ModelCommand
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.
Initializes the command.
107 108 109 110 111 |
# File 'lib/ronin/db/cli/model_command.rb', line 107 def initialize(**kwargs) super(**kwargs) @query_method_calls = [] end |
Instance Attribute Details
#query_method_calls ⇒ Array<(Symbol), (Symbol, Array), (Symbol, Hash), (Symbol, Array, Hash)> (readonly)
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.
The query method calls to chain together.
99 100 101 |
# File 'lib/ronin/db/cli/model_command.rb', line 99 def query_method_calls @query_method_calls end |
Class Method Details
.model_file(new_model_file = nil) ⇒ String
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.
Sets or gets the model file to require.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ronin/db/cli/model_command.rb', line 54 def self.model_file(new_model_file=nil) if new_model_file @model_file = new_model_file else @model_file ||= if superclass < ModelCommand superclass.model_file else raise(NotImplementedError,"#{self} did not define model_file") end end end |
.model_name(new_model_name = nil) ⇒ String
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.
Sets or gets the model name to lookup.
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ronin/db/cli/model_command.rb', line 81 def self.model_name(new_model_name=nil) if new_model_name @model_name = new_model_name else @model_name ||= if superclass < ModelCommand superclass.model_name else raise(NotImplementedError,"#{self} did not define model_name") end end end |
Instance Method Details
#db_connect ⇒ 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.
Connects to the database.
124 125 126 127 128 129 130 |
# File 'lib/ronin/db/cli/model_command.rb', line 124 def db_connect # connect to the database but do not load other models. DB.connect(db_config, load_models: false) # load and connect the model model.connection end |
#list ⇒ 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.
Queries and lists records.
163 164 165 166 |
# File 'lib/ronin/db/cli/model_command.rb', line 163 def list records = query records.each(&method(:print_record)) end |
#load_model ⇒ Class<ActiveRecord::Base>
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.
The model to query.
144 145 146 147 148 |
# File 'lib/ronin/db/cli/model_command.rb', line 144 def load_model require self.class.model_file Ronin::DB.const_get(self.class.model_name) end |
#model ⇒ Class<ActiveRecord::Base>
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.
The model to query.
156 157 158 |
# File 'lib/ronin/db/cli/model_command.rb', line 156 def model @model ||= load_model end |
#print_record(record) ⇒ 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.
Prints the given record.
197 198 199 |
# File 'lib/ronin/db/cli/model_command.rb', line 197 def print_record(record) puts record end |
#query ⇒ ActiveRecord::Relation, ActiveRecord::QueryMethods::WhereChain
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.
Builds a new query by chaining together the method calls defined by #query_method_calls.
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/ronin/db/cli/model_command.rb', line 175 def query common_object_methods = Object.public_instance_methods query = model.all @query_method_calls.each do |method,arguments,kwargs={}| if common_object_methods.include?(method) raise(ArgumentError,"cannot call method Object##{method} on query #{query.inspect}") end query = query.public_send(method,*arguments,**kwargs) end return query 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.
116 117 118 119 |
# File 'lib/ronin/db/cli/model_command.rb', line 116 def run db_connect list end |