Module: ActiveRecord::Userstamp::Stamper::InstanceMethods

Defined in:
lib/active_record/userstamp/stamper.rb

Instance Method Summary collapse

Instance Method Details

#reset_stamperObject

Sets the stamper back to nil to prepare for the next request.



31
32
33
# File 'lib/active_record/userstamp/stamper.rb', line 31

def reset_stamper
  self.stamper = nil
end

#stamperObject

Retrieves the existing stamper.



26
27
28
# File 'lib/active_record/userstamp/stamper.rb', line 26

def stamper
  Thread.current[stamper_identifier]
end

#stamper=(object_id) ⇒ Fixnum #stamper=(object) ⇒ ActiveRecord::Base

Used to set the current stamper for this model.

Overloads:

  • #stamper=(object_id) ⇒ Fixnum

    Parameters:

    • object_id (Fixnum)

      The ID of the stamper.

    Returns:

    • (Fixnum)
  • #stamper=(object) ⇒ ActiveRecord::Base

    Parameters:

    • object (ActiveRecord::Base)

      The stamper object.

    Returns:

    • (ActiveRecord::Base)


21
22
23
# File 'lib/active_record/userstamp/stamper.rb', line 21

def stamper=(object)
  Thread.current[stamper_identifier] = object
end

#with_stamper(stamper) ⇒ Object

For the duration that execution is within the provided block, the stamper for this class would be the specified value.

This replaces the #stamper= and #reset_stamper pair because this guarantees exception safety.



40
41
42
43
44
45
46
# File 'lib/active_record/userstamp/stamper.rb', line 40

def with_stamper(stamper)
  old_stamper = self.stamper
  self.stamper = stamper
  yield
ensure
  self.stamper = old_stamper
end