Class: WorkShaper::OffsetHolder

Inherits:
Object
  • Object
show all
Defined in:
lib/work_shaper/offset_holder.rb

Constant Summary collapse

STATES =
{enqueued: 2, acked: 1, completed: 0}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(partition, offset, at: Time.now.to_f) ⇒ OffsetHolder

Returns a new instance of OffsetHolder.



6
7
8
9
10
11
12
# File 'lib/work_shaper/offset_holder.rb', line 6

def initialize(partition, offset, at: Time.now.to_f)
  @partition = partition
  @offset = offset
  @at = at

  @state = :enqueued
end

Instance Attribute Details

#offsetObject (readonly)

Returns the value of attribute offset.



3
4
5
# File 'lib/work_shaper/offset_holder.rb', line 3

def offset
  @offset
end

#partitionObject (readonly)

Returns the value of attribute partition.



3
4
5
# File 'lib/work_shaper/offset_holder.rb', line 3

def partition
  @partition
end

#stateObject (readonly)

Returns the value of attribute state.



3
4
5
# File 'lib/work_shaper/offset_holder.rb', line 3

def state
  @state
end

Instance Method Details

#<(other) ⇒ Object



18
19
20
# File 'lib/work_shaper/offset_holder.rb', line 18

def <(other)
  self.<=>(other) == -1
end

#<=(other) ⇒ Object



14
15
16
# File 'lib/work_shaper/offset_holder.rb', line 14

def <=(other)
  self.<=>(other) <= 0
end

#<=>(other) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/work_shaper/offset_holder.rb', line 22

def <=>(other)
  r = offset <=> other.offset

  if r == 0
    r = STATES[state] <=> STATES[other.state]
    WorkShaper.logger.debug "States: #{r} | #{STATES[state]} #{STATES[other.state]}"
  end

  if r == 0
    r = @at <=> other.instance_variable_get(:@at)
    WorkShaper.logger.debug "At: #{r}"
  end
  WorkShaper.logger.debug "Final: #{r}"
  r
end

#ack!Object



38
39
40
# File 'lib/work_shaper/offset_holder.rb', line 38

def ack!
  @state = :acked
end

#complete!Object



42
43
44
# File 'lib/work_shaper/offset_holder.rb', line 42

def complete!
  @state = :completed
end

#completed?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/work_shaper/offset_holder.rb', line 46

def completed?
  @state == :completed
end

#to_iObject



50
51
52
# File 'lib/work_shaper/offset_holder.rb', line 50

def to_i
  offset
end

#to_sObject



54
55
56
# File 'lib/work_shaper/offset_holder.rb', line 54

def to_s
  "#{partition}/#{offset}:#{STATES[state]}"
end