Class: Qfill::Manager

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

Constant Summary collapse

STRATEGY_OPTIONS =
%i[drain_to_limit drain_to_empty sample time_slice].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Manager

Returns a new instance of Manager.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/qfill/manager.rb', line 31

def initialize(options = {})
  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

  @fill_count = 0

  @popper = options[:popper]
  @pusher = options[:pusher]
  @strategy_name = options[:strategy] || :drain_to_limit # or :drain_to_empty or :sample
  @strategy_options = options[:strategy_options]

  # Allow the strategy to define the pusher when not defined by user
  @pusher ||= strategy.default_pusher
  unless @popper && @pusher
    raise ArgumentError, "#{self.class}: popper and pusher (except where defined by the strategy) are required options for #{self.class}.new(options)"
  end

  # Provided by user, or defaults to the total number of elements in popper list set
  @all_list_max = if options[:all_list_max]
                    [options[:all_list_max],
                     popper.count_all_elements].min
                  else
                    popper.count_all_elements
                  end
  @primary_list_total = popper.count_primary_elements
end

Instance Attribute Details

#all_list_maxObject

Returns the value of attribute all_list_max.



21
22
23
# File 'lib/qfill/manager.rb', line 21

def all_list_max
  @all_list_max
end

#fill_countObject

Returns the value of attribute fill_count.



21
22
23
# File 'lib/qfill/manager.rb', line 21

def fill_count
  @fill_count
end

#popperObject

Returns the value of attribute popper.



21
22
23
# File 'lib/qfill/manager.rb', line 21

def popper
  @popper
end

#primary_list_totalObject

Returns the value of attribute primary_list_total.



21
22
23
# File 'lib/qfill/manager.rb', line 21

def primary_list_total
  @primary_list_total
end

#pusherObject

Returns the value of attribute pusher.



21
22
23
# File 'lib/qfill/manager.rb', line 21

def pusher
  @pusher
end

#resultObject

Returns the value of attribute result.



21
22
23
# File 'lib/qfill/manager.rb', line 21

def result
  @result
end

#strategy_optionsObject

Returns the value of attribute strategy_options.



21
22
23
# File 'lib/qfill/manager.rb', line 21

def strategy_options
  @strategy_options
end

Instance Method Details

#each(&block) ⇒ Object



109
110
111
112
113
114
# File 'lib/qfill/manager.rb', line 109

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!

  pusher.each(&block)
end

#fill!Object



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

def fill!
  while !is_full? && !popper.primary_empty? && (self.result = pusher.current_list)
    strategy.on_fill!
    fill_to_ratio!
    pusher.set_next_as_current!
    result.elements.shuffle! if result.shuffle
  end
end

#fill_according_to_list_ratios!Object

Go through the queues this result should be filled from and push elements from them onto the current result list.



96
97
98
# File 'lib/qfill/manager.rb', line 96

def fill_according_to_list_ratios!
  strategy.fill_according_to_list_ratios!
end

#fill_to_ratio!Object



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

def fill_to_ratio!
  strategy.result_max!
  if result.list_ratios.empty?
    fill_up_to_ratio!
  else
    fill_according_to_list_ratios!
  end
end

#fill_up_to_ratio!Object

Go through the primary (non backfill) queues in the popper and push elements from them onto the current result list.



101
102
103
# File 'lib/qfill/manager.rb', line 101

def fill_up_to_ratio!
  strategy.fill_up_to_ratio!
end

#is_full?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/qfill/manager.rb', line 105

def is_full?
  fill_count >= all_list_max
end

#remaining_to_fillObject



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

def remaining_to_fill
  primary_list_total - fill_count
end

#strategyObject



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

def strategy
  @strategy ||= case @strategy_name
                when :drain_to_empty
                  Qfill::Strategy::DrainToEmpty.new(self)
                when :drain_to_limit
                  Qfill::Strategy::DrainToLimit.new(self)
                when :sample
                  Qfill::Strategy::Sample.new(self)
                when :time_slice
                  Qfill::Strategy::TimeSlice.new(self)
                end
end

#to_sObject



116
117
118
# File 'lib/qfill/manager.rb', line 116

def to_s
  "[#{strategy_name}][Result Max:#{result.max}][All Max:#{all_list_max}][Current Max:#{result.max}][Filled:#{fill_count}][Primary #:#{popper.count_primary_elements}]"
end