Class: Qfill::Popper

Inherits:
ListSet show all
Defined in:
lib/qfill/popper.rb

Instance Attribute Summary collapse

Attributes inherited from ListSet

#current_index, #queues

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ListSet

#[], #count_all_elements, #index_of, #reset!

Constructor Details

#initialize(*args) ⇒ Popper

Returns a new instance of Popper.



50
51
52
53
# File 'lib/qfill/popper.rb', line 50

def initialize(*args)
  super(*args)
  @total_elements = count_all_elements
end

Instance Attribute Details

#total_elementsObject

Returns the value of attribute total_elements.



39
40
41
# File 'lib/qfill/popper.rb', line 39

def total_elements
  @total_elements
end

Class Method Details

.from_array_of_hashes(array_of_hashes = []) ⇒ Object



42
43
44
45
46
47
# File 'lib/qfill/popper.rb', line 42

def from_array_of_hashes(array_of_hashes = [])
  args = array_of_hashes.map do |hash|
    Qfill::Origin.new(hash)
  end
  Qfill::Popper.new(*args)
end

Instance Method Details

#count_primary_elementsObject



97
98
99
# File 'lib/qfill/popper.rb', line 97

def count_primary_elements
  primary.inject(0) { |counter, queue| counter += queue.elements.length }
end

#current_listObject



59
60
61
# File 'lib/qfill/popper.rb', line 59

def current_list
  primary[current_index]
end

#next_objects!(list_name, n = 1) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/qfill/popper.rb', line 73

def next_objects!(list_name, n = 1)
  origin_list = self[list_name]
  if origin_list.elements.length >= n
    origin_list.elements.pop(n)
  else
    result = origin_list.elements.pop(n)
    while result.length < n && origin_list.has_backfill?
      secondary_list = self[origin_list.backfill]
      remaining = n - result.length
      result += secondary_list.elements.pop(remaining)
      origin_list = secondary_list
    end
    result
  end
end

#primaryObject



55
56
57
# File 'lib/qfill/popper.rb', line 55

def primary
  @primary ||= queues.reject { |x| x.backfill == true }
end

#primary_empty?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/qfill/popper.rb', line 89

def primary_empty?
  count_primary_elements.zero?
end

#set_next_as_current!Object



63
64
65
66
67
68
69
70
71
# File 'lib/qfill/popper.rb', line 63

def set_next_as_current!
  next_index = current_index + 1
  if (next_index) >= primary.length
    # If we have iterated through all the queues, then we reset
    reset!
  else
    self.current_index = next_index
  end
end

#totally_empty?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/qfill/popper.rb', line 93

def totally_empty?
  count_all_elements.zero?
end