Class: Qfill::Pusher

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

Instance Attribute Summary

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) ⇒ Pusher

Returns a new instance of Pusher.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/qfill/pusher.rb', line 48

def initialize(*args)
  super(*args)
  with_ratio = queues.map(&:ratio).compact
  ratio_to_split = (1 - with_ratio.sum)
  # if ratio_to_split < 0
  #  raise ArgumentError, "#{self.class}: mismatched ratios for queues #{with_ratio.join(' + ')} must not total more than 1"
  # end
  num_without_ratio = queues.length - with_ratio.length
  if num_without_ratio.positive? && ratio_to_split <= 1
    equal_portion = ratio_to_split / num_without_ratio
    queues.each do |queue|
      next unless queue.ratio.nil?

      queue.tap do |q|
        q.ratio = equal_portion
      end
    end
  end
end

Class Method Details

.from_array_of_hashes(array_of_hashes = []) ⇒ Object



82
83
84
85
86
87
# File 'lib/qfill/pusher.rb', line 82

def self.from_array_of_hashes(array_of_hashes = [])
  args = array_of_hashes.map do |hash|
    Qfill::Result.new(hash)
  end
  Qfill::Pusher.new(*args)
end

Instance Method Details

#current_listObject



68
69
70
# File 'lib/qfill/pusher.rb', line 68

def current_list
  queues[current_index]
end

#each(&block) ⇒ Object



97
98
99
100
101
102
# File 'lib/qfill/pusher.rb', line 97

def each(&block)
  # NOTE: on magic: http://blog.arkency.com/2014/01/ruby-to-enum-for-enumerator/
  return enum_for(:each) unless block # Sparkling magic!

  queues.each(&block)
end

#more_to_fill?Boolean

Returns:

  • (Boolean)


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

def more_to_fill?
  !queues.reject(&:is_full?).empty?
end

#next_to_fillObject



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

def next_to_fill
  queues.reject(&:is_full?).first
end

#set_next_as_current!Object



72
73
74
75
76
77
78
79
80
# File 'lib/qfill/pusher.rb', line 72

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