aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/duration/iso8601_parser.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support/duration/iso8601_parser.rb')
-rw-r--r--activesupport/lib/active_support/duration/iso8601_parser.rb131
1 files changed, 66 insertions, 65 deletions
diff --git a/activesupport/lib/active_support/duration/iso8601_parser.rb b/activesupport/lib/active_support/duration/iso8601_parser.rb
index 07af58ad99..e96cb8e883 100644
--- a/activesupport/lib/active_support/duration/iso8601_parser.rb
+++ b/activesupport/lib/active_support/duration/iso8601_parser.rb
@@ -1,4 +1,5 @@
-require 'strscan'
+require "strscan"
+require "active_support/core_ext/regexp"
module ActiveSupport
class Duration
@@ -11,8 +12,8 @@ module ActiveSupport
class ParsingError < ::ArgumentError; end
PERIOD_OR_COMMA = /\.|,/
- PERIOD = '.'.freeze
- COMMA = ','.freeze
+ PERIOD = ".".freeze
+ COMMA = ",".freeze
SIGN_MARKER = /\A\-|\+|/
DATE_MARKER = /P/
@@ -20,8 +21,8 @@ module ActiveSupport
DATE_COMPONENT = /(\-?\d+(?:[.,]\d+)?)(Y|M|D|W)/
TIME_COMPONENT = /(\-?\d+(?:[.,]\d+)?)(H|M|S)/
- DATE_TO_PART = { 'Y' => :years, 'M' => :months, 'W' => :weeks, 'D' => :days }
- TIME_TO_PART = { 'H' => :hours, 'M' => :minutes, 'S' => :seconds }
+ DATE_TO_PART = { "Y" => :years, "M" => :months, "W" => :weeks, "D" => :days }
+ TIME_TO_PART = { "H" => :hours, "M" => :minutes, "S" => :seconds }
DATE_COMPONENTS = [:years, :months, :days]
TIME_COMPONENTS = [:hours, :minutes, :seconds]
@@ -39,36 +40,36 @@ module ActiveSupport
def parse!
while !finished?
case mode
- when :start
- if scan(SIGN_MARKER)
- self.sign = (scanner.matched == '-') ? -1 : 1
- self.mode = :sign
- else
- raise_parsing_error
- end
-
- when :sign
- if scan(DATE_MARKER)
- self.mode = :date
- else
- raise_parsing_error
- end
-
- when :date
- if scan(TIME_MARKER)
- self.mode = :time
- elsif scan(DATE_COMPONENT)
- parts[DATE_TO_PART[scanner[2]]] = number * sign
- else
- raise_parsing_error
- end
-
- when :time
- if scan(TIME_COMPONENT)
- parts[TIME_TO_PART[scanner[2]]] = number * sign
- else
- raise_parsing_error
- end
+ when :start
+ if scan(SIGN_MARKER)
+ self.sign = (scanner.matched == "-") ? -1 : 1
+ self.mode = :sign
+ else
+ raise_parsing_error
+ end
+
+ when :sign
+ if scan(DATE_MARKER)
+ self.mode = :date
+ else
+ raise_parsing_error
+ end
+
+ when :date
+ if scan(TIME_MARKER)
+ self.mode = :time
+ elsif scan(DATE_COMPONENT)
+ parts[DATE_TO_PART[scanner[2]]] = number * sign
+ else
+ raise_parsing_error
+ end
+
+ when :time
+ if scan(TIME_COMPONENT)
+ parts[TIME_TO_PART[scanner[2]]] = number * sign
+ else
+ raise_parsing_error
+ end
end
end
@@ -79,44 +80,44 @@ module ActiveSupport
private
- def finished?
- scanner.eos?
- end
+ def finished?
+ scanner.eos?
+ end
- # Parses number which can be a float with either comma or period.
- def number
- scanner[1] =~ PERIOD_OR_COMMA ? scanner[1].tr(COMMA, PERIOD).to_f : scanner[1].to_i
- end
+ # Parses number which can be a float with either comma or period.
+ def number
+ PERIOD_OR_COMMA.match?(scanner[1]) ? scanner[1].tr(COMMA, PERIOD).to_f : scanner[1].to_i
+ end
- def scan(pattern)
- scanner.scan(pattern)
- end
+ def scan(pattern)
+ scanner.scan(pattern)
+ end
- def raise_parsing_error(reason = nil)
- raise ParsingError, "Invalid ISO 8601 duration: #{scanner.string.inspect} #{reason}".strip
- end
+ def raise_parsing_error(reason = nil)
+ raise ParsingError, "Invalid ISO 8601 duration: #{scanner.string.inspect} #{reason}".strip
+ end
- # Checks for various semantic errors as stated in ISO 8601 standard.
- def validate!
- raise_parsing_error('is empty duration') if parts.empty?
+ # Checks for various semantic errors as stated in ISO 8601 standard.
+ def validate!
+ raise_parsing_error("is empty duration") if parts.empty?
- # Mixing any of Y, M, D with W is invalid.
- if parts.key?(:weeks) && (parts.keys & DATE_COMPONENTS).any?
- raise_parsing_error('mixing weeks with other date parts not allowed')
- end
+ # Mixing any of Y, M, D with W is invalid.
+ if parts.key?(:weeks) && (parts.keys & DATE_COMPONENTS).any?
+ raise_parsing_error("mixing weeks with other date parts not allowed")
+ end
- # Specifying an empty T part is invalid.
- if mode == :time && (parts.keys & TIME_COMPONENTS).empty?
- raise_parsing_error('time part marker is present but time part is empty')
- end
+ # Specifying an empty T part is invalid.
+ if mode == :time && (parts.keys & TIME_COMPONENTS).empty?
+ raise_parsing_error("time part marker is present but time part is empty")
+ end
- fractions = parts.values.reject(&:zero?).select { |a| (a % 1) != 0 }
- unless fractions.empty? || (fractions.size == 1 && fractions.last == @parts.values.reject(&:zero?).last)
- raise_parsing_error '(only last part can be fractional)'
- end
+ fractions = parts.values.reject(&:zero?).select { |a| (a % 1) != 0 }
+ unless fractions.empty? || (fractions.size == 1 && fractions.last == @parts.values.reject(&:zero?).last)
+ raise_parsing_error "(only last part can be fractional)"
+ end
- return true
- end
+ return true
+ end
end
end
end