Class: Ronin::UI::CLI::ModelCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/ronin/ui/cli/model_command.rb

Overview

A base-command for querying Models.

Direct Known Subclasses

Commands::Repos, ResourcesCommand, ScriptCommand

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

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

Constructor Details

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

Class Method Details

.each_query_option {|name| ... } ⇒ Enumerator

Iterates over the query options for the command.

Yields:

  • (name)

    The given block will be passed the names of the query options.

Yield Parameters:

  • name (Symbol)

    The name of a query option.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator object will be returned.

Since:

  • 1.1.0



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

def self.each_query_option(&block)
  return enum_for(:each_query_option) unless block

  self.class.ancestors.each do |ancestor|
    if ancestor < ModelCommand
      ancestor.query_options.each(&block)
    end
  end
end

.model(model = nil) ⇒ DataMapper::Resource (protected)

Sets or gets the model to query.

Parameters:

  • model (DataMapper::Resource, nil) (defaults to: nil)

    The model class.

Returns:

  • (DataMapper::Resource)

    The model that will be queried.

Since:

  • 1.1.0



87
88
89
90
91
92
93
94
95
# File 'lib/ronin/ui/cli/model_command.rb', line 87

def self.model(model=nil)
  if model
    @model = model
  else
    @model ||= if superclass < ModelCommand
                 superclass.model
               end
  end
end

.query_option(name, options = {}) ⇒ Object (protected)

Defines a query option for the command.

Parameters:

  • name (Symbol)

    The option name.

  • options (Hash) (defaults to: {})

    Additional options.

Since:

  • 1.0.0



110
111
112
113
# File 'lib/ronin/ui/cli/model_command.rb', line 110

def self.query_option(name,options={})
  query_options << name
  class_option name, options
end

.query_optionsArray<Symbol>

The query options for the command.

Returns:

  • (Array<Symbol>)

    The query options and their query method names.

Since:

  • 1.0.0



44
45
46
# File 'lib/ronin/ui/cli/model_command.rb', line 44

def self.query_options
  @query_options ||= []
end

Instance Method Details

#queryDataMapper::Collection (protected)

Builds a new query using the options of the command.

Returns:

  • (DataMapper::Collection)

    The new query.

Raises:

  • (RuntimeError)

    The query option does not map to a query-method or property defined in the Model.

Since:

  • 1.0.0



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/ronin/ui/cli/model_command.rb', line 146

def query
  unless self.class.model
    raise("query model not defined for #{self.class}")
  end

  query = self.class.model.all

  self.class.each_query_option do |name|
    value = options[name]

    # skip unset options
    next if value.nil?

    if model.properties.named?(name)
      query = query.all(name => value)
    elsif model.respond_to?(name)
      query_method = model.method(name)

      query = case query_method.arity
              when 0
                query.send(name)
              when 1
                query.send(name,value)
              else
                query.send(name,*value)
              end
    else
      raise("unknown query method or property #{name} for #{model}")
    end
  end

  return query
end

#setupObject (protected)

Sets up the Database.

Since:

  • 1.1.0



122
123
124
125
126
127
128
129
130
# File 'lib/ronin/ui/cli/model_command.rb', line 122

def setup
  super

  if self.options[:database]
    Database.repositories[:default] = options[:database]
  end

  Database.setup
end