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

Inherits:
DatabaseCommand show all
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.

Constant Summary

Constants included from URIMethods

URIMethods::ADAPTER_ALIASES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DatabaseCommand

#config

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.



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_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)>)


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.

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.



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.

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.



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

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



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

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



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_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:



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

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



154
155
156
# File 'lib/ronin/db/cli/model_command.rb', line 154

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.



195
196
197
# File 'lib/ronin/db/cli/model_command.rb', line 195

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.



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

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



114
115
116
117
# File 'lib/ronin/db/cli/model_command.rb', line 114

def run
  connect
  list
end