Class: Delayed::PsychExt::ToRuby

Inherits:
Psych::Visitors::ToRuby
  • Object
show all
Defined in:
lib/delayed/psych_ext.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.createObject



25
26
27
# File 'lib/delayed/psych_ext.rb', line 25

def self.create
  new
end

Instance Method Details

#jruby_is_seriously_borkedObject

defined? is triggering something really messed up in jruby causing both the if AND else clauses to execute, however if the check is run here, everything is fine



84
85
86
# File 'lib/delayed/psych_ext.rb', line 84

def jruby_is_seriously_borked
  defined?(ActiveRecord::Base)
end

#resolve_class(klass_name) ⇒ Object



88
89
90
91
92
93
# File 'lib/delayed/psych_ext.rb', line 88

def resolve_class(klass_name)
  return nil if !klass_name || klass_name.empty?
  klass_name.constantize
rescue
  super
end

#visit_Psych_Nodes_Mapping(object) ⇒ Object

rubocop:disable CyclomaticComplexity, MethodName, PerceivedComplexity



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/delayed/psych_ext.rb', line 30

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

  case object.tag
  when %r{^!ruby/object}
    result = super
    if jruby_is_seriously_borked && result.is_a?(ActiveRecord::Base)
      klass = result.class
      id = result[klass.primary_key]
      begin
        klass.unscoped.find(id)
      rescue ActiveRecord::RecordNotFound => error # rubocop:disable BlockNesting
        raise Delayed::DeserializationError, "ActiveRecord::RecordNotFound, class: #{klass}, primary key: #{id} (#{error.message})"
      end
    else
      result
    end
  when %r{^!ruby/ActiveRecord:(.+)$}
    klass = resolve_class(Regexp.last_match[1])
    payload = Hash[*object.children.map { |c| accept c }]
    id = payload['attributes'][klass.primary_key]
    id = id.value if defined?(ActiveRecord::Attribute) && id.is_a?(ActiveRecord::Attribute)
    begin
      klass.unscoped.find(id)
    rescue ActiveRecord::RecordNotFound => error
      raise Delayed::DeserializationError, "ActiveRecord::RecordNotFound, class: #{klass}, primary key: #{id} (#{error.message})"
    end
  when %r{^!ruby/Mongoid:(.+)$}
    klass = resolve_class(Regexp.last_match[1])
    payload = Hash[*object.children.map { |c| accept c }]
    id = payload['attributes']['_id']
    begin
      klass.find(id)
    rescue Mongoid::Errors::DocumentNotFound => error
      raise Delayed::DeserializationError, "Mongoid::Errors::DocumentNotFound, class: #{klass}, primary key: #{id} (#{error.message})"
    end
  when %r{^!ruby/DataMapper:(.+)$}
    klass = resolve_class(Regexp.last_match[1])
    payload = Hash[*object.children.map { |c| accept c }]
    begin
      primary_keys = klass.properties.select(&:key?)
      key_names = primary_keys.map { |p| p.name.to_s }
      klass.get!(*key_names.map { |k| payload['attributes'][k] })
    rescue DataMapper::ObjectNotFoundError => error
      raise Delayed::DeserializationError, "DataMapper::ObjectNotFoundError, class: #{klass} (#{error.message})"
    end
  else
    super
  end
end