Class: Qfill::Result

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

Instance Attribute Summary collapse

Attributes inherited from List

#elements, #filter, #name

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Result

Returns a new instance of Result.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/qfill/result.rb', line 27

def initialize(options = {})
  super(options)
  @list_ratios = options[:list_ratios] || {}
  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] || 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

Instance Attribute Details

#current_countObject

Returns the value of attribute current_count.



14
15
16
# File 'lib/qfill/result.rb', line 14

def current_count
  @current_count
end

#current_list_ratio_indexObject

Returns the value of attribute current_list_ratio_index.



14
15
16
# File 'lib/qfill/result.rb', line 14

def current_list_ratio_index
  @current_list_ratio_index
end

#fill_countObject

Returns the value of attribute fill_count.



14
15
16
# File 'lib/qfill/result.rb', line 14

def fill_count
  @fill_count
end

#fill_trackerObject

Returns the value of attribute fill_tracker.



14
15
16
# File 'lib/qfill/result.rb', line 14

def fill_tracker
  @fill_tracker
end

#list_ratiosObject

Returns the value of attribute list_ratios.



14
15
16
# File 'lib/qfill/result.rb', line 14

def list_ratios
  @list_ratios
end

#maxObject

Returns the value of attribute max.



14
15
16
# File 'lib/qfill/result.rb', line 14

def max
  @max
end

#max_trackerObject

Returns the value of attribute max_tracker.



14
15
16
# File 'lib/qfill/result.rb', line 14

def max_tracker
  @max_tracker
end

#preferredObject

Returns the value of attribute preferred.



14
15
16
# File 'lib/qfill/result.rb', line 14

def preferred
  @preferred
end

#preferred_potentialObject

Returns the value of attribute preferred_potential.



14
15
16
# File 'lib/qfill/result.rb', line 14

def preferred_potential
  @preferred_potential
end

#preferred_potential_ratioObject

Returns the value of attribute preferred_potential_ratio.



14
15
16
# File 'lib/qfill/result.rb', line 14

def preferred_potential_ratio
  @preferred_potential_ratio
end

#ratioObject

Returns the value of attribute ratio.



14
15
16
# File 'lib/qfill/result.rb', line 14

def ratio
  @ratio
end

#shuffleObject

Returns the value of attribute shuffle.



14
15
16
# File 'lib/qfill/result.rb', line 14

def shuffle
  @shuffle
end

#strategyObject

Returns the value of attribute strategy.



14
15
16
# File 'lib/qfill/result.rb', line 14

def strategy
  @strategy
end

#validateObject

Returns the value of attribute validate.



14
15
16
# File 'lib/qfill/result.rb', line 14

def validate
  @validate
end

Class Method Details

.get_limit_from_max_and_ratio(all_list_max, ratio) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/qfill/result.rb', line 17

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.
  if limit == 0
    limit += 1
  end
  limit
end

Instance Method Details

#add!(object) ⇒ Object



73
74
75
# File 'lib/qfill/result.rb', line 73

def add!(object)
  self.elements << object
end

#allow?(object, list_name) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
# File 'lib/qfill/result.rb', line 77

def allow?(object, list_name)
  !self.filter.respond_to?(:call) ||
    # If there is a filter, then it must return true to proceed
    self.filter.run(object, list_name)
end

#bump_fill_tracker!(list_name) ⇒ Object



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

def bump_fill_tracker!(list_name)
  self.fill_tracker[list_name] += 1
  self.fill_count += 1
  self.current_count += 1
end

#current_list_ratioObject



107
108
109
# File 'lib/qfill/result.rb', line 107

def current_list_ratio
  self.list_ratio_as_array[self.current_list_ratio_index]
end

#is_full?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/qfill/result.rb', line 126

def is_full?
  self.current_count >= self.max
end

#list_ratio_as_arrayObject



102
103
104
105
# File 'lib/qfill/result.rb', line 102

def list_ratio_as_array
  # [["high",0.4],["medium",0.4],["low",0.2]]
  @list_ratio_as_array ||= self.list_ratios.to_a
end

#list_ratio_full?(list_name, max_from_list) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/qfill/result.rb', line 50

def list_ratio_full?(list_name, max_from_list)
  self.fill_tracker[list_name] >= max_from_list
end


69
70
71
# File 'lib/qfill/result.rb', line 69

def print(list_name)
  puts "Added to #{list_name}.\nResult List #{self.name} now has #{self.elements.length} total objects.\nSources:\n #{self.fill_tracker.inspect} "
end

#push(objects, list_name) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/qfill/result.rb', line 54

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
      #self.print(list_name)
    end
  end
  return added
end

#reset!Object



121
122
123
124
# File 'lib/qfill/result.rb', line 121

def reset!
  self.current_list_ratio_index = 0
  self.current_count = 0
end

#set_next_as_current!Object



111
112
113
114
115
116
117
118
119
# File 'lib/qfill/result.rb', line 111

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

#to_sObject



130
131
132
# File 'lib/qfill/result.rb', line 130

def to_s
  "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

#use_validation?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/qfill/result.rb', line 98

def use_validation?
  !self.list_ratios.empty?
end

#valid?(list_name) ⇒ Boolean

Does the queue being pushed into match one of the list_ratios

Returns:

  • (Boolean)


90
91
92
# File 'lib/qfill/result.rb', line 90

def valid?(list_name)
  self.list_ratios.has_key?(list_name)
end

#validate!(list_name) ⇒ Object

Raises:

  • (ArgumentError)


94
95
96
# File 'lib/qfill/result.rb', line 94

def validate!(list_name)
  raise ArgumentError, "#{self.class}: #{list_name} is an invalid list_name.  Valid list_names are: #{self.list_ratios.keys}" if self.validate && !self.valid?(list_name)
end