Class: Collective::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/collective/key.rb

Overview

A key uniquely identifies a worker.

Defined Under Namespace

Classes: MalformedKey

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, pid, host = Collective::Key.local_host) ⇒ Key

Returns a new instance of Key.



15
16
17
18
19
# File 'lib/collective/key.rb', line 15

def initialize( name, pid, host = Collective::Key.local_host )
  @name = name
  @pid  = pid.to_i
  @host = host
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



13
14
15
# File 'lib/collective/key.rb', line 13

def host
  @host
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/collective/key.rb', line 11

def name
  @name
end

#pidObject (readonly)

Returns the value of attribute pid.



12
13
14
# File 'lib/collective/key.rb', line 12

def pid
  @pid
end

Class Method Details

.local_hostObject



41
42
43
# File 'lib/collective/key.rb', line 41

def self.local_host
  @local_host ||= `hostname`.chomp.strip
end

.parse(key_string) ⇒ Object



31
32
33
34
# File 'lib/collective/key.rb', line 31

def self.parse(key_string)
  key_string =~ /^(.*)-([0-9]+)@([^@]+)$/ or raise MalformedKey.new(key_string)
  new( $1, $2, $3 )
end

Instance Method Details

#==(other) ⇒ Object



21
22
23
24
# File 'lib/collective/key.rb', line 21

def ==(other)
  self.equal?(other) ||
  ( name == other.name && pid == other.pid && host == other.host )
end

#to_sObject



27
28
29
# File 'lib/collective/key.rb', line 27

def to_s
  "%s-%i@%s" % [ name, pid, host ]
end