Class Psych::Visitors::ToRuby
In: lib/delayed/psych_ext.rb
Parent: Object

Methods

Public Instance methods

[Source]

     # File lib/delayed/psych_ext.rb, line 124
124:       def resolve_class_with_constantize(klass_name)
125:         klass_name.constantize
126:       rescue
127:         resolve_class_without_constantize(klass_name)
128:       end

[Source]

     # File lib/delayed/psych_ext.rb, line 93
 93:       def visit_Psych_Nodes_Mapping_with_class(object)
 94:         return revive(Psych.load_tags[object.tag], object) if Psych.load_tags[object.tag]
 95: 
 96:         case object.tag
 97:         when /^!ruby\/ActiveRecord:(.+)$/
 98:           klass = resolve_class($1)
 99:           payload = Hash[*object.children.map { |c| accept c }]
100:           id = payload["attributes"][klass.primary_key]
101:           begin
102:             if ActiveRecord::VERSION::MAJOR == 3
103:               klass.unscoped.find(id)
104:             else # Rails 2
105:               klass.with_exclusive_scope { klass.find(id) }
106:             end
107:           rescue ActiveRecord::RecordNotFound
108:             raise Delayed::DeserializationError
109:           end
110:         when /^!ruby\/Mongoid:(.+)$/
111:           klass = resolve_class($1)
112:           payload = Hash[*object.children.map { |c| accept c }]
113:           begin
114:             klass.find(payload["attributes"]["_id"])
115:           rescue Mongoid::Errors::DocumentNotFound
116:             raise Delayed::DeserializationError
117:           end
118:         else
119:           visit_Psych_Nodes_Mapping_without_class(object)
120:         end
121:       end

[Source]

    # File lib/delayed/psych_ext.rb, line 31
31:       def visit_Psych_Nodes_Scalar(o)
32:         @st[o.anchor] = o.value if o.anchor
33: 
34:         if klass = Psych.load_tags[o.tag]
35:           instance = klass.allocate
36: 
37:           if instance.respond_to?(:init_with)
38:             coder = Psych::Coder.new(o.tag)
39:             coder.scalar = o.value
40:             instance.init_with coder
41:           end
42: 
43:           return instance
44:         end
45: 
46:         return o.value if o.quoted
47:         return @ss.tokenize(o.value) unless o.tag
48: 
49:         case o.tag
50:         when '!binary', 'tag:yaml.org,2002:binary'
51:           o.value.unpack('m').first
52:         when '!str', 'tag:yaml.org,2002:str'
53:           o.value
54:         when "!ruby/object:DateTime"
55:           require 'date'
56:           @ss.parse_time(o.value).to_datetime
57:         when "!ruby/object:Complex"
58:           Complex(o.value)
59:         when "!ruby/object:Rational"
60:           Rational(o.value)
61:         when "!ruby/class", "!ruby/module"
62:           resolve_class o.value
63:         when "tag:yaml.org,2002:float", "!float"
64:           Float(@ss.tokenize(o.value))
65:         when "!ruby/regexp"
66:           o.value =~ /^\/(.*)\/([mixn]*)$/
67:           source  = $1
68:           options = 0
69:           lang    = nil
70:           ($2 || '').split('').each do |option|
71:             case option
72:             when 'x' then options |= Regexp::EXTENDED
73:             when 'i' then options |= Regexp::IGNORECASE
74:             when 'm' then options |= Regexp::MULTILINE
75:             when 'n' then options |= Regexp::NOENCODING
76:             else lang = option
77:             end
78:           end
79:           Regexp.new(*[source, options, lang].compact)
80:         when "!ruby/range"
81:           args = o.value.split(/([.]{2,3})/, 2).map { |s|
82:             accept Nodes::Scalar.new(s)
83:           }
84:           args.push(args.delete_at(1) == '...')
85:           Range.new(*args)
86:         when /^!ruby\/sym(bol)?:?(.*)?$/
87:           o.value.to_sym
88:         else
89:           @ss.tokenize o.value
90:         end
91:       end

[Validate]