Skip to content

Commit

Permalink
allows configuring class variable 'assign_at_midpoint' to default nam…
Browse files Browse the repository at this point in the history
…ed priorities to a midpoint-within-range value'
  • Loading branch information
tkoar committed Apr 3, 2024
1 parent 77b97e9 commit 062cd24
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
36 changes: 34 additions & 2 deletions lib/delayed/priority.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def names=(names)

@ranges = nil
@alerts = nil
@names = names&.sort_by(&:last)&.to_h&.transform_values { |v| new(v) }
@names = names_to_default_priority(names)
end

def alerts=(alerts)
Expand All @@ -82,6 +82,15 @@ def alerts=(alerts)
@alerts = alerts&.sort_by { |k, _| names.keys.index(k) }&.to_h
end

def assign_at_midpoint?
@assign_at_midpoint || false
end

def assign_at_midpoint=(assign_at_midpoint=false)
@assign_at_midpoint = assign_at_midpoint
@assign_at_midpoint
end

def ranges
@ranges ||= names.zip(names.except(names.keys.first)).each_with_object({}) do |((name, lower), (_, upper)), obj|
obj[name] = (lower...(upper || Float::INFINITY))
Expand All @@ -91,7 +100,7 @@ def ranges
private

def default_names
@default_names ||= DEFAULT_NAMES.transform_values { |v| new(v) }
@default_names ||= names_to_default_priority(DEFAULT_NAMES)
end

def default_alerts
Expand All @@ -109,6 +118,29 @@ def method_missing(method_name, *args)
super
end
end

def names_to_default_priority(names)
return unless names

names_to_priority = {}

if assign_at_midpoint?
sorted_priorities_by_name = names&.sort_by(&:last)
sorted_priorities_by_name.each.with_index do |(name, priority_value), index|
if assign_at_midpoint?
(_, next_priority_value) = sorted_priorities_by_name[index+1] || [nil, priority_value + 10]
midpoint = priority_value + ((next_priority_value - priority_value).to_f/2.0).ceil
names_to_priority[name] = new(midpoint)
else
names_to_priority[name] = new(priority_value)
end
end
else
names_to_priority = names.transform_values { |v| new(v) }
end

names_to_priority.sort_by(&:last).to_h
end
end

attr_reader :value
Expand Down
23 changes: 23 additions & 0 deletions spec/delayed/priority_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
RSpec.describe Delayed::Priority do
let(:custom_names) { nil }
let(:custom_alerts) { nil }
let(:assign_at_midpoint) { nil }

around do |example|
described_class.assign_at_midpoint = assign_at_midpoint
described_class.names = custom_names
described_class.alerts = custom_alerts
example.run
Expand Down Expand Up @@ -43,6 +45,17 @@
expect(described_class.reporting).to eq 30
end

context 'when assign_at_midpoint is set to true' do
let(:assign_at_midpoint) { true }

it 'returns the midpoint value' do
expect(described_class.interactive).to eq 5
expect(described_class.user_visible).to eq 15
expect(described_class.eventual).to eq 25
expect(described_class.reporting).to eq 35
end
end

context 'when customized to high, medium, low' do
let(:custom_names) { { high: 0, medium: 100, low: 500 } }

Expand Down Expand Up @@ -81,6 +94,16 @@
)
end
end

context 'when assign_at_midpoint is set to true' do
let(:assign_at_midpoint) { true }

it 'returns the midpoint value' do
expect(described_class.high).to eq 50
expect(described_class.medium).to eq 300
expect(described_class.low).to eq 505
end
end
end
end

Expand Down

0 comments on commit 062cd24

Please sign in to comment.