aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-01-14 15:36:27 +0000
committerPratik Naik <pratiknaik@gmail.com>2009-01-14 15:36:27 +0000
commitdeb597d63db0c903ce0ff70ab53abed60f95192a (patch)
tree52cd072f2e666baa0286aea741c365d7cb30f42f /activesupport
parente46c80765525817a89b784e6336a9f66250d53fc (diff)
parent9bcf01b23c25e640da7908ac8b1b49fbf7d2e51a (diff)
downloadrails-deb597d63db0c903ce0ff70ab53abed60f95192a.tar.gz
rails-deb597d63db0c903ce0ff70ab53abed60f95192a.tar.bz2
rails-deb597d63db0c903ce0ff70ab53abed60f95192a.zip
Merge commit 'mainstream/master'
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/object/misc.rb2
-rw-r--r--activesupport/lib/active_support/time_with_zone.rb8
-rw-r--r--activesupport/test/core_ext/object_and_class_ext_test.rb19
-rw-r--r--activesupport/test/core_ext/time_with_zone_test.rb9
5 files changed, 28 insertions, 12 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 757cb1da04..fed977775e 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*2.3.0 [Edge]*
+* TimeWithZone#xmlschema accepts optional fraction_digits argument [#1725 state:resolved] [Nicholas Dainty]
+
* Object#tap shim for Ruby < 1.8.7. Similar to Object#returning, tap yields self then returns self. [Jeremy Kemper]
array.select { ... }.tap(&:inspect).map { ... }
diff --git a/activesupport/lib/active_support/core_ext/object/misc.rb b/activesupport/lib/active_support/core_ext/object/misc.rb
index 4570570bbc..c0a109ecf3 100644
--- a/activesupport/lib/active_support/core_ext/object/misc.rb
+++ b/activesupport/lib/active_support/core_ext/object/misc.rb
@@ -102,6 +102,6 @@ class Object
# Person.try(:find, 1)
# @people.try(:map) {|p| p.name}
def try(method, *args, &block)
- send(method, *args, &block) if respond_to?(method, true)
+ send(method, *args, &block) unless self.nil?
end
end
diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb
index 99be89fdf0..ec28f9801a 100644
--- a/activesupport/lib/active_support/time_with_zone.rb
+++ b/activesupport/lib/active_support/time_with_zone.rb
@@ -99,8 +99,12 @@ module ActiveSupport
"#{time.strftime('%a, %d %b %Y %H:%M:%S')} #{zone} #{formatted_offset}"
end
- def xmlschema
- "#{time.strftime("%Y-%m-%dT%H:%M:%S")}#{formatted_offset(true, 'Z')}"
+ def xmlschema(fraction_digits = 0)
+ fraction = if fraction_digits > 0
+ ".%i" % time.usec.to_s[0, fraction_digits]
+ end
+
+ "#{time.strftime("%Y-%m-%dT%H:%M:%S")}#{fraction}#{formatted_offset(true, 'Z')}"
end
alias_method :iso8601, :xmlschema
diff --git a/activesupport/test/core_ext/object_and_class_ext_test.rb b/activesupport/test/core_ext/object_and_class_ext_test.rb
index 2f79b6f67f..0bdbd14f33 100644
--- a/activesupport/test/core_ext/object_and_class_ext_test.rb
+++ b/activesupport/test/core_ext/object_and_class_ext_test.rb
@@ -256,21 +256,13 @@ class ObjectTryTest < Test::Unit::TestCase
def test_nonexisting_method
method = :undefined_method
assert !@string.respond_to?(method)
- assert_nil @string.try(method)
+ assert_raises(NoMethodError) { @string.try(method) }
end
def test_valid_method
assert_equal 5, @string.try(:size)
end
- def test_valid_private_method
- class << @string
- private :size
- end
-
- assert_equal 5, @string.try(:size)
- end
-
def test_argument_forwarding
assert_equal 'Hey', @string.try(:sub, 'llo', 'y')
end
@@ -278,4 +270,13 @@ class ObjectTryTest < Test::Unit::TestCase
def test_block_forwarding
assert_equal 'Hey', @string.try(:sub, 'llo') { |match| 'y' }
end
+
+ def test_nil_to_type
+ assert_nil nil.try(:to_s)
+ assert_nil nil.try(:to_i)
+ end
+
+ def test_false_try
+ assert_equal 'false', false.try(:to_s)
+ end
end
diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb
index 7c8fb7dd94..4dc1fec559 100644
--- a/activesupport/test/core_ext/time_with_zone_test.rb
+++ b/activesupport/test/core_ext/time_with_zone_test.rb
@@ -105,6 +105,15 @@ class TimeWithZoneTest < Test::Unit::TestCase
end
end
+ def test_xmlschema_with_fractional_seconds
+ silence_warnings do # silence warnings raised by tzinfo gem
+ @twz += 0.123456 # advance the time by a fraction of a second
+ assert_equal "1999-12-31T19:00:00.123-05:00", @twz.xmlschema(3)
+ assert_equal "1999-12-31T19:00:00.123456-05:00", @twz.xmlschema(6)
+ assert_equal "1999-12-31T19:00:00.123456-05:00", @twz.xmlschema(12)
+ end
+ end
+
def test_to_yaml
silence_warnings do # silence warnings raised by tzinfo gem
assert_equal "--- 1999-12-31 19:00:00 -05:00\n", @twz.to_yaml