lib/qfill/result.rb in qfill-0.0.3 vs lib/qfill/result.rb in qfill-0.0.4
- old
+ new
@@ -1,15 +1,20 @@
+# :preferred is used for :draim_to_empty
+# :ratio is used for the other strategies
# Qfill::Result.new(:name => "Best Results",
# :filter => filter3,
# :ratio => 0.5,
# :list_ratios => {
# "High List" => 0.4,
# "Medium List" => 0.2,
-# "Low List" => 0.4 } )
+# "Low List" => 0.4 },
+# :preferred => ["High List", "Medium List"]
+# )
module Qfill
class Result < Qfill::List
- attr_accessor :ratio, :list_ratios, :fill_tracker, :fill_count, :validate, :filter, :current_list_ratio_index, :max
+ attr_accessor :ratio, :list_ratios, :fill_tracker, :fill_count, :current_count, :validate, :current_list_ratio_index, :max,
+ :strategy, :shuffle, :preferred, :preferred_potential, :preferred_potential_ratio, :max_tracker
def self.get_limit_from_max_and_ratio(all_list_max, ratio)
limit = (all_list_max * ratio).round(0)
# If we rounded down to zero we have to keep at least one.
# This is because with small origin sets all ratios might round down to 0.
@@ -25,14 +30,21 @@
with_ratio = self.list_ratio_as_array.map {|tuple| tuple[1]}.compact
ratio_leftover = (1 - with_ratio.inject(0, :+))
if ratio_leftover < 0
raise ArgumentError, "#{self.class}: invalid list_ratios for queue '#{self.name}'. List Ratios (#{with_ratio.join(' + ')}) must not total more than 1"
end
- @ratio = options[:ratio]
+ @ratio = options[:ratio] || 1
@max = 0
+ @preferred = options[:preferred] # Used by :drain_to_empty and :drain_to_limit
+ @preferred_potential = 0
+ @preferred_potential_ratio = 0
+ @strategy = options[:strategy] # nil, :drain_to_limit, :drain_to_empty or :sample
@fill_tracker = {}
+ @max_tracker = {}
@fill_count = 0
+ @current_count = 0
+ @shuffle = options[:shuffle] || false
@current_list_ratio_index = 0 # Used by :sample strategy
@validate = self.use_validation?
end
def list_ratio_full?(list_name, max_from_list)
@@ -40,10 +52,11 @@
end
def push(objects, list_name)
self.validate!(list_name)
added = 0
+ self.fill_tracker[list_name] ||= 0
objects.each do |object|
if self.allow?(object, list_name)
self.bump_fill_tracker!(list_name)
self.add!(object)
added += 1
@@ -66,13 +79,13 @@
# If there is a filter, then it must return true to proceed
self.filter.run(object, list_name)
end
def bump_fill_tracker!(list_name)
- self.fill_tracker[list_name] ||= 0
self.fill_tracker[list_name] += 1
self.fill_count += 1
+ self.current_count += 1
end
# Does the queue being pushed into match one of the list_ratios
def valid?(list_name)
self.list_ratios.has_key?(list_name)
@@ -105,16 +118,17 @@
end
end
def reset!
self.current_list_ratio_index = 0
+ self.current_count = 0
end
def is_full?
- self.fill_count >= self.max
+ self.current_count >= self.max
end
def to_s
- "Qfill::Result: ratio: #{self.ratio}, list_ratios: #{self.list_ratios}, fill_tracker: #{self.fill_tracker}, fill_count: #{self.fill_count}, filter: #{!!self.filter ? 'Yes' : 'No'}, current_list_ratio_index: #{self.current_list_ratio_index}, max: #{self.max}"
+ "Qfill::Result: ratio: #{self.ratio}, list_ratios: #{self.list_ratios}, fill_tracker: #{self.fill_tracker}, fill_count: #{self.fill_count}, current_count: #{self.current_count}, filter: #{!!self.filter ? 'Yes' : 'No'}, current_list_ratio_index: #{self.current_list_ratio_index}, max: #{self.max}"
end
end
end