parent
ed496447e5
commit
ff1d32aceb
@ -1,136 +0,0 @@
|
|||||||
From 7d7c2d13ba896dd8b58fe91d2fe0c4fc588069ca Mon Sep 17 00:00:00 2001
|
|
||||||
From: =?UTF-8?q?V=C3=ADt=20Ondruch?= <vondruch@redhat.com>
|
|
||||||
Date: Mon, 4 Jul 2016 13:11:44 +0200
|
|
||||||
Subject: [PATCH 1/2] DRY Downloader.
|
|
||||||
|
|
||||||
---
|
|
||||||
activesupport/test/multibyte_conformance_test.rb | 18 ------------------
|
|
||||||
.../test/multibyte_grapheme_break_conformance_test.rb | 19 ++-----------------
|
|
||||||
.../test/multibyte_normalization_conformance_test.rb | 18 ------------------
|
|
||||||
activesupport/test/multibyte_test_helpers.rb | 18 ++++++++++++++++++
|
|
||||||
4 files changed, 20 insertions(+), 53 deletions(-)
|
|
||||||
|
|
||||||
diff --git a/activesupport/test/multibyte_conformance_test.rb b/activesupport/test/multibyte_conformance_test.rb
|
|
||||||
index 9fca47a..709c63a 100644
|
|
||||||
--- a/activesupport/test/multibyte_conformance_test.rb
|
|
||||||
+++ b/activesupport/test/multibyte_conformance_test.rb
|
|
||||||
@@ -6,24 +6,6 @@
|
|
||||||
require 'tmpdir'
|
|
||||||
|
|
||||||
class MultibyteConformanceTest < ActiveSupport::TestCase
|
|
||||||
- class Downloader
|
|
||||||
- def self.download(from, to)
|
|
||||||
- unless File.exist?(to)
|
|
||||||
- unless File.exist?(File.dirname(to))
|
|
||||||
- system "mkdir -p #{File.dirname(to)}"
|
|
||||||
- end
|
|
||||||
- open(from) do |source|
|
|
||||||
- File.open(to, 'w') do |target|
|
|
||||||
- source.each_line do |l|
|
|
||||||
- target.write l
|
|
||||||
- end
|
|
||||||
- end
|
|
||||||
- end
|
|
||||||
- end
|
|
||||||
- true
|
|
||||||
- end
|
|
||||||
- end
|
|
||||||
-
|
|
||||||
include MultibyteTestHelpers
|
|
||||||
|
|
||||||
UNIDATA_URL = "http://www.unicode.org/Public/#{ActiveSupport::Multibyte::Unicode::UNICODE_VERSION}/ucd"
|
|
||||||
diff --git a/activesupport/test/multibyte_grapheme_break_conformance_test.rb b/activesupport/test/multibyte_grapheme_break_conformance_test.rb
|
|
||||||
index 6e2f02a..45e1976 100644
|
|
||||||
--- a/activesupport/test/multibyte_grapheme_break_conformance_test.rb
|
|
||||||
+++ b/activesupport/test/multibyte_grapheme_break_conformance_test.rb
|
|
||||||
@@ -1,29 +1,14 @@
|
|
||||||
# encoding: utf-8
|
|
||||||
|
|
||||||
require 'abstract_unit'
|
|
||||||
+require 'multibyte_test_helpers'
|
|
||||||
|
|
||||||
require 'fileutils'
|
|
||||||
require 'open-uri'
|
|
||||||
require 'tmpdir'
|
|
||||||
|
|
||||||
class MultibyteGraphemeBreakConformanceTest < ActiveSupport::TestCase
|
|
||||||
- class Downloader
|
|
||||||
- def self.download(from, to)
|
|
||||||
- unless File.exist?(to)
|
|
||||||
- $stderr.puts "Downloading #{from} to #{to}"
|
|
||||||
- unless File.exist?(File.dirname(to))
|
|
||||||
- system "mkdir -p #{File.dirname(to)}"
|
|
||||||
- end
|
|
||||||
- open(from) do |source|
|
|
||||||
- File.open(to, 'w') do |target|
|
|
||||||
- source.each_line do |l|
|
|
||||||
- target.write l
|
|
||||||
- end
|
|
||||||
- end
|
|
||||||
- end
|
|
||||||
- end
|
|
||||||
- end
|
|
||||||
- end
|
|
||||||
+ include MultibyteTestHelpers
|
|
||||||
|
|
||||||
TEST_DATA_URL = "http://www.unicode.org/Public/#{ActiveSupport::Multibyte::Unicode::UNICODE_VERSION}/ucd/auxiliary"
|
|
||||||
TEST_DATA_FILE = '/GraphemeBreakTest.txt'
|
|
||||||
diff --git a/activesupport/test/multibyte_normalization_conformance_test.rb b/activesupport/test/multibyte_normalization_conformance_test.rb
|
|
||||||
index 0d31c95..ad2e651 100644
|
|
||||||
--- a/activesupport/test/multibyte_normalization_conformance_test.rb
|
|
||||||
+++ b/activesupport/test/multibyte_normalization_conformance_test.rb
|
|
||||||
@@ -8,24 +8,6 @@
|
|
||||||
require 'tmpdir'
|
|
||||||
|
|
||||||
class MultibyteNormalizationConformanceTest < ActiveSupport::TestCase
|
|
||||||
- class Downloader
|
|
||||||
- def self.download(from, to)
|
|
||||||
- unless File.exist?(to)
|
|
||||||
- $stderr.puts "Downloading #{from} to #{to}"
|
|
||||||
- unless File.exist?(File.dirname(to))
|
|
||||||
- system "mkdir -p #{File.dirname(to)}"
|
|
||||||
- end
|
|
||||||
- open(from) do |source|
|
|
||||||
- File.open(to, 'w') do |target|
|
|
||||||
- source.each_line do |l|
|
|
||||||
- target.write l
|
|
||||||
- end
|
|
||||||
- end
|
|
||||||
- end
|
|
||||||
- end
|
|
||||||
- end
|
|
||||||
- end
|
|
||||||
-
|
|
||||||
include MultibyteTestHelpers
|
|
||||||
|
|
||||||
UNIDATA_URL = "http://www.unicode.org/Public/#{ActiveSupport::Multibyte::Unicode::UNICODE_VERSION}/ucd"
|
|
||||||
diff --git a/activesupport/test/multibyte_test_helpers.rb b/activesupport/test/multibyte_test_helpers.rb
|
|
||||||
index 58cf548..709586e 100644
|
|
||||||
--- a/activesupport/test/multibyte_test_helpers.rb
|
|
||||||
+++ b/activesupport/test/multibyte_test_helpers.rb
|
|
||||||
@@ -1,4 +1,22 @@
|
|
||||||
module MultibyteTestHelpers
|
|
||||||
+ class Downloader
|
|
||||||
+ def self.download(from, to)
|
|
||||||
+ unless File.exist?(to)
|
|
||||||
+ unless File.exist?(File.dirname(to))
|
|
||||||
+ system "mkdir -p #{File.dirname(to)}"
|
|
||||||
+ end
|
|
||||||
+ open(from) do |source|
|
|
||||||
+ File.open(to, 'w') do |target|
|
|
||||||
+ source.each_line do |l|
|
|
||||||
+ target.write l
|
|
||||||
+ end
|
|
||||||
+ end
|
|
||||||
+ end
|
|
||||||
+ end
|
|
||||||
+ true
|
|
||||||
+ end
|
|
||||||
+ end
|
|
||||||
+
|
|
||||||
UNICODE_STRING = 'こにちわ'.freeze
|
|
||||||
ASCII_STRING = 'ohayo'.freeze
|
|
||||||
BYTE_STRING = "\270\236\010\210\245".force_encoding("ASCII-8BIT").freeze
|
|
||||||
--
|
|
||||||
2.9.0
|
|
||||||
|
|
@ -1,101 +0,0 @@
|
|||||||
From c245ca30f248367e07d5d831195b12c93a1e3137 Mon Sep 17 00:00:00 2001
|
|
||||||
From: =?UTF-8?q?V=C3=ADt=20Ondruch?= <vondruch@redhat.com>
|
|
||||||
Date: Mon, 4 Jul 2016 13:36:30 +0200
|
|
||||||
Subject: [PATCH] Skip the test if test data download fails.
|
|
||||||
|
|
||||||
---
|
|
||||||
activesupport/test/multibyte_conformance_test.rb | 2 --
|
|
||||||
.../test/multibyte_grapheme_break_conformance_test.rb | 13 +++++++------
|
|
||||||
.../test/multibyte_normalization_conformance_test.rb | 9 +++++----
|
|
||||||
activesupport/test/multibyte_test_helpers.rb | 4 ++++
|
|
||||||
4 files changed, 16 insertions(+), 12 deletions(-)
|
|
||||||
|
|
||||||
diff --git a/activesupport/test/multibyte_conformance_test.rb b/activesupport/test/multibyte_conformance_test.rb
|
|
||||||
index 709c63a..c10133a 100644
|
|
||||||
--- a/activesupport/test/multibyte_conformance_test.rb
|
|
||||||
+++ b/activesupport/test/multibyte_conformance_test.rb
|
|
||||||
@@ -8,9 +8,7 @@
|
|
||||||
class MultibyteConformanceTest < ActiveSupport::TestCase
|
|
||||||
include MultibyteTestHelpers
|
|
||||||
|
|
||||||
- UNIDATA_URL = "http://www.unicode.org/Public/#{ActiveSupport::Multibyte::Unicode::UNICODE_VERSION}/ucd"
|
|
||||||
UNIDATA_FILE = '/NormalizationTest.txt'
|
|
||||||
- CACHE_DIR = "#{Dir.tmpdir}/cache/unicode_conformance"
|
|
||||||
FileUtils.mkdir_p(CACHE_DIR)
|
|
||||||
RUN_P = begin
|
|
||||||
Downloader.download(UNIDATA_URL + UNIDATA_FILE, CACHE_DIR + UNIDATA_FILE)
|
|
||||||
diff --git a/activesupport/test/multibyte_grapheme_break_conformance_test.rb b/activesupport/test/multibyte_grapheme_break_conformance_test.rb
|
|
||||||
index 45e1976..61943b1 100644
|
|
||||||
--- a/activesupport/test/multibyte_grapheme_break_conformance_test.rb
|
|
||||||
+++ b/activesupport/test/multibyte_grapheme_break_conformance_test.rb
|
|
||||||
@@ -10,13 +10,14 @@
|
|
||||||
class MultibyteGraphemeBreakConformanceTest < ActiveSupport::TestCase
|
|
||||||
include MultibyteTestHelpers
|
|
||||||
|
|
||||||
- TEST_DATA_URL = "http://www.unicode.org/Public/#{ActiveSupport::Multibyte::Unicode::UNICODE_VERSION}/ucd/auxiliary"
|
|
||||||
- TEST_DATA_FILE = '/GraphemeBreakTest.txt'
|
|
||||||
- CACHE_DIR = "#{Dir.tmpdir}/cache/unicode_conformance"
|
|
||||||
+ UNIDATA_FILE = '/auxiliary/GraphemeBreakTest.txt'
|
|
||||||
+ RUN_P = begin
|
|
||||||
+ Downloader.download(UNIDATA_URL + UNIDATA_FILE, CACHE_DIR + UNIDATA_FILE)
|
|
||||||
+ rescue
|
|
||||||
+ end
|
|
||||||
|
|
||||||
def setup
|
|
||||||
- FileUtils.mkdir_p(CACHE_DIR)
|
|
||||||
- Downloader.download(TEST_DATA_URL + TEST_DATA_FILE, CACHE_DIR + TEST_DATA_FILE)
|
|
||||||
+ skip "Unable to download test data" unless RUN_P
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_breaks
|
|
||||||
@@ -31,7 +32,7 @@ def test_breaks
|
|
||||||
def each_line_of_break_tests(&block)
|
|
||||||
lines = 0
|
|
||||||
max_test_lines = 0 # Don't limit below 21, because that's the header of the testfile
|
|
||||||
- File.open(File.join(CACHE_DIR, TEST_DATA_FILE), 'r') do | f |
|
|
||||||
+ File.open(File.join(CACHE_DIR, UNIDATA_FILE), 'r') do | f |
|
|
||||||
until f.eof? || (max_test_lines > 21 and lines > max_test_lines)
|
|
||||||
lines += 1
|
|
||||||
line = f.gets.chomp!
|
|
||||||
diff --git a/activesupport/test/multibyte_normalization_conformance_test.rb b/activesupport/test/multibyte_normalization_conformance_test.rb
|
|
||||||
index ad2e651..77ed0ce 100644
|
|
||||||
--- a/activesupport/test/multibyte_normalization_conformance_test.rb
|
|
||||||
+++ b/activesupport/test/multibyte_normalization_conformance_test.rb
|
|
||||||
@@ -10,14 +10,15 @@
|
|
||||||
class MultibyteNormalizationConformanceTest < ActiveSupport::TestCase
|
|
||||||
include MultibyteTestHelpers
|
|
||||||
|
|
||||||
- UNIDATA_URL = "http://www.unicode.org/Public/#{ActiveSupport::Multibyte::Unicode::UNICODE_VERSION}/ucd"
|
|
||||||
UNIDATA_FILE = '/NormalizationTest.txt'
|
|
||||||
- CACHE_DIR = "#{Dir.tmpdir}/cache/unicode_conformance"
|
|
||||||
+ RUN_P = begin
|
|
||||||
+ Downloader.download(UNIDATA_URL + UNIDATA_FILE, CACHE_DIR + UNIDATA_FILE)
|
|
||||||
+ rescue
|
|
||||||
+ end
|
|
||||||
|
|
||||||
def setup
|
|
||||||
- FileUtils.mkdir_p(CACHE_DIR)
|
|
||||||
- Downloader.download(UNIDATA_URL + UNIDATA_FILE, CACHE_DIR + UNIDATA_FILE)
|
|
||||||
@proxy = ActiveSupport::Multibyte::Chars
|
|
||||||
+ skip "Unable to download test data" unless RUN_P
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_normalizations_C
|
|
||||||
diff --git a/activesupport/test/multibyte_test_helpers.rb b/activesupport/test/multibyte_test_helpers.rb
|
|
||||||
index 709586e..0ada8bc 100644
|
|
||||||
--- a/activesupport/test/multibyte_test_helpers.rb
|
|
||||||
+++ b/activesupport/test/multibyte_test_helpers.rb
|
|
||||||
@@ -17,6 +17,10 @@ def self.download(from, to)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
+ UNIDATA_URL = "http://www.unicode.org/Public/#{ActiveSupport::Multibyte::Unicode::UNICODE_VERSION}/ucd"
|
|
||||||
+ CACHE_DIR = "#{Dir.tmpdir}/cache/unicode_conformance"
|
|
||||||
+ FileUtils.mkdir_p(CACHE_DIR)
|
|
||||||
+
|
|
||||||
UNICODE_STRING = 'こにちわ'.freeze
|
|
||||||
ASCII_STRING = 'ohayo'.freeze
|
|
||||||
BYTE_STRING = "\270\236\010\210\245".force_encoding("ASCII-8BIT").freeze
|
|
||||||
--
|
|
||||||
2.9.0
|
|
||||||
|
|
@ -0,0 +1,102 @@
|
|||||||
|
From 28938dd64c8b4fa1943d0b878d3d832b94fa12a3 Mon Sep 17 00:00:00 2001
|
||||||
|
From: Andrew White <andrew.white@unboxed.co>
|
||||||
|
Date: Sat, 20 May 2017 16:33:09 +0100
|
||||||
|
Subject: [PATCH] Fix implicit calculations with scalars and durations
|
||||||
|
|
||||||
|
Previously calculations where the scalar is first would be converted
|
||||||
|
to a duration of seconds but this causes issues with dates being
|
||||||
|
converted to times, e.g:
|
||||||
|
|
||||||
|
Time.zone = "Beijing" # => Asia/Shanghai
|
||||||
|
date = Date.civil(2017, 5, 20) # => Mon, 20 May 2017
|
||||||
|
2 * 1.day # => 172800 seconds
|
||||||
|
date + 2 * 1.day # => Mon, 22 May 2017 00:00:00 CST +08:00
|
||||||
|
|
||||||
|
Now the `ActiveSupport::Duration::Scalar` calculation methods will try
|
||||||
|
to maintain the part structure of the duration where possible, e.g:
|
||||||
|
|
||||||
|
Time.zone = "Beijing" # => Asia/Shanghai
|
||||||
|
date = Date.civil(2017, 5, 20) # => Mon, 20 May 2017
|
||||||
|
2 * 1.day # => 2 days
|
||||||
|
date + 2 * 1.day # => Mon, 22 May 2017
|
||||||
|
|
||||||
|
Fixes #29160, #28970.
|
||||||
|
---
|
||||||
|
activesupport/CHANGELOG.md | 22 +++++++++++++++
|
||||||
|
activesupport/lib/active_support/duration.rb | 41 ++++++++++++++++++++++++----
|
||||||
|
activesupport/test/core_ext/duration_test.rb | 34 +++++++++++++++++++++++
|
||||||
|
3 files changed, 91 insertions(+), 6 deletions(-)
|
||||||
|
|
||||||
|
diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb
|
||||||
|
index 1648a9b27060..3108f24f218c 100644
|
||||||
|
--- a/activesupport/test/core_ext/duration_test.rb
|
||||||
|
+++ b/activesupport/test/core_ext/duration_test.rb
|
||||||
|
@@ -337,6 +337,13 @@ def test_scalar_plus
|
||||||
|
assert_equal "no implicit conversion of String into ActiveSupport::Duration::Scalar", exception.message
|
||||||
|
end
|
||||||
|
|
||||||
|
+ def test_scalar_plus_parts
|
||||||
|
+ scalar = ActiveSupport::Duration::Scalar.new(10)
|
||||||
|
+
|
||||||
|
+ assert_equal({ days: 1, seconds: 10 }, (scalar + 1.day).parts)
|
||||||
|
+ assert_equal({ days: -1, seconds: 10 }, (scalar + -1.day).parts)
|
||||||
|
+ end
|
||||||
|
+
|
||||||
|
def test_scalar_minus
|
||||||
|
scalar = ActiveSupport::Duration::Scalar.new(10)
|
||||||
|
|
||||||
|
@@ -349,6 +356,9 @@ def test_scalar_minus
|
||||||
|
assert_equal 5, scalar - 5.seconds
|
||||||
|
assert_instance_of ActiveSupport::Duration, scalar - 5.seconds
|
||||||
|
|
||||||
|
+ assert_equal({ days: -1, seconds: 10 }, (scalar - 1.day).parts)
|
||||||
|
+ assert_equal({ days: 1, seconds: 10 }, (scalar - -1.day).parts)
|
||||||
|
+
|
||||||
|
exception = assert_raises(TypeError) do
|
||||||
|
scalar - "foo"
|
||||||
|
end
|
||||||
|
@@ -356,6 +366,13 @@ def test_scalar_minus
|
||||||
|
assert_equal "no implicit conversion of String into ActiveSupport::Duration::Scalar", exception.message
|
||||||
|
end
|
||||||
|
|
||||||
|
+ def test_scalar_minus_parts
|
||||||
|
+ scalar = ActiveSupport::Duration::Scalar.new(10)
|
||||||
|
+
|
||||||
|
+ assert_equal({ days: -1, seconds: 10 }, (scalar - 1.day).parts)
|
||||||
|
+ assert_equal({ days: 1, seconds: 10 }, (scalar - -1.day).parts)
|
||||||
|
+ end
|
||||||
|
+
|
||||||
|
def test_scalar_multiply
|
||||||
|
scalar = ActiveSupport::Duration::Scalar.new(5)
|
||||||
|
|
||||||
|
@@ -375,6 +392,14 @@ def test_scalar_multiply
|
||||||
|
assert_equal "no implicit conversion of String into ActiveSupport::Duration::Scalar", exception.message
|
||||||
|
end
|
||||||
|
|
||||||
|
+ def test_scalar_multiply_parts
|
||||||
|
+ scalar = ActiveSupport::Duration::Scalar.new(1)
|
||||||
|
+ assert_equal({ days: 2 }, (scalar * 2.days).parts)
|
||||||
|
+ assert_equal(172800, (scalar * 2.days).value)
|
||||||
|
+ assert_equal({ days: -2 }, (scalar * -2.days).parts)
|
||||||
|
+ assert_equal(-172800, (scalar * -2.days).value)
|
||||||
|
+ end
|
||||||
|
+
|
||||||
|
def test_scalar_divide
|
||||||
|
scalar = ActiveSupport::Duration::Scalar.new(10)
|
||||||
|
|
||||||
|
@@ -394,6 +419,15 @@ def test_scalar_divide
|
||||||
|
assert_equal "no implicit conversion of String into ActiveSupport::Duration::Scalar", exception.message
|
||||||
|
end
|
||||||
|
|
||||||
|
+ def test_scalar_divide_parts
|
||||||
|
+ scalar = ActiveSupport::Duration::Scalar.new(10)
|
||||||
|
+
|
||||||
|
+ assert_equal({ days: 2 }, (scalar / 5.days).parts)
|
||||||
|
+ assert_equal(172800, (scalar / 5.days).value)
|
||||||
|
+ assert_equal({ days: -2 }, (scalar / -5.days).parts)
|
||||||
|
+ assert_equal(-172800, (scalar / -5.days).value)
|
||||||
|
+ end
|
||||||
|
+
|
||||||
|
def test_twelve_months_equals_one_year
|
||||||
|
assert_equal 12.months, 1.year
|
||||||
|
end
|
@ -0,0 +1,126 @@
|
|||||||
|
From 28938dd64c8b4fa1943d0b878d3d832b94fa12a3 Mon Sep 17 00:00:00 2001
|
||||||
|
From: Andrew White <andrew.white@unboxed.co>
|
||||||
|
Date: Sat, 20 May 2017 16:33:09 +0100
|
||||||
|
Subject: [PATCH] Fix implicit calculations with scalars and durations
|
||||||
|
|
||||||
|
Previously calculations where the scalar is first would be converted
|
||||||
|
to a duration of seconds but this causes issues with dates being
|
||||||
|
converted to times, e.g:
|
||||||
|
|
||||||
|
Time.zone = "Beijing" # => Asia/Shanghai
|
||||||
|
date = Date.civil(2017, 5, 20) # => Mon, 20 May 2017
|
||||||
|
2 * 1.day # => 172800 seconds
|
||||||
|
date + 2 * 1.day # => Mon, 22 May 2017 00:00:00 CST +08:00
|
||||||
|
|
||||||
|
Now the `ActiveSupport::Duration::Scalar` calculation methods will try
|
||||||
|
to maintain the part structure of the duration where possible, e.g:
|
||||||
|
|
||||||
|
Time.zone = "Beijing" # => Asia/Shanghai
|
||||||
|
date = Date.civil(2017, 5, 20) # => Mon, 20 May 2017
|
||||||
|
2 * 1.day # => 2 days
|
||||||
|
date + 2 * 1.day # => Mon, 22 May 2017
|
||||||
|
|
||||||
|
Fixes #29160, #28970.
|
||||||
|
---
|
||||||
|
activesupport/CHANGELOG.md | 22 +++++++++++++++
|
||||||
|
activesupport/lib/active_support/duration.rb | 41 ++++++++++++++++++++++++----
|
||||||
|
activesupport/test/core_ext/duration_test.rb | 34 +++++++++++++++++++++++
|
||||||
|
3 files changed, 91 insertions(+), 6 deletions(-)
|
||||||
|
|
||||||
|
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
|
||||||
|
index c50c1902fe53..bae573cf37ac 100644
|
||||||
|
--- a/activesupport/CHANGELOG.md
|
||||||
|
+++ b/activesupport/CHANGELOG.md
|
||||||
|
@@ -1,3 +1,25 @@
|
||||||
|
+* Fix implicit coercion calculations with scalars and durations
|
||||||
|
+
|
||||||
|
+ Previously calculations where the scalar is first would be converted to a duration
|
||||||
|
+ of seconds but this causes issues with dates being converted to times, e.g:
|
||||||
|
+
|
||||||
|
+ Time.zone = "Beijing" # => Asia/Shanghai
|
||||||
|
+ date = Date.civil(2017, 5, 20) # => Mon, 20 May 2017
|
||||||
|
+ 2 * 1.day # => 172800 seconds
|
||||||
|
+ date + 2 * 1.day # => Mon, 22 May 2017 00:00:00 CST +08:00
|
||||||
|
+
|
||||||
|
+ Now the `ActiveSupport::Duration::Scalar` calculation methods will try to maintain
|
||||||
|
+ the part structure of the duration where possible, e.g:
|
||||||
|
+
|
||||||
|
+ Time.zone = "Beijing" # => Asia/Shanghai
|
||||||
|
+ date = Date.civil(2017, 5, 20) # => Mon, 20 May 2017
|
||||||
|
+ 2 * 1.day # => 2 days
|
||||||
|
+ date + 2 * 1.day # => Mon, 22 May 2017
|
||||||
|
+
|
||||||
|
+ Fixes #29160, #28970.
|
||||||
|
+
|
||||||
|
+ *Andrew White*
|
||||||
|
+
|
||||||
|
## Rails 5.1.0 (April 27, 2017) ##
|
||||||
|
|
||||||
|
* `ActiveSupport::EventedFileUpdateChecker` no longer listens to
|
||||||
|
diff --git a/activesupport/lib/active_support/duration.rb b/activesupport/lib/active_support/duration.rb
|
||||||
|
index d4424ed792a4..39deb2313f5a 100644
|
||||||
|
--- a/activesupport/lib/active_support/duration.rb
|
||||||
|
+++ b/activesupport/lib/active_support/duration.rb
|
||||||
|
@@ -37,27 +37,56 @@ def <=>(other)
|
||||||
|
end
|
||||||
|
|
||||||
|
def +(other)
|
||||||
|
- calculate(:+, other)
|
||||||
|
+ if Duration === other
|
||||||
|
+ seconds = value + other.parts[:seconds]
|
||||||
|
+ new_parts = other.parts.merge(seconds: seconds)
|
||||||
|
+ new_value = value + other.value
|
||||||
|
+
|
||||||
|
+ Duration.new(new_value, new_parts)
|
||||||
|
+ else
|
||||||
|
+ calculate(:+, other)
|
||||||
|
+ end
|
||||||
|
end
|
||||||
|
|
||||||
|
def -(other)
|
||||||
|
- calculate(:-, other)
|
||||||
|
+ if Duration === other
|
||||||
|
+ seconds = value - other.parts[:seconds]
|
||||||
|
+ new_parts = other.parts.map { |part, other_value| [part, -other_value] }.to_h
|
||||||
|
+ new_parts = new_parts.merge(seconds: seconds)
|
||||||
|
+ new_value = value - other.value
|
||||||
|
+
|
||||||
|
+ Duration.new(new_value, new_parts)
|
||||||
|
+ else
|
||||||
|
+ calculate(:-, other)
|
||||||
|
+ end
|
||||||
|
end
|
||||||
|
|
||||||
|
def *(other)
|
||||||
|
- calculate(:*, other)
|
||||||
|
+ if Duration === other
|
||||||
|
+ new_parts = other.parts.map { |part, other_value| [part, value * other_value] }.to_h
|
||||||
|
+ new_value = value * other.value
|
||||||
|
+
|
||||||
|
+ Duration.new(new_value, new_parts)
|
||||||
|
+ else
|
||||||
|
+ calculate(:*, other)
|
||||||
|
+ end
|
||||||
|
end
|
||||||
|
|
||||||
|
def /(other)
|
||||||
|
- calculate(:/, other)
|
||||||
|
+ if Duration === other
|
||||||
|
+ new_parts = other.parts.map { |part, other_value| [part, value / other_value] }.to_h
|
||||||
|
+ new_value = new_parts.inject(0) { |total, (part, value)| total + value * Duration::PARTS_IN_SECONDS[part] }
|
||||||
|
+
|
||||||
|
+ Duration.new(new_value, new_parts)
|
||||||
|
+ else
|
||||||
|
+ calculate(:/, other)
|
||||||
|
+ end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def calculate(op, other)
|
||||||
|
if Scalar === other
|
||||||
|
Scalar.new(value.public_send(op, other.value))
|
||||||
|
- elsif Duration === other
|
||||||
|
- Duration.seconds(value).public_send(op, other)
|
||||||
|
elsif Numeric === other
|
||||||
|
Scalar.new(value.public_send(op, other))
|
||||||
|
else
|
@ -1,2 +1,2 @@
|
|||||||
SHA512 (activesupport-5.0.2-tests.tgz) = e73d3a0165aa9e80365a48d3f71550fb7ce00f7b41ba51968467027cd24528880adb4563d4a57c6756867d844d42d19698470c3b8453f06274b10dacbfe5d0c0
|
SHA512 (activesupport-5.1.1.gem) = 3ed4596c827182d95f98194cad8c981bbb1567e9a7c4dca06b129e1604f7fa68a425b2b521bc41b758b45ddadf143eed19f3455a3f85a64fe0a7fb00f762a5d8
|
||||||
SHA512 (activesupport-5.0.2.gem) = 6f3d26d6a6cdcfcbd069f2e7f1062e312e7572e0dcd6fe1d9b3f0d01c3eab81967935bd014043fb08e8a3c1f4b7730ccd718b46203673d6a0cbaed79c39c6bb0
|
SHA512 (activesupport-5.1.1-tests.tgz) = ffe188866a38d8195c735a729ea854d6619629e28ab471a5920aa45f8da4895c026b0e872918c74d10ade4e2abeea302c9ad85a63eb38a50d960d4433ee1dd5b
|
||||||
|
Loading…
Reference in new issue