Class: Qfill::Manager
- Inherits:
-
Object
- Object
- Qfill::Manager
- 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
-
#all_list_max ⇒ Object
Returns the value of attribute all_list_max.
-
#fill_count ⇒ Object
Returns the value of attribute fill_count.
-
#popper ⇒ Object
Returns the value of attribute popper.
-
#primary_list_total ⇒ Object
Returns the value of attribute primary_list_total.
-
#pusher ⇒ Object
Returns the value of attribute pusher.
-
#result ⇒ Object
Returns the value of attribute result.
-
#strategy_options ⇒ Object
Returns the value of attribute strategy_options.
Instance Method Summary collapse
- #each(&block) ⇒ Object
- #fill! ⇒ Object
-
#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.
- #fill_to_ratio! ⇒ Object
-
#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.
-
#initialize(options = {}) ⇒ Manager
constructor
A new instance of Manager.
- #is_full? ⇒ Boolean
- #remaining_to_fill ⇒ Object
- #strategy ⇒ Object
- #to_s ⇒ Object
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( = {}) unless [:strategy].nil? || STRATEGY_OPTIONS.include?([:strategy]) raise ArgumentError, "#{self.class}: strategy is optional, but must be one of #{STRATEGY_OPTIONS.inspect} if provided" end @fill_count = 0 @popper = [:popper] @pusher = [:pusher] @strategy_name = [:strategy] || :drain_to_limit # or :drain_to_empty or :sample @strategy_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 [:all_list_max] [[: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_max ⇒ Object
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_count ⇒ Object
Returns the value of attribute fill_count.
21 22 23 |
# File 'lib/qfill/manager.rb', line 21 def fill_count @fill_count end |
#popper ⇒ Object
Returns the value of attribute popper.
21 22 23 |
# File 'lib/qfill/manager.rb', line 21 def popper @popper end |
#primary_list_total ⇒ Object
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 |
#pusher ⇒ Object
Returns the value of attribute pusher.
21 22 23 |
# File 'lib/qfill/manager.rb', line 21 def pusher @pusher end |
#result ⇒ Object
Returns the value of attribute result.
21 22 23 |
# File 'lib/qfill/manager.rb', line 21 def result @result end |
#strategy_options ⇒ Object
Returns the value of attribute strategy_options.
21 22 23 |
# File 'lib/qfill/manager.rb', line 21 def @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
105 106 107 |
# File 'lib/qfill/manager.rb', line 105 def is_full? fill_count >= all_list_max end |
#remaining_to_fill ⇒ Object
91 92 93 |
# File 'lib/qfill/manager.rb', line 91 def remaining_to_fill primary_list_total - fill_count end |
#strategy ⇒ Object
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_s ⇒ Object
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 |