Class: CallbackLocker

Inherits:
Object
  • Object
show all
Defined in:
lib/callback-locker.rb

Overview

Runs or defers callbacks according to its internal locked or unlocked state.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCallbackLocker

Constructor.



39
40
41
42
43
# File 'lib/callback-locker.rb', line 39

def initialize
    @syncing = false
    @locked = false
    @stack = [ ]
end

Instance Attribute Details

#stackArray

Holds callback stack.

Returns:

  • (Array)

    array of callbacks



32
33
34
# File 'lib/callback-locker.rb', line 32

def stack
  @stack
end

Instance Method Details

#lockObject Also known as: try_lock, lock!

Locks the locker. Note, it’s also alias for #try_lock which is multiple pass-in so sets lock although it’s locked.



51
52
53
# File 'lib/callback-locker.rb', line 51

def lock
    @locked = true
end

#locked?Boolean

Indicates, locker is locked.

Returns:

  • (Boolean)

    true if it’s, false otherwise



75
76
77
# File 'lib/callback-locker.rb', line 75

def locked?
    @locked
end

#synchronize { ... } ⇒ Object

Synchronizes using the lock. Works by similiar way as standard Mutex#synchronize.

Yields:

  • if locker is unlocked the given callback



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/callback-locker.rb', line 95

def synchronize(&block)
    if self.locked? or @syncing
        @stack << block
    else
        @syncing = true
        yield
        @syncing = false
        
        if not @stack.empty?
            self.eval!
        end
    end
end

#unlockObject Also known as: unlock!

Unlocks the locker.



63
64
65
66
# File 'lib/callback-locker.rb', line 63

def unlock
    @locked = false
    self.eval!
end

#unlocked?Boolean

Indicates, locker is unlocked.

Returns:

  • (Boolean)

    true if it’s, false otherwise



84
85
86
# File 'lib/callback-locker.rb', line 84

def unlocked?
    not self.locked?
end