Skip to content
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

Support duration in ActiveSupport::XmlMini #51651

Merged
merged 1 commit into from May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions activesupport/CHANGELOG.md
@@ -1,3 +1,7 @@
* Support `duration` type in `ActiveSupport::XmlMini`.

*heka1024*

* Remove deprecated `ActiveSupport::Notifications::Event#children` and `ActiveSupport::Notifications::Event#parent_of?`.

*Rafael Mendonça França*
Expand Down
3 changes: 3 additions & 0 deletions activesupport/lib/active_support/xml_mini.rb
Expand Up @@ -46,6 +46,7 @@ def content_type
"Date" => "date",
"DateTime" => "dateTime",
"Time" => "dateTime",
"ActiveSupport::Duration" => "duration",
"Array" => "array",
"Hash" => "hash"
}
Expand All @@ -56,6 +57,7 @@ def content_type
"symbol" => Proc.new { |symbol| symbol.to_s },
"date" => Proc.new { |date| date.to_fs(:db) },
"dateTime" => Proc.new { |time| time.xmlschema },
"duration" => Proc.new { |duration| duration.iso8601 },
"binary" => Proc.new { |binary| ::Base64.encode64(binary) },
"yaml" => Proc.new { |yaml| yaml.to_yaml }
} unless defined?(FORMATTING)
Expand All @@ -66,6 +68,7 @@ def content_type
"symbol" => Proc.new { |symbol| symbol.to_s.to_sym },
"date" => Proc.new { |date| ::Date.parse(date) },
"datetime" => Proc.new { |time| Time.xmlschema(time).utc rescue ::DateTime.parse(time).utc },
"duration" => Proc.new { |duration| Duration.parse(duration) },
"integer" => Proc.new { |integer| integer.to_i },
"float" => Proc.new { |float| float.to_f },
"decimal" => Proc.new do |number|
Expand Down
16 changes: 16 additions & 0 deletions activesupport/test/xml_mini_test.rb
Expand Up @@ -6,6 +6,7 @@
require "active_support/core_ext/hash"
require "active_support/core_ext/big_decimal"
require "active_support/core_ext/date/conversions"
require "active_support/core_ext/integer/time"
require "yaml"

module XmlMiniTest
Expand Down Expand Up @@ -142,6 +143,12 @@ def to_xml(options) options[:builder].yo(options[:root].to_s) end
end
end

test "#to_tag accepts duration types" do
duration = 3.years + 6.months + 4.days + 12.hours + 30.minutes + 5.seconds
@xml.to_tag(:b, duration, @options)
assert_xml("<b type=\"duration\">P3Y6M4DT12H30M5S</b>")
end

test "#to_tag accepts array types" do
@xml.to_tag(:b, ["first_name", "last_name"], @options)
assert_xml("<b type=\"array\"><b>first_name</b><b>last_name</b></b>")
Expand Down Expand Up @@ -267,6 +274,15 @@ def test_datetime
assert_raises(ArgumentError) { parser.call("1384190018") }
end

def test_duration
parser = @parsing["duration"]

assert_equal 1, parser.call("PT1S")
assert_equal 1.minutes, parser.call("PT1M")
assert_equal 3.years + 6.months + 4.days + 12.hours + 30.minutes + 5.seconds, parser.call("P3Y6M4DT12H30M5S")
assert_raises(ArgumentError) { parser.call("not really a duration") }
end

def test_integer
parser = @parsing["integer"]
assert_equal 123, parser.call(123)
Expand Down