Class: Dynflow::Director::QueueHash

Inherits:
Object
  • Object
show all
Includes:
Algebrick::TypeCheck
Defined in:
lib/dynflow/director/queue_hash.rb

Instance Method Summary collapse

Constructor Details

#initialize(key_type = Object, value_type = Object) ⇒ QueueHash

Returns a new instance of QueueHash.



8
9
10
11
12
# File 'lib/dynflow/director/queue_hash.rb', line 8

def initialize(key_type = Object, value_type = Object)
  @key_type = key_type
  @value_type = value_type
  @stash = Hash.new { |hash, key| hash[key] = [] }
end

Instance Method Details

#clearObject



33
34
35
36
37
# File 'lib/dynflow/director/queue_hash.rb', line 33

def clear
  ret = @stash.dup
  @stash.clear
  ret
end

#empty?(key) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/dynflow/director/queue_hash.rb', line 29

def empty?(key)
  !present?(key)
end

#first(key) ⇒ Object



44
45
46
47
# File 'lib/dynflow/director/queue_hash.rb', line 44

def first(key)
  return nil if empty?(key)
  @stash[key].first
end

#present?(key) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/dynflow/director/queue_hash.rb', line 25

def present?(key)
  @stash.key?(key)
end

#push(key, value) ⇒ Object



14
15
16
17
18
# File 'lib/dynflow/director/queue_hash.rb', line 14

def push(key, value)
  Type! key, @key_type
  Type! value, @value_type
  @stash[key].push value
end

#shift(key) ⇒ Object



20
21
22
23
# File 'lib/dynflow/director/queue_hash.rb', line 20

def shift(key)
  return nil unless present? key
  @stash[key].shift.tap { @stash.delete(key) if @stash[key].empty? }
end

#size(key) ⇒ Object



39
40
41
42
# File 'lib/dynflow/director/queue_hash.rb', line 39

def size(key)
  return 0 if empty?(key)
  @stash[key].size
end