summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2009-10-05 13:38:56 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2009-10-05 13:38:56 -0700
commit3d54485fc149b65344db0272df0ed1820bbb03f3 (patch)
treeebeb95f884ca5ac65d56325974fb35395d284e7b
parent3027b4d69c2fe2e3f4f254e102af3834fddff2f6 (diff)
downloadpsych-3d54485fc149b65344db0272df0ed1820bbb03f3.zip
supporting dates
-rw-r--r--lib/psych/scalar_scanner.rb2
-rw-r--r--lib/psych/visitors/to_ruby.rb10
-rw-r--r--test/test_scalar_scanner.rb6
-rw-r--r--test/visitors/test_to_ruby.rb9
4 files changed, 26 insertions, 1 deletions
diff --git a/lib/psych/scalar_scanner.rb b/lib/psych/scalar_scanner.rb
index be1e7b6..5a038a5 100644
--- a/lib/psych/scalar_scanner.rb
+++ b/lib/psych/scalar_scanner.rb
@@ -12,6 +12,8 @@ module Psych
return [:NULL, nil] if @string.empty?
case @string
+ when /^\d{4}-\d{2}-\d{2}$/
+ [:DATE, @string]
when /^\.inf$/i
[:POSITIVE_INFINITY, 1 / 0.0]
when /^-\.inf$/i
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb
index 757e27d..da94ebb 100644
--- a/lib/psych/visitors/to_ruby.rb
+++ b/lib/psych/visitors/to_ruby.rb
@@ -16,7 +16,15 @@ module Psych
return o.value if ['!str', 'tag:yaml.org,2002:str'].include?(o.tag)
return o.value if o.quoted
- return ScalarScanner.new(o.value).tokenize.last
+ token = ScalarScanner.new(o.value).tokenize
+
+ case token.first
+ when :DATE
+ require 'date'
+ Date.strptime token.last, '%Y-%m-%d'
+ else
+ token.last
+ end
end
def visit_Psych_Nodes_Sequence o
diff --git a/test/test_scalar_scanner.rb b/test/test_scalar_scanner.rb
index af4adc5..c3b19aa 100644
--- a/test/test_scalar_scanner.rb
+++ b/test/test_scalar_scanner.rb
@@ -2,6 +2,12 @@ require 'minitest/autorun'
require 'psych'
class TestScalarScanner < MiniTest::Unit::TestCase
+ def test_scan_date
+ date = '1980-12-16'
+ ss = Psych::ScalarScanner.new date
+ assert_equal [:DATE, date], ss.tokenize
+ end
+
def test_scan_inf
ss = Psych::ScalarScanner.new('.inf')
assert_equal [:POSITIVE_INFINITY, 1 / 0.0], ss.tokenize
diff --git a/test/visitors/test_to_ruby.rb b/test/visitors/test_to_ruby.rb
index 7265316..fa7c539 100644
--- a/test/visitors/test_to_ruby.rb
+++ b/test/visitors/test_to_ruby.rb
@@ -9,6 +9,15 @@ module Psych
@visitor = ToRuby.new
end
+ def test_date
+ d = '1980-12-16'
+ actual = Date.strptime(d, '%Y-%m-%d')
+
+ date = Nodes::Scalar.new(d, nil, 'tag:yaml.org,2002:timestamp', false)
+
+ assert_equal actual, date.to_ruby
+ end
+
def test_rational
mapping = Nodes::Mapping.new nil, 'ruby/object:Rational'
mapping.children << Nodes::Scalar.new('denominator')