Class: Psych::Visitors::ToRuby

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

Instance Method Summary collapse

Instance Method Details

#resolve_class_with_constantize(klass_name) ⇒ Object



74
75
76
77
78
# File 'lib/delayed/psych_ext.rb', line 74

def resolve_class_with_constantize(klass_name)
  klass_name.constantize
rescue
  resolve_class_without_constantize(klass_name)
end

#visit_Psych_Nodes_Mapping_with_class(object) ⇒ Object

rubocop:disable CyclomaticComplexity, MethodName



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
# File 'lib/delayed/psych_ext.rb', line 36

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]

  case object.tag
  when /^!ruby\/ActiveRecord:(.+)$/
    klass = resolve_class(Regexp.last_match[1])
    payload = Hash[*object.children.map { |c| accept c }]
    id = payload['attributes'][klass.primary_key]
    begin
      klass.unscoped.find(id)
    rescue ActiveRecord::RecordNotFound => error
      raise Delayed::DeserializationError, "ActiveRecord::RecordNotFound, class: #{klass}, primary key: #{id} (#{error.message})"
    end
  when /^!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 /^!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
    visit_Psych_Nodes_Mapping_without_class(object)
  end
end