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

#[], #get_total_elements, #reset!

Constructor Details

#initialize(*args) ⇒ Popper

Returns a new instance of Popper.



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

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

Instance Attribute Details

#total_elementsObject

Returns the value of attribute total_elements.



35
36
37
# File 'lib/qfill/popper.rb', line 35

def total_elements
  @total_elements
end

Class Method Details

.from_array_of_hashes(array_of_hashes = []) ⇒ Object



76
77
78
79
80
81
# File 'lib/qfill/popper.rb', line 76

def self.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

#current_listObject



46
47
48
# File 'lib/qfill/popper.rb', line 46

def current_list
  self.primary[self.current_index]
end

#get_primary_elementsObject



91
92
93
# File 'lib/qfill/popper.rb', line 91

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

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/qfill/popper.rb', line 60

def next_objects!(list_name, n = 1)
  origin_list = self[list_name]
  if origin_list.elements.length >= n
    return 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
    return result
  end
end

#primaryObject



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

def primary
  @primary ||= self.queues.select {|x| x.backfill != true}
end

#primary_empty?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/qfill/popper.rb', line 83

def primary_empty?
  self.get_primary_elements == 0
end

#set_next_as_current!Object



50
51
52
53
54
55
56
57
58
# File 'lib/qfill/popper.rb', line 50

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

#totally_empty?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/qfill/popper.rb', line 87

def totally_empty?
  self.get_total_elements == 0
end