Class: Sidekiq::SerializedCurrentAttributes::Load

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::ServerMiddleware
Defined in:
lib/sidekiq/serialized_current_attributes.rb

Instance Method Summary collapse

Constructor Details

#initialize(cattrs) ⇒ Load

Returns a new instance of Load.



47
48
49
# File 'lib/sidekiq/serialized_current_attributes.rb', line 47

def initialize(cattrs)
  @cattrs = cattrs
end

Instance Method Details

#call(_, job, _, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/sidekiq/serialized_current_attributes.rb', line 51

def call(_, job, _, &block)
  cattrs_to_reset = []

  @cattrs.each do |(key, strklass)|
    if job.has_key?(key)
      constklass = strklass.constantize
      cattrs_to_reset << constklass

      attributes = deserialize(job[key])
      attributes.each do |(attribute, value)|
        constklass.public_send("#{attribute}=", value)
      end
    end
  end

  yield
ensure
  cattrs_to_reset.each(&:reset)
end

#deserialize(attributes) ⇒ Object



71
72
73
74
# File 'lib/sidekiq/serialized_current_attributes.rb', line 71

def deserialize(attributes)
  deserialized_values = ActiveJob::Arguments.deserialize(attributes.values)
  attributes.keys.zip(deserialized_values).to_h
end