Skip to content

Commit

Permalink
Support duration in ActiveSupport::XmlMini
Browse files Browse the repository at this point in the history
  • Loading branch information
heka1024 committed May 1, 2024
1 parent 325c04c commit 750cbd3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions activesupport/CHANGELOG.md
@@ -1,3 +1,7 @@
* Support `duration` type in `ActiveSupport::XmlMini`.

*heka1024*

* Warn on tests without assertions.

`ActiveSupport::TestCase` now warns when tests do not run any assertions.
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

0 comments on commit 750cbd3

Please sign in to comment.