Class: DockDriver::DockItem

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#commandObject

The command to be run.



44
45
46
# File 'lib/dock_driver/dock_item.rb', line 44

def command
  @command
end

#nameObject

This item’s name.



38
39
40
# File 'lib/dock_driver/dock_item.rb', line 38

def name
  @name
end

#outputObject

The last output from the command.



42
43
44
# File 'lib/dock_driver/dock_item.rb', line 42

def output
  @output
end

#periodObject

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_regexObject

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

#threadObject

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_sObject

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