Skip to content

Commit

Permalink
adds methods to add and subtract priorities without type coercion
Browse files Browse the repository at this point in the history
  • Loading branch information
tkoar committed Apr 5, 2024
1 parent 4ae6564 commit f15bd93
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
delayed (0.5.3)
delayed (0.5.4)
activerecord (>= 5.2)
concurrent-ruby

Expand Down
12 changes: 10 additions & 2 deletions lib/delayed/priority.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ def default_alerts
def names_to_midpoint_priority
names.each_cons(2).to_h { |(name, priority_value), (_, next_priority_value)|
[name, new(midpoint(priority_value, next_priority_value))]
}.merge(names.keys.last => new(names.values.last.to_i + 5))
}.merge(names.keys.last => new(names.values.last + 5))
end

def midpoint(low, high)
low.to_i + ((high.to_i - low.to_i).to_d / 2).ceil
low + ((high - low).to_d / 2).ceil
end

def respond_to_missing?(method_name, include_private = false)
Expand Down Expand Up @@ -173,6 +173,14 @@ def <=>(other)
to_i <=> other
end

def -(other)
to_i - other.to_i
end

def +(other)
to_i + other.to_i
end

private

def respond_to_missing?(method_name, include_private = false)
Expand Down
9 changes: 9 additions & 0 deletions spec/delayed/priority_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,13 @@
].sort,
).to eq [-13, 3, 5, 40]
end

it 'supports addition and subtraction' do
expect(described_class.new(0) + 10).to eq(10)
expect(10 + described_class.new(5)).to eq(15)
expect(described_class.new(0) + described_class.new(33)).to eq(33)
expect(described_class.new(10) - 5).to eq(5)
expect(15 - described_class.new(10)).to eq(5)
expect(described_class.new(5) - described_class.new(15)).to eq(-10)
end
end

0 comments on commit f15bd93

Please sign in to comment.