Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Make raise_exceptions an option in ChronicDuration

Christophe Bliard157c57e84262
diff --git a/app/services/duration_converter.rb b/app/services/duration_converter.rb
index 1df40065e91..e2efe5a771e 100644
--- a/app/services/duration_converter.rb
+++ b/app/services/duration_converter.rb
@@ -78,23 +78,7 @@ class DurationConverter
def parse(duration_string)
return nil if duration_string.blank?
- # Assume the next logical unit to allow users to write
- # durations such as "2h 1" assuming "1" is "1 minute"
- last_unit_in_string = duration_string.scan(/[a-zA-Z]+/)
- .last
- default_unit = if last_unit_in_string
- last_unit_in_string
- .then { |last_unit| UNIT_ABBREVIATION_MAP[last_unit.downcase] }
- .then { |last_unit| NEXT_UNIT_MAP[last_unit] }
- else
- "hours"
- end
-
- ChronicDuration.raise_exceptions = true
- ChronicDuration.parse(duration_string,
- keep_zero: true,
- default_unit:,
- **duration_length_options) / 3600.to_f
+ do_parse(duration_string)
end
def valid?(duration)
@@ -130,7 +114,7 @@ class DurationConverter
number >= 0
else
begin
- internal_parse(duration_string)
+ do_parse(duration_string)
true
rescue ChronicDuration::DurationParseError
false
@@ -138,7 +122,7 @@ class DurationConverter
end
end
- def internal_parse(duration_string)
+ def do_parse(duration_string)
# Assume the next logical unit to allow users to write
# durations such as "2h 1" assuming "1" is "1 minute"
last_unit_in_string = duration_string.scan(/[a-zA-Z]+/)
diff --git a/lib/chronic_duration.rb b/lib/chronic_duration.rb
index a26e2eb0807..94500124811 100644
--- a/lib/chronic_duration.rb
+++ b/lib/chronic_duration.rb
@@ -74,7 +74,7 @@ module ChronicDuration
# return an integer (or float, if fractions of a
# second are input)
def parse(string, opts = {})
- result = calculate_from_words(cleanup(string), opts)
+ result = calculate_from_words(cleanup(string, opts), opts)
!opts[:keep_zero] && result == 0 ? nil : result
end
@@ -247,11 +247,11 @@ module ChronicDuration
val
end
- def cleanup(string)
+ def cleanup(string, opts = {})
res = string.downcase
res = filter_by_type(res)
res = res.gsub(float_matcher) { |n| " #{n} " }.squeeze(" ").strip
- filter_through_white_list(res)
+ filter_through_white_list(res, opts)
end
def convert_to_number(string)
@@ -308,7 +308,7 @@ module ChronicDuration
# Get rid of unknown words and map found
# words to defined time units
- def filter_through_white_list(string)
+ def filter_through_white_list(string, opts)
res = []
string.split.each do |word|
if word&.match?(float_matcher)
@@ -318,7 +318,7 @@ module ChronicDuration
stripped_word = word.strip.gsub(/^,/, "").gsub(/,$/, "")
if mappings.has_key?(stripped_word)
res << mappings[stripped_word]
- elsif !join_words.include?(stripped_word) and ChronicDuration.raise_exceptions # rubocop:disable Rails/NegateInclude
+ elsif !join_words.include?(stripped_word) and opts.fetch(:raise_exceptions, ChronicDuration.raise_exceptions) # rubocop:disable Rails/NegateInclude
raise DurationParseError, "An invalid word #{word.inspect} was used in the string to be parsed."
end
end
diff --git a/spec/lib/chronic_duration_spec.rb b/spec/lib/chronic_duration_spec.rb
index 32e9ebc6563..b47039765c5 100644
--- a/spec/lib/chronic_duration_spec.rb
+++ b/spec/lib/chronic_duration_spec.rb
@@ -79,6 +79,19 @@ RSpec.describe ChronicDuration do
it "raises with ChronicDuration::DurationParseError" do
expect { described_class.parse("23 gobblygoos") }.to raise_error(ChronicDuration::DurationParseError)
end
+
+ context "when passing `raise_exceptions: false` as an option" do
+ it "overrides @@raise_exception and returns nil" do
+ expect(described_class.parse("gobblygoos", raise_exceptions: false)).to be_nil
+ end
+ end
+ end
+
+ context "when passing `raise_exceptions: true` as an option" do
+ it "overrides @@raise_exception and raises with ChronicDuration::DurationParseError" do
+ expect { described_class.parse("23 gobblygoos", raise_exceptions: true) }
+ .to raise_error(ChronicDuration::DurationParseError)
+ end
end
end