Skip to content

Commit a6e2bb0

Browse files
Merge pull request rails#51651 from heka1024/support-duration-in-xml
Support duration in `ActiveSupport::XmlMini`
2 parents 749d337 + b681bb6 commit a6e2bb0

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

activesupport/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Support `duration` type in `ActiveSupport::XmlMini`.
2+
3+
*heka1024*
4+
15
* Remove deprecated `ActiveSupport::Notifications::Event#children` and `ActiveSupport::Notifications::Event#parent_of?`.
26

37
*Rafael Mendonça França*

activesupport/lib/active_support/xml_mini.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def content_type
4646
"Date" => "date",
4747
"DateTime" => "dateTime",
4848
"Time" => "dateTime",
49+
"ActiveSupport::Duration" => "duration",
4950
"Array" => "array",
5051
"Hash" => "hash"
5152
}
@@ -56,6 +57,7 @@ def content_type
5657
"symbol" => Proc.new { |symbol| symbol.to_s },
5758
"date" => Proc.new { |date| date.to_fs(:db) },
5859
"dateTime" => Proc.new { |time| time.xmlschema },
60+
"duration" => Proc.new { |duration| duration.iso8601 },
5961
"binary" => Proc.new { |binary| ::Base64.encode64(binary) },
6062
"yaml" => Proc.new { |yaml| yaml.to_yaml }
6163
} unless defined?(FORMATTING)
@@ -66,6 +68,7 @@ def content_type
6668
"symbol" => Proc.new { |symbol| symbol.to_s.to_sym },
6769
"date" => Proc.new { |date| ::Date.parse(date) },
6870
"datetime" => Proc.new { |time| Time.xmlschema(time).utc rescue ::DateTime.parse(time).utc },
71+
"duration" => Proc.new { |duration| Duration.parse(duration) },
6972
"integer" => Proc.new { |integer| integer.to_i },
7073
"float" => Proc.new { |float| float.to_f },
7174
"decimal" => Proc.new do |number|

activesupport/test/xml_mini_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require "active_support/core_ext/hash"
77
require "active_support/core_ext/big_decimal"
88
require "active_support/core_ext/date/conversions"
9+
require "active_support/core_ext/integer/time"
910
require "yaml"
1011

1112
module XmlMiniTest
@@ -142,6 +143,12 @@ def to_xml(options) options[:builder].yo(options[:root].to_s) end
142143
end
143144
end
144145

146+
test "#to_tag accepts duration types" do
147+
duration = 3.years + 6.months + 4.days + 12.hours + 30.minutes + 5.seconds
148+
@xml.to_tag(:b, duration, @options)
149+
assert_xml("<b type=\"duration\">P3Y6M4DT12H30M5S</b>")
150+
end
151+
145152
test "#to_tag accepts array types" do
146153
@xml.to_tag(:b, ["first_name", "last_name"], @options)
147154
assert_xml("<b type=\"array\"><b>first_name</b><b>last_name</b></b>")
@@ -267,6 +274,15 @@ def test_datetime
267274
assert_raises(ArgumentError) { parser.call("1384190018") }
268275
end
269276

277+
def test_duration
278+
parser = @parsing["duration"]
279+
280+
assert_equal 1, parser.call("PT1S")
281+
assert_equal 1.minutes, parser.call("PT1M")
282+
assert_equal 3.years + 6.months + 4.days + 12.hours + 30.minutes + 5.seconds, parser.call("P3Y6M4DT12H30M5S")
283+
assert_raises(ArgumentError) { parser.call("not really a duration") }
284+
end
285+
270286
def test_integer
271287
parser = @parsing["integer"]
272288
assert_equal 123, parser.call(123)

0 commit comments

Comments
 (0)