Class: Qfill::Result

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

Instance Attribute Summary collapse

Attributes inherited from List

#elements, #name

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Result

Returns a new instance of Result.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/qfill/result.rb', line 22

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]
  @max = 0
  @fill_tracker = {}
  @fill_count = 0
  @current_list_ratio_index = 0 # Used by :sample strategy
  @validate = self.use_validation?
end

Instance Attribute Details

#current_list_ratio_indexObject

Returns the value of attribute current_list_ratio_index.



10
11
12
# File 'lib/qfill/result.rb', line 10

def current_list_ratio_index
  @current_list_ratio_index
end

#fill_countObject

Returns the value of attribute fill_count.



10
11
12
# File 'lib/qfill/result.rb', line 10

def fill_count
  @fill_count
end

#fill_trackerObject

Returns the value of attribute fill_tracker.



10
11
12
# File 'lib/qfill/result.rb', line 10

def fill_tracker
  @fill_tracker
end

#filterObject

Returns the value of attribute filter.



10
11
12
# File 'lib/qfill/result.rb', line 10

def filter
  @filter
end

#list_ratiosObject

Returns the value of attribute list_ratios.



10
11
12
# File 'lib/qfill/result.rb', line 10

def list_ratios
  @list_ratios
end

#maxObject

Returns the value of attribute max.



10
11
12
# File 'lib/qfill/result.rb', line 10

def max
  @max
end

#ratioObject

Returns the value of attribute ratio.



10
11
12
# File 'lib/qfill/result.rb', line 10

def ratio
  @ratio
end

#validateObject

Returns the value of attribute validate.



10
11
12
# File 'lib/qfill/result.rb', line 10

def validate
  @validate
end

Class Method Details

.get_limit_from_max_and_ratio(all_list_max, ratio) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/qfill/result.rb', line 12

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



60
61
62
# File 'lib/qfill/result.rb', line 60

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

#allow?(object, list_name) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
# File 'lib/qfill/result.rb', line 64

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



70
71
72
73
74
# File 'lib/qfill/result.rb', line 70

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

#current_list_ratioObject



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

def current_list_ratio
  self.list_ratio_as_array[self.current_list_ratio_index]
end

#is_full?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/qfill/result.rb', line 112

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

#list_ratio_as_arrayObject



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

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)


38
39
40
# File 'lib/qfill/result.rb', line 38

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


56
57
58
# File 'lib/qfill/result.rb', line 56

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



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/qfill/result.rb', line 42

def push(objects, list_name)
  self.validate!(list_name)
  added = 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



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

def reset!
  self.current_list_ratio_index = 0
end

#set_next_as_current!Object



98
99
100
101
102
103
104
105
106
# File 'lib/qfill/result.rb', line 98

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



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

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}"
end

#use_validation?Boolean

Returns:

  • (Boolean)


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

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)


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

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

#validate!(list_name) ⇒ Object

Raises:

  • (ArgumentError)


81
82
83
# File 'lib/qfill/result.rb', line 81

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