Class: Readorder::Command
- Inherits:
-
Object
- Object
- Readorder::Command
- Defined in:
- lib/readorder/command.rb
Overview
The Command is the base class for any class that wants to implement a command line command for
Inheriting from this calss will make the class registered and be available for invocation from the Runner class
The lifecycle of a command is:
1) instantiation with a hash parameter
2) before
3) run
4) after
5) error calld if the runner catches and exception from the command
Direct Known Subclasses
Readorder::Commands::Analyze, Readorder::Commands::Sort, Readorder::Commands::Test
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#analyzer ⇒ Object
readonly
Returns the value of attribute analyzer.
-
#filelist ⇒ Object
readonly
Returns the value of attribute filelist.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Class Method Summary collapse
- .command_name ⇒ Object
-
.commands ⇒ Object
The list of commands registered.
-
.find(name) ⇒ Object
get the command klass for the given name.
-
.inherited(klass) ⇒ Object
this method is invoked by the Ruby interpreter whenever a class inherts from Command.
Instance Method Summary collapse
-
#after ⇒ Object
called by runner when all is done.
-
#before ⇒ Object
called by the Runner before the command, this can be used to setup additional items for the command.
- #command_name ⇒ Object
-
#error ⇒ Object
called by the Runner if an error is encountered during the run method.
- #get_physical? ⇒ Boolean
-
#initialize(opts = {}) ⇒ Command
constructor
A new instance of Command.
- #logger ⇒ Object
- #results ⇒ Object
- #results_dbfile ⇒ Object
-
#run ⇒ Object
called by the Runner to execute the command.
-
#shutdown ⇒ Object
called by runner if a signal is hit.
Constructor Details
#initialize(opts = {}) ⇒ Command
Returns a new instance of Command.
30 31 32 33 34 35 36 |
# File 'lib/readorder/command.rb', line 30 def initialize( opts = {} ) @options = opts @filelist = nil @analyzer = nil @output = nil @delete_results = true end |
Instance Attribute Details
#analyzer ⇒ Object (readonly)
Returns the value of attribute analyzer.
27 28 29 |
# File 'lib/readorder/command.rb', line 27 def analyzer @analyzer end |
#filelist ⇒ Object (readonly)
Returns the value of attribute filelist.
26 27 28 |
# File 'lib/readorder/command.rb', line 26 def filelist @filelist end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
25 26 27 |
# File 'lib/readorder/command.rb', line 25 def @options end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
28 29 30 |
# File 'lib/readorder/command.rb', line 28 def output @output end |
Class Method Details
.command_name ⇒ Object
21 22 23 |
# File 'lib/readorder/command.rb', line 21 def self.command_name name.split("::").last.downcase end |
.commands ⇒ Object
The list of commands registered.
152 153 154 155 156 157 |
# File 'lib/readorder/command.rb', line 152 def commands unless defined? @commands @commands = [] end return @commands end |
.find(name) ⇒ Object
get the command klass for the given name
160 161 162 |
# File 'lib/readorder/command.rb', line 160 def find( name ) @commands.find { |klass| klass.command_name == name } end |
.inherited(klass) ⇒ Object
this method is invoked by the Ruby interpreter whenever a class inherts from Command. This is how commands register to be invoked
144 145 146 147 148 |
# File 'lib/readorder/command.rb', line 144 def inherited( klass ) return unless klass.instance_of? Class return if commands.include? klass commands << klass end |
Instance Method Details
#after ⇒ Object
called by runner when all is done
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/readorder/command.rb', line 123 def after() if ['error-filelist'] then if analyzer.bad_data_count > 0 then File.open( ['error-filelist'], "w+" ) do |f| analyzer.dump_errors_to( f ) end logger.info "wrote error filelist to #{['error-filelist']}" end end if output != $stdout then output.close results.close File.unlink( results_dbfile ) if @delete_results end end |
#before ⇒ Object
called by the Runner before the command, this can be used to setup additional items for the command
104 |
# File 'lib/readorder/command.rb', line 104 def before() ; end |
#command_name ⇒ Object
94 95 96 |
# File 'lib/readorder/command.rb', line 94 def command_name self.class.command_name end |
#error ⇒ Object
called by the Runner if an error is encountered during the run method
112 113 114 |
# File 'lib/readorder/command.rb', line 112 def error() results.close end |
#get_physical? ⇒ Boolean
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/readorder/command.rb', line 81 def get_physical? return false if @options['inode'] unless Datum.is_linux? then logger.warn "unable to get physical block number, this is not a linux machine, it is #{Config::CONFIG['host_os']}" return false end unless Process.euid == 0 then logger.warn "no permissions to get physical block number, try running as root." return false end return true end |
#logger ⇒ Object
98 99 100 |
# File 'lib/readorder/command.rb', line 98 def logger ::Logging::Logger[self] end |
#results ⇒ Object
64 65 66 |
# File 'lib/readorder/command.rb', line 64 def results @results ||= Results.new( results_dbfile, ['batch-size'] ) end |
#results_dbfile ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/readorder/command.rb', line 54 def results_dbfile if ['output'] then output_dname = File.dirname( ['output'] ) output_bname = File.basename( ['output'], '.*' ) return File.join( output_dname, "#{output_bname}.db" ) else return ":memory:" end end |
#run ⇒ Object
called by the Runner to execute the command
107 108 109 |
# File 'lib/readorder/command.rb', line 107 def run raise Error, "Unknown command `#{command_name}`" end |
#shutdown ⇒ Object
called by runner if a signal is hit
117 118 119 120 |
# File 'lib/readorder/command.rb', line 117 def shutdown() results.close @delete_results = false end |