Class: Qfill::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/qfill/manager.rb

Constant Summary collapse

STRATEGY_OPTIONS =
[:drain, :sample]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Manager

Returns a new instance of Manager.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/qfill/manager.rb', line 12

def initialize(options = {})
  unless options[:popper] && options[:pusher]
    raise ArgumentError, "#{self.class}: popper and pusher are required options for #{self.class}.new(options)"
  end
  unless options[:strategy].nil? || STRATEGY_OPTIONS.include?(options[:strategy])
    raise ArgumentError, "#{self.class}: strategy is optional, but must be one of #{STRATEGY_OPTIONS.inspect} if provided"
  end
  @popper = options[:popper]
  @pusher = options[:pusher]
  # Provided by user, or defaults to the total number of primary elements in popper list set
  @all_list_max = options[:all_list_max] ? [options[:all_list_max], self.popper.get_primary_elements].min : self.popper.get_primary_elements
  @fill_count = 0
  @strategy = options[:strategy] || :drain # or :sample
end

Instance Attribute Details

#all_list_maxObject

Returns the value of attribute all_list_max.



8
9
10
# File 'lib/qfill/manager.rb', line 8

def all_list_max
  @all_list_max
end

#fill_countObject

Returns the value of attribute fill_count.



8
9
10
# File 'lib/qfill/manager.rb', line 8

def fill_count
  @fill_count
end

#popperObject

Returns the value of attribute popper.



8
9
10
# File 'lib/qfill/manager.rb', line 8

def popper
  @popper
end

#pusherObject

Returns the value of attribute pusher.



8
9
10
# File 'lib/qfill/manager.rb', line 8

def pusher
  @pusher
end

#strategyObject

Returns the value of attribute strategy.



8
9
10
# File 'lib/qfill/manager.rb', line 8

def strategy
  @strategy
end

Instance Method Details

#fill!Object



27
28
29
30
31
32
# File 'lib/qfill/manager.rb', line 27

def fill!
  while !is_full? && !self.popper.primary_empty? && (result = self.pusher.current_list)
    self.fill_to_ratio!(result, self.all_list_max)
    self.pusher.set_next_as_current!
  end
end

#fill_according_to_list_ratios!(result) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/qfill/manager.rb', line 43

def fill_according_to_list_ratios!(result)
  added = 0
  if self.strategy == :drain
    result.list_ratios.each do |list_name, list_ratio|
      #puts "fill_according_to_list_ratios!, :drain, #{list_name}: Primary remaining => #{self.popper.get_primary_elements}"
      max_from_list = Qfill::Result.get_limit_from_max_and_ratio(result.max, list_ratio)
      array_to_push = self.popper.next_objects!(list_name, max_from_list)
      added = result.push(array_to_push, list_name)
    end
    self.fill_count += added
  elsif self.strategy == :sample
    while !is_full? && !result.is_full? && !self.popper.totally_empty? && (list_ratio_tuple = result.current_list_ratio)
      #puts "fill_according_to_list_ratios!, :sample, #{list_ratio_tuple[0]}: Primary remaining => #{self.popper.get_primary_elements}"
      max_from_list = Qfill::Result.get_limit_from_max_and_ratio(result.max, list_ratio_tuple[1])
      array_to_push = self.popper.next_objects!(list_ratio_tuple[0], max_from_list)
      added = result.push(array_to_push, list_ratio_tuple[0])
      self.fill_count += added
      result.set_next_as_current!
    end
  end
end

#fill_to_ratio!(result, all_list_max) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/qfill/manager.rb', line 34

def fill_to_ratio!(result, all_list_max)
  result.max = Qfill::Result.get_limit_from_max_and_ratio(all_list_max, result.ratio)
  if !result.list_ratios.empty?
    self.fill_according_to_list_ratios!(result)
  else
    self.fill_up_to_ratio!(result)
  end
end

#fill_up_to_ratio!(result) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/qfill/manager.rb', line 65

def fill_up_to_ratio!(result)
  ratio = 1.0 / self.popper.primary.length
  max_from_list = Qfill::Result.get_limit_from_max_and_ratio(result.max, ratio)
  added = 0
  if self.strategy == :drain
    self.popper.primary.each do |queue|
      #puts "fill_up_to_ratio!, :drain max #{max_from_list}, #{queue.name}: Primary remaining => #{self.popper.get_primary_elements}"
      array_to_push = self.popper.next_objects!(queue.name, max_from_list)
      added = result.push(array_to_push, queue.name)
    end
    self.fill_count += added
  elsif self.strategy == :sample
    while !is_full? && !result.is_full? && !self.popper.totally_empty? && (origin_list = self.popper.current_list)
      #puts "fill_up_to_ratio!, :sample max #{max_from_list}, #{origin_list.name}: Primary remaining => #{self.popper.get_primary_elements}"
      array_to_push = self.popper.next_objects!(origin_list.name, max_from_list)
      added = result.push(array_to_push, origin_list.name)
      self.fill_count += added
      self.popper.set_next_as_current!
    end
  end
end

#is_full?Boolean

Returns:

  • (Boolean)


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

def is_full?
  self.fill_count >= self.all_list_max
end