Class: GrepCommandBuilder
- Inherits:
-
Object
- Object
- GrepCommandBuilder
- Defined in:
- lib/clarity/commands/grep_command_builder.rb
Direct Known Subclasses
Defined Under Namespace
Classes: InvalidParameterError
Constant Summary collapse
- TermParameters =
parameter names
['term1', 'term2', 'term3']
- FileParameter =
'file'
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#params ⇒ Object
Returns the value of attribute params.
-
#terms ⇒ Object
readonly
Returns the value of attribute terms.
Instance Method Summary collapse
- #bzip_tools ⇒ Object
- #command ⇒ Object
- #default_tools ⇒ Object
- #exec_functions ⇒ Object
- #gzip_tools ⇒ Object
-
#initialize(params) ⇒ GrepCommandBuilder
constructor
A new instance of GrepCommandBuilder.
- #valid? ⇒ Boolean
Constructor Details
#initialize(params) ⇒ GrepCommandBuilder
Returns a new instance of GrepCommandBuilder.
10 11 12 13 14 15 16 |
# File 'lib/clarity/commands/grep_command_builder.rb', line 10 def initialize(params) @params = params @filename = params.fetch(FileParameter) @terms = TermParameters.map {|term| params.fetch(term, nil) }.compact.reject {|term| term.empty? } @options = "" valid? end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
8 9 10 |
# File 'lib/clarity/commands/grep_command_builder.rb', line 8 def filename @filename end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/clarity/commands/grep_command_builder.rb', line 8 def @options end |
#params ⇒ Object
Returns the value of attribute params.
7 8 9 |
# File 'lib/clarity/commands/grep_command_builder.rb', line 7 def params @params end |
#terms ⇒ Object (readonly)
Returns the value of attribute terms.
8 9 10 |
# File 'lib/clarity/commands/grep_command_builder.rb', line 8 def terms @terms end |
Instance Method Details
#bzip_tools ⇒ Object
46 47 48 |
# File 'lib/clarity/commands/grep_command_builder.rb', line 46 def bzip_tools terms.empty? ? ['bzcat filename'] : ['bzgrep options -e term filename'] + ['grep options -e term'] * (terms.size-1) end |
#command ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/clarity/commands/grep_command_builder.rb', line 23 def command results = [] exec_functions.each_with_index do |cmd, index| results << cmd.gsub('filename', filename.to_s).gsub('options', .to_s).gsub('term', terms[index].to_s) end %[sh -c '#{results.join(" | ")}'] end |
#default_tools ⇒ Object
50 51 52 |
# File 'lib/clarity/commands/grep_command_builder.rb', line 50 def default_tools terms.empty? ? ['cat filename'] : ['grep options -e term filename'] + ['grep options -e term']* (terms.size-1) end |
#exec_functions ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/clarity/commands/grep_command_builder.rb', line 32 def exec_functions case File.extname(filename) when '.gz' then gzip_tools when '.bz2' then bzip_tools else default_tools end end |
#gzip_tools ⇒ Object
41 42 43 44 |
# File 'lib/clarity/commands/grep_command_builder.rb', line 41 def gzip_tools cat_tool = (ENV["PATH"].split(":").find{|d| File.exists?(File.join(d, "gzcat"))} ? "zcat" : "gzcat") terms.empty? ? ["#{cat_tool} filename"] : ['zgrep options -e term filename'] + ['grep options -e term'] * (terms.size-1) end |
#valid? ⇒ Boolean
18 19 20 21 |
# File 'lib/clarity/commands/grep_command_builder.rb', line 18 def valid? raise InvalidParameterError, "Log file parameter not supplied" unless filename && !filename.empty? true end |