Class: Ronin::DB::CLI::ModelCommand Private

Inherits:
Command
  • Object
show all
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.

Constant Summary

Constants included from URIMethods

URIMethods::ADAPTER_ALIASES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DatabaseOptions

#db_config, included

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.

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.



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_callsArray<(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.

Returns:

  • (Array<(Symbol), (Symbol, Array), (Symbol, Hash), (Symbol, Array, Hash)>)


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.

Examples:

model_file 'ronin/db/foo'

Parameters:

  • new_model_file (String, nil) (defaults to: nil)

    The new model file.

Returns:

  • (String)

    The model file to require.

Raises:

  • (NotImplementedError)

    The class did not define a model_file.



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.

Examples:

model_name 'Foo'

Parameters:

  • new_model_name (String, nil) (defaults to: nil)

    The new model name.

Returns:

  • (String)

    The model name to lookup.

Raises:

  • (NotImplementedError)

    The class did not define a model_name.



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_connectObject

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

#listObject

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_modelClass<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.

Returns:

  • (Class<ActiveRecord::Base>)

    The loaded model.

Raises:



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

#modelClass<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.

Returns:

  • (Class<ActiveRecord::Base>)

    The loaded model.



156
157
158
# File 'lib/ronin/db/cli/model_command.rb', line 156

def model
  @model ||= load_model
end

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.

Parameters:

  • record (ActiveRecord::Base)

    The record to print.



197
198
199
# File 'lib/ronin/db/cli/model_command.rb', line 197

def print_record(record)
  puts record
end

#queryActiveRecord::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.

Returns:

  • (ActiveRecord::Relation, ActiveRecord::QueryMethods::WhereChain)

    The new query.



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

#runObject

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