Merge pull request #51651 from heka1024/support-duration-in-xml
Support duration in `ActiveSupport::XmlMini`
This commit is contained in:
commit
a6e2bb04dd
@ -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*
|
||||
|
@ -46,6 +46,7 @@ def content_type
|
||||
"Date" => "date",
|
||||
"DateTime" => "dateTime",
|
||||
"Time" => "dateTime",
|
||||
"ActiveSupport::Duration" => "duration",
|
||||
"Array" => "array",
|
||||
"Hash" => "hash"
|
||||
}
|
||||
@ -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)
|
||||
@ -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|
|
||||
|
@ -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
|
||||
@ -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>")
|
||||
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user