Class: DockDriver::DockItem
- Inherits:
-
Object
- Object
- DockDriver::DockItem
- Extended by:
- Loggability
- Includes:
- Observable
- Defined in:
- lib/dock_driver/dock_item.rb
Overview
Runs a command periodically and notifies an observer periodically, and provides output from the command.
In your dock_driver config YAML file (typically ~/.dock_driver.yml), you can specify lists of DockItems to create and make available for your template. For example:
In your YAML file:
dock:
items:
- name: mail
command: dock_mail.rb
period: 10
The above creates an item named ‘mail’ that executes the command script dock_mail.rb every 10 seconds.
To include the output of dock_mail.rb in your dock, you’d then add the following to the template section of your YAML file.
<%= mail %>
Instance Attribute Summary collapse
-
#command ⇒ Object
The command to be run.
-
#name ⇒ Object
This item’s name.
-
#output ⇒ Object
The last output from the command.
-
#period ⇒ Object
How many seconds to wait between command executions.
-
#scan_regex ⇒ Object
A regex to scan for in the command output.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ DockItem
constructor
A standard contstructor.
-
#thread ⇒ Object
Lazily create the thread to periodically call poll.
-
#to_s ⇒ Object
Returns the formatted output from the command.
Constructor Details
#initialize(opts = {}) ⇒ DockItem
A standard contstructor.
49 50 51 52 53 54 55 56 |
# File 'lib/dock_driver/dock_item.rb', line 49 def initialize( opts = {} ) #:nodoc: @name = opts['name'] @period = opts['period'] || 1 @command = opts['command'] @scan_regex = Regexp.new( opts['scan_regex'] ) if opts['scan_regex'] @thread = nil @output_lock = Mutex.new end |
Instance Attribute Details
#command ⇒ Object
The command to be run.
44 45 46 |
# File 'lib/dock_driver/dock_item.rb', line 44 def command @command end |
#name ⇒ Object
This item’s name.
38 39 40 |
# File 'lib/dock_driver/dock_item.rb', line 38 def name @name end |
#output ⇒ Object
The last output from the command.
42 43 44 |
# File 'lib/dock_driver/dock_item.rb', line 42 def output @output end |
#period ⇒ Object
How many seconds to wait between command executions.
40 41 42 |
# File 'lib/dock_driver/dock_item.rb', line 40 def period @period end |
#scan_regex ⇒ Object
A regex to scan for in the command output.
46 47 48 |
# File 'lib/dock_driver/dock_item.rb', line 46 def scan_regex @scan_regex end |
Instance Method Details
#thread ⇒ Object
Lazily create the thread to periodically call poll.
59 60 61 |
# File 'lib/dock_driver/dock_item.rb', line 59 def thread return @thread ||= Thread.new { loop { poll } } end |
#to_s ⇒ Object
Returns the formatted output from the command.
64 65 66 |
# File 'lib/dock_driver/dock_item.rb', line 64 def to_s return @output_lock.synchronize { self.output.to_s } end |