diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 1c4195c9babac..b3bbe92afb14a 100644 --- a/activesupport/CHANGELOG.md +++ b/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. diff --git a/activesupport/lib/active_support/xml_mini.rb b/activesupport/lib/active_support/xml_mini.rb index 4a9736f99738e..380d0156b685e 100644 --- a/activesupport/lib/active_support/xml_mini.rb +++ b/activesupport/lib/active_support/xml_mini.rb @@ -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| diff --git a/activesupport/test/xml_mini_test.rb b/activesupport/test/xml_mini_test.rb index 66d4067678e36..90e5699dd7d75 100644 --- a/activesupport/test/xml_mini_test.rb +++ b/activesupport/test/xml_mini_test.rb @@ -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("P3Y6M4DT12H30M5S") + end + test "#to_tag accepts array types" do @xml.to_tag(:b, ["first_name", "last_name"], @options) assert_xml("first_namelast_name") @@ -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)