Class: Ronin::UI::CLI::ModelCommand
- Defined in:
- lib/ronin/ui/cli/model_command.rb
Overview
A base-command for querying Models.
Direct Known Subclasses
Class Method Summary collapse
-
.each_query_option {|name| ... } ⇒ Enumerator
Iterates over the query options for the command.
-
.model(model = nil) ⇒ DataMapper::Resource
protected
Sets or gets the model to query.
-
.query_option(name, options = {}) ⇒ Object
protected
Defines a query option for the command.
-
.query_options ⇒ Array<Symbol>
The query options for the command.
Instance Method Summary collapse
-
#query ⇒ DataMapper::Collection
protected
Builds a new query using the options of the command.
-
#setup ⇒ Object
protected
Sets up the Database.
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.
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..each(&block) end end end |
.model(model = nil) ⇒ DataMapper::Resource (protected)
Sets or gets the model to query.
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.
110 111 112 113 |
# File 'lib/ronin/ui/cli/model_command.rb', line 110 def self.query_option(name,={}) << name class_option name, end |
.query_options ⇒ Array<Symbol>
The query options for the command.
44 45 46 |
# File 'lib/ronin/ui/cli/model_command.rb', line 44 def self. @query_options ||= [] end |
Instance Method Details
#query ⇒ DataMapper::Collection (protected)
Builds a new query using the options of the command.
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 = [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 |
#setup ⇒ Object (protected)
Sets up the Database.
122 123 124 125 126 127 128 129 130 |
# File 'lib/ronin/ui/cli/model_command.rb', line 122 def setup super if self.[:database] Database.repositories[:default] = [:database] end Database.setup end |