Skip to content

Commit

Permalink
pull out name to priority mapping to be its own mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
tkoar committed Apr 3, 2024
1 parent daf32f7 commit fcc4b9a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 29 deletions.
54 changes: 25 additions & 29 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_to_default_priority(names)
@names = names&.sort_by(&:last)&.to_h&.transform_values { |v| new(v) }
end

def alerts=(alerts)
Expand All @@ -96,50 +96,46 @@ def ranges
end
end

def names_to_priority
return names unless assign_at_midpoint?

new_names_to_priority = {}

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.to_i + 10]
midpoint = priority_value.to_i + ((next_priority_value.to_i - priority_value.to_i).to_f/2.0).ceil
new_names_to_priority[name] = new(midpoint)
else
new_names_to_priority[name] = new(priority_value)
end
end

new_names_to_priority.sort_by(&:last).to_h
end

private

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

def default_alerts
@names ? {} : DEFAULT_ALERTS
end

def respond_to_missing?(method_name, include_private = false)
names.key?(method_name) || super
names_to_priority.key?(method_name) || super
end

def method_missing(method_name, *args)
if names.key?(method_name) && args.none?
names[method_name]
if names_to_priority.key?(method_name) && args.none?
names_to_priority[method_name]
else
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 All @@ -149,7 +145,7 @@ def names_to_default_priority(names)

def initialize(value)
super()
value = self.class.names[value] if value.is_a?(Symbol)
value = self.class.names_to_priority[value] if value.is_a?(Symbol)
@value = value.to_i
end

Expand Down
38 changes: 38 additions & 0 deletions spec/delayed/priority_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,37 @@
expect(described_class.new(-123).name).to eq nil
end

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

it 'provides the name of the priority range' do
expect(described_class.new(0).name).to eq :interactive
expect(described_class.new(3).name).to eq :interactive
expect(described_class.new(10).name).to eq :user_visible
expect(described_class.new(29).name).to eq :eventual
expect(described_class.new(999).name).to eq :reporting
expect(described_class.new(-123).name).to eq nil
end
end

it 'supports initialization by symbol value' do
expect(described_class.new(:interactive)).to eq(0)
expect(described_class.new(:user_visible)).to eq(10)
expect(described_class.new(:eventual)).to eq(20)
expect(described_class.new(:reporting)).to eq(30)
end

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

it 'supports initialization by symbol value' do
expect(described_class.new(:interactive)).to eq(5)
expect(described_class.new(:user_visible)).to eq(15)
expect(described_class.new(:eventual)).to eq(25)
expect(described_class.new(:reporting)).to eq(35)
end
end

it "supports predicate ('?') methods" do
expect(described_class.new(0).interactive?).to eq true
expect(described_class.new(3)).to be_interactive
Expand All @@ -133,6 +157,20 @@
expect(described_class.new(-123).interactive?).to eq false
end

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

it "supports predicate ('?') methods" do
expect(described_class.new(0).interactive?).to eq true
expect(described_class.new(3)).to be_interactive
expect(described_class.new(3).user_visible?).to eq false
expect(described_class.new(10)).to be_user_visible
expect(described_class.new(29)).to be_eventual
expect(described_class.new(999)).to be_reporting
expect(described_class.new(-123).interactive?).to eq false
end
end

it 'supports alert threshold methods' do
described_class.alerts = {
interactive: { age: 77.seconds },
Expand Down

0 comments on commit fcc4b9a

Please sign in to comment.