Skip to content

Commit

Permalink
Remove Psych deserialization extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
scpike committed Jul 25, 2014
1 parent 6280813 commit 21e6c55
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 66 deletions.
66 changes: 0 additions & 66 deletions lib/delayed/psych_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,73 +31,7 @@ def encode_with(coder)

module Psych
module Visitors
class YAMLTree
def visit_Class(klass) # rubocop:disable MethodName
@emitter.scalar klass.name, nil, '!ruby/class', false, false, Nodes::Scalar::SINGLE_QUOTED
end
end

class ToRuby
def visit_Psych_Nodes_Scalar(o) # rubocop:disable CyclomaticComplexity, MethodName
@st[o.anchor] = o.value if o.anchor

if (klass = Psych.load_tags[o.tag])
instance = klass.allocate

if instance.respond_to?(:init_with)
coder = Psych::Coder.new(o.tag)
coder.scalar = o.value
instance.init_with coder
end

return instance
end

return o.value if o.quoted
return @ss.tokenize(o.value) unless o.tag

case o.tag
when '!binary', 'tag:yaml.org,2002:binary'
o.value.unpack('m').first
when '!str', 'tag:yaml.org,2002:str'
o.value
when '!ruby/object:DateTime'
require 'date'
@ss.parse_time(o.value).to_datetime
when '!ruby/object:Complex'
Complex(o.value)
when '!ruby/object:Rational'
Rational(o.value)
when '!ruby/class', '!ruby/module'
resolve_class o.value
when 'tag:yaml.org,2002:float', '!float'
Float(@ss.tokenize(o.value))
when '!ruby/regexp'
o.value =~ %r{^/(.*)/([mixn]*)$}
source = Regexp.last_match[1]
options = 0
lang = nil
(Regexp.last_match[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
when 'n' then options |= Regexp::NOENCODING
else lang = option
end
end
Regexp.new(*[source, options, lang].compact)
when '!ruby/range'
args = o.value.split(/([.]{2,3})/, 2).collect { |s| accept Nodes::Scalar.new(s) }
args.push(args.delete_at(1) == '...')
Range.new(*args)
when /^!ruby\/sym(bol)?:?(.*)?$/
o.value.to_sym
else
@ss.tokenize o.value
end
end

def visit_Psych_Nodes_Mapping_with_class(object) # rubocop:disable CyclomaticComplexity, MethodName
return revive(Psych.load_tags[object.tag], object) if Psych.load_tags[object.tag]

Expand Down
12 changes: 12 additions & 0 deletions spec/psych_ext_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'helper'

describe 'Psych::Visitors::ToRuby', :if => defined?(Psych::Visitors::ToRuby) do
context BigDecimal do
it 'deserializes correctly' do
deserialized = YAML.load("--- !ruby/object:BigDecimal 18:0.1337E2\n...\n")

expect(deserialized).to be_an_instance_of(BigDecimal)
expect(deserialized).to eq(BigDecimal('13.37'))
end
end
end

0 comments on commit 21e6c55

Please sign in to comment.