-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow configuring default priority value at midpoint of range #39
Changes from 8 commits
062cd24
daf32f7
fcc4b9a
487a24f
e1134ed
75c6ac8
4ae6564
f15bd93
f3aac3e
8436715
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -57,6 +57,8 @@ class Priority < Numeric | |||||||||||||||||||||||||||||
}.freeze | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
class << self | ||||||||||||||||||||||||||||||
attr_writer :assign_at_midpoint | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def names | ||||||||||||||||||||||||||||||
@names || default_names | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
@@ -70,6 +72,7 @@ def names=(names) | |||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
@ranges = nil | ||||||||||||||||||||||||||||||
@alerts = nil | ||||||||||||||||||||||||||||||
@names_to_priority = nil | ||||||||||||||||||||||||||||||
@names = names&.sort_by(&:last)&.to_h&.transform_values { |v| new(v) } | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
@@ -82,12 +85,25 @@ 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 ranges | ||||||||||||||||||||||||||||||
@ranges ||= names.zip(names.except(names.keys.first)).each_with_object({}) do |((name, lower), (_, upper)), obj| | ||||||||||||||||||||||||||||||
obj[name] = (lower...(upper || Float::INFINITY)) | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def names_to_priority | ||||||||||||||||||||||||||||||
tkoar marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
@names_to_priority ||= | ||||||||||||||||||||||||||||||
if assign_at_midpoint? | ||||||||||||||||||||||||||||||
tkoar marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
names_to_midpoint_priority | ||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||
names | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
private | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def default_names | ||||||||||||||||||||||||||||||
|
@@ -98,13 +114,23 @@ def default_alerts | |||||||||||||||||||||||||||||
@names ? {} : DEFAULT_ALERTS | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
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 + 5)) | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def midpoint(low, high) | ||||||||||||||||||||||||||||||
low + ((high - low).to_d / 2).ceil | ||||||||||||||||||||||||||||||
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 | ||||||||||||||||||||||||||||||
|
@@ -118,7 +144,7 @@ def method_missing(method_name, *args) | |||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
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) | ||||||||||||||||||||||||||||||
tkoar marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
@value = value.to_i | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
@@ -147,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 | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking about what it meant to always call
I would expect that to return And also these examples: Priority.new(10) + 1 Should also probably return But I'm not sure what this should return: Priority.new(10) + 0.9 (Should it round to Maybe we don't need to handle that last case for now. So I think that leaves us with this:
Suggested change
Though we could do what we do in other = other.to_i if other.is_a?(self.class) WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yeah that's a good call! We should return a Priority instance, since that is the caller. Hmm yeah the math complexities are interesting. I would say we probably kick the can on dealing with floats? Right now the |
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
private | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def respond_to_missing?(method_name, include_private = false) | ||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, this makes sense. At some point we should consider making this the default, but opt-in is good for now.