Class: Ronin::DB::CLI::ResourcesCommand

Inherits:
ModelCommand show all
Defined in:
lib/ronin/db/cli/resources_command.rb

Overview

A base-command class for listing Database Resources.

Constant Summary

Constants included from URIMethods

URIMethods::ADAPTER_ALIASES

Instance Attribute Summary

Attributes inherited from ModelCommand

#query_method_calls

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ModelCommand

#connect, #initialize, #list, #load_model, #model, model_file, model_name, #print_record, #query, #run

Methods inherited from DatabaseCommand

#config, #connect

Methods included from URIMethods

#normalize_adapter, #normalize_sqlite3_path, #parse_uri

Constructor Details

This class inherits a constructor from Ronin::DB::CLI::ModelCommand

Class Method Details

.model(model = nil) ⇒ Object

Sets the model used by the command.

See Also:

Since:

  • 1.3.0



70
71
72
73
74
75
76
77
78
79
# File 'lib/ronin/db/cli/resources_command.rb', line 70

def self.model(model=nil)
  if (model && model < Model::Importable)
    option :import, type:        String,
                    flag:        '-i',
                    usage:       'FILE',
                    description: 'The file to import'
  end

  return super(model)
end

Instance Method Details

#executeObject

Default method performs the query and prints the found resources.

Since:

  • 1.1.0



53
54
55
56
57
58
59
60
61
# File 'lib/ronin/db/cli/resources_command.rb', line 53

def execute
  if @import
    self.class.model.import(@import) do |resource|
      print_info "Imported #{resource}"
    end
  else
    print_resources(query)
  end
end

Default method which will print every queried resource.

Parameters:

  • resource (DataMapper::Resource)

    A queried resource from the Database.

Since:

  • 1.1.0



91
92
93
# File 'lib/ronin/db/cli/resources_command.rb', line 91

def print_resource(resource)
  puts resource
end

Prints multiple resources.

Parameters:

  • resources (DataMapper::Collection)

    The query to print.

Since:

  • 1.1.0



105
106
107
108
109
110
111
112
113
# File 'lib/ronin/db/cli/resources_command.rb', line 105

def print_resources(resources)
  if    csv?  then puts resources.to_csv
  elsif xml?  then puts resources.to_xml
  elsif yaml? then puts resources.to_yaml
  elsif json? then puts resources.to_json
  else
    resources.each { |resource| print_resource(resource) }
  end
end