Class: Ronin::UI::CLI::ResourcesCommand

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

Overview

A base-command class for listing Database Resources.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ModelCommand

each_query_option, #query, query_option, query_options, #setup

Methods inherited from Command

banner, command_name, #indent, inherited, #initialize, #print_array, #print_exception, #print_hash, #print_section, #print_title, #puts, run, #setup

Constructor Details

This class inherits a constructor from Ronin::UI::CLI::Command

Class Method Details

.model(model = nil) ⇒ Object (protected)

Sets the model used by the command.

See Also:

Since:

  • 1.3.0



62
63
64
65
66
67
68
69
70
# File 'lib/ronin/ui/cli/resources_command.rb', line 62

def self.model(model=nil)
  if (model && model < Model::Importable)
    class_option :import, :type => :string,
                          :aliases => '-i',
                          :banner => 'FILE'
  end

  return super(model)
end

Instance Method Details

#executeObject

Default method performs the query and prints the found resources.

Since:

  • 1.1.0



43
44
45
46
47
48
49
50
51
# File 'lib/ronin/ui/cli/resources_command.rb', line 43

def execute
  if options[:import]
    self.class.model.import(options[: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



82
83
84
# File 'lib/ronin/ui/cli/resources_command.rb', line 82

def print_resource(resource)
  puts resource
end

Prints multiple resources.

Parameters:

  • resources (DataMapper::Collection)

    The query to print.

Since:

  • 1.1.0



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ronin/ui/cli/resources_command.rb', line 96

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