Class: Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/shed/tool.rb

Overview

Abstract base class for tools. Provides basic default settings, allows control over the level of logging to standard out, and installs a sig int handler.

Constant Summary collapse

INVALID_OPTS =
"Invalid options:"

Instance Method Summary collapse

Constructor Details

#initialize(opt, out = STDOUT) ⇒ Tool



12
13
14
15
16
17
18
19
20
21
# File 'lib/shed/tool.rb', line 12

def initialize(opt,out=STDOUT)
  @src      = opt[:src] || '.'
  @output   = opt[:output] || 'tool-shed.txt'
  @verbose  = opt[:verbose] || false
  @silent   = opt[:silent] || false
  @excludes = opt[:excludes] || ['.svn','.git', 'bin', 'bin-debug']
  @out      = out

  add_sigint_handler
end

Instance Method Details

#add_sigint_handlerObject

Installs a sigint handler.



66
67
68
69
70
71
# File 'lib/shed/tool.rb', line 66

def add_sigint_handler
  trap 'INT' do
    puts '\nCancelled. Bye Bye!'
    exit!
  end
end

#generated_atObject

Generate a timestamp to include in reports.



59
60
61
# File 'lib/shed/tool.rb', line 59

def generated_at
  "Generated at " + Time.now.strftime("[%m/%d/%Y %H:%M:%S]")
end

#log(msg) ⇒ Object

Puts the message if we are in verbose mode (but not in silent mode).



33
34
35
# File 'lib/shed/tool.rb', line 33

def log(msg)
  puts msg if @verbose
end

#puts(msg) ⇒ Object

Puts the message unless we are in silent mode.



26
27
28
# File 'lib/shed/tool.rb', line 26

def puts(msg)
  @out.puts msg unless @silent
end

#to_disk(str) ⇒ Object

Write the requested string to the output file.



47
48
49
50
51
52
53
54
# File 'lib/shed/tool.rb', line 47

def to_disk(str)
  file = File.open(@output, "w")
  file.puts str
  file.flush
  file.close

  puts "Saved result to #{File.expand_path(@output)}."
end

#valid_optsObject

Validates the opts the tool has been invoked with.



40
41
42
# File 'lib/shed/tool.rb', line 40

def valid_opts
  true
end