Class: Ronin::DB::CLI::ModelCommand Private
- Inherits:
-
DatabaseCommand
- Object
- Core::CLI::Command
- Command
- DatabaseCommand
- Ronin::DB::CLI::ModelCommand
- Includes:
- CommandKit::Options::Verbose, Core::CLI::Logging
- 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::Creds, Commands::Emails, Commands::Hosts, Commands::Ips, Commands::Urls, ResourcesCommand
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
-
#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 inherited from DatabaseCommand
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.
105 106 107 108 109 |
# File 'lib/ronin/db/cli/model_command.rb', line 105 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.
97 98 99 |
# File 'lib/ronin/db/cli/model_command.rb', line 97 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.
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ronin/db/cli/model_command.rb', line 52 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.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ronin/db/cli/model_command.rb', line 79 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
#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.
122 123 124 125 126 127 128 |
# File 'lib/ronin/db/cli/model_command.rb', line 122 def connect # connect to the database but do not load other models. DB.connect(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.
161 162 163 164 |
# File 'lib/ronin/db/cli/model_command.rb', line 161 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.
142 143 144 145 146 |
# File 'lib/ronin/db/cli/model_command.rb', line 142 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.
154 155 156 |
# File 'lib/ronin/db/cli/model_command.rb', line 154 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.
195 196 197 |
# File 'lib/ronin/db/cli/model_command.rb', line 195 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.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/ronin/db/cli/model_command.rb', line 173 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.
114 115 116 117 |
# File 'lib/ronin/db/cli/model_command.rb', line 114 def run connect list end |