Class: IORequest::Utility::ExtendedID

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/io_request/utility/with_id.rb

Overview

Extended Id of object

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pid = nil, tid = nil, oid = nil) ⇒ ExtendedID

Create new Id based on PID, thread ID and object ID.



11
12
13
14
15
# File 'lib/io_request/utility/with_id.rb', line 11

def initialize(pid = nil, tid = nil, oid = nil)
  @pid = pid || Process.pid
  @tid = tid || Thread.current.object_id
  @oid = oid || object_id
end

Instance Attribute Details

#oidInteger (readonly)

Returns object ID.

Returns:

  • (Integer)

    object ID.



24
25
26
# File 'lib/io_request/utility/with_id.rb', line 24

def oid
  @oid
end

#pidInteger (readonly)

Returns process ID.

Returns:

  • (Integer)

    process ID.



18
19
20
# File 'lib/io_request/utility/with_id.rb', line 18

def pid
  @pid
end

#tidInteger (readonly)

Returns thread ID.

Returns:

  • (Integer)

    thread ID.



21
22
23
# File 'lib/io_request/utility/with_id.rb', line 21

def tid
  @tid
end

Class Method Details

.from(obj) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/io_request/utility/with_id.rb', line 42

def self.from(obj)
  case obj
  when ExtendedID then new(obj.pid, obj.tid, obj.oid)
  when String then new(*obj.split('#').map(&:to_i))
  else
    raise 'unknown type'
  end
end

Instance Method Details

#<=>(other) ⇒ Object

Comparison operator.



32
33
34
35
36
37
38
39
40
# File 'lib/io_request/utility/with_id.rb', line 32

def <=>(other)
  if @pid == other.pid && @tid == other.tid
    @oid <=> other.oid
  elsif @pid == other.pid && @tid != other.tid
    @tid <=> tid
  else
    @pid <=> other.pid
  end
end

#to_sString

Returns:

  • (String)


27
28
29
# File 'lib/io_request/utility/with_id.rb', line 27

def to_s
  "#{@pid}##{@tid}##{@oid}"
end