summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2009-10-07 09:32:58 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2009-10-07 09:32:58 -0700
commit762d889dc911c43274aa4c00104302d36e883add (patch)
tree9afd10778aea5ac53001a569d22d7452d7e5709d
parent2cf1f822b5e44308bb9e5af8ee4d75cfda0b4d9e (diff)
downloadpsych-762d889dc911c43274aa4c00104302d36e883add.zip
scalar regexp goes to ruby
-rw-r--r--lib/psych/ruby.rb6
-rw-r--r--lib/psych/visitors/to_ruby.rb14
-rw-r--r--test/visitors/test_to_ruby.rb11
3 files changed, 27 insertions, 4 deletions
diff --git a/lib/psych/ruby.rb b/lib/psych/ruby.rb
index 6258e81..8831811 100644
--- a/lib/psych/ruby.rb
+++ b/lib/psych/ruby.rb
@@ -3,12 +3,10 @@ require 'rational'
require 'date'
[
- Object, String, Class, Hash, Array, NilClass, Float,
- FalseClass, TrueClass, Range, Complex, Rational, Date
+ Object, String, Class, Hash, Array, NilClass, Float, FalseClass, TrueClass,
+ Range, Complex, Rational, Date, Time, Regexp
# Struct
# Exception
- # Regexp
- # Time
].each do |klass|
klass.send(:remove_method, :to_yaml) rescue NameError
end
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb
index 40236ce..ab8ef52 100644
--- a/lib/psych/visitors/to_ruby.rb
+++ b/lib/psych/visitors/to_ruby.rb
@@ -22,6 +22,20 @@ module Psych
Complex(o.value)
when "!ruby/object:Rational"
Rational(o.value)
+ when "!ruby/regexp"
+ o.value =~ /^\/(.*)\/([mix]*)$/
+ source = $1
+ options = 0
+ lang = nil
+ ($2 || '').split('').each do |option|
+ case option
+ when 'x' then options |= Regexp::EXTENDED
+ when 'i' then options |= Regexp::IGNORECASE
+ when 'm' then options |= Regexp::MULTILINE
+ else lang = option
+ end
+ end
+ Regexp.new(*[source, options, lang].compact)
when "!ruby/range"
args = o.value.split(/([.]{2,3})/, 2).map { |s|
accept Nodes::Scalar.new(s)
diff --git a/test/visitors/test_to_ruby.rb b/test/visitors/test_to_ruby.rb
index 41eae48..7fd99a4 100644
--- a/test/visitors/test_to_ruby.rb
+++ b/test/visitors/test_to_ruby.rb
@@ -11,6 +11,17 @@ module Psych
@visitor = ToRuby.new
end
+ def test_regexp
+ node = Nodes::Scalar.new('/foo/', nil, '!ruby/regexp')
+ assert_equal(/foo/, node.to_ruby)
+
+ node = Nodes::Scalar.new('/foo/m', nil, '!ruby/regexp')
+ assert_equal(/foo/m, node.to_ruby)
+
+ node = Nodes::Scalar.new('/foo/ix', nil, '!ruby/regexp')
+ assert_equal(/foo/ix, node.to_ruby)
+ end
+
def test_time
now = Time.now
formatted = now.strftime("%Y-%m-%d %H:%M:%S") +