Module: Tusk::Observable::DRb

Defined in:
lib/tusk/observable/drb.rb

Overview

An observer implementation for DRb. This module requires that you start a DRb server, which can be done via Server.start

This observer works across processes.

Example:

require 'tusk/observable/drb'

class Timer
  include Tusk::Observable::DRb

  # Start the DRb server. Do this once
  Thread.new { Server.start }

  def tick
    changed
    notify_observers
  end
end

class Listener
  def update
    puts "got update"
  end
end

timer = Timer.new

fork do
  timer.add_observer Listener.new
  sleep # put the process to sleep so it doesn't exit
end

loop do
  timer.tick
  sleep 1
end

Defined Under Namespace

Classes: Proxy, Server

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/tusk/observable/drb.rb', line 93

def self.extended klass
  super

  klass.instance_eval do
    @bus = DRbObject.new_with_uri uri
    @observer_state = false
    @subscribers      = {}
  end
end

Instance Method Details

#add_observer(object, func = :update) ⇒ Object

Add observer as an observer to this object. The object will receive a notification when #changed? returns true and #notify_observers is called.

func method is called on object when notifications are sent.



116
117
118
119
120
121
122
123
124
# File 'lib/tusk/observable/drb.rb', line 116

def add_observer object, func = :update
  unless ::DRb.thread && ::DRb.thread.alive?
    ::DRb.start_service
  end

  proxy = Proxy.new object, func
  @subscribers[object] = proxy
  @bus.watch channel, proxy
end

#changed(state = true) ⇒ Object

Set the changed state of this object. Notifications will be sent only if the changed state is a truthy object.



156
157
158
# File 'lib/tusk/observable/drb.rb', line 156

def changed state = true
  @observer_state = state
end

#changed?Boolean

Returns true if this object's state has been changed since the last call to #notify_observers.

Returns:

  • (Boolean)


150
151
152
# File 'lib/tusk/observable/drb.rb', line 150

def changed?
  @observer_state
end

#count_observersObject

Returns the number of observers associated with this object in the current process. If the object is observed across multiple processes, the returned count will not reflect the other processes.



163
164
165
# File 'lib/tusk/observable/drb.rb', line 163

def count_observers
  @subscribers.length
end

#delete_observer(o) ⇒ Object

Remove observer so that it will no longer receive notifications.



143
144
145
146
# File 'lib/tusk/observable/drb.rb', line 143

def delete_observer o
  proxy = @subscribers.delete o
  @bus.delete_observer channel, proxy
end

#delete_observersObject

Remove all observers associated with this object in the current process. This method will not impact observers of this object in other processes.



137
138
139
140
# File 'lib/tusk/observable/drb.rb', line 137

def delete_observers
  @bus.delete channel
  @subscribers.clear
end

#initialize(*args) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/tusk/observable/drb.rb', line 103

def initialize *args
  super

  @bus = DRbObject.new_with_uri uri
  @observer_state = false
  @subscribers      = {}
end

#notify_observers(*args) ⇒ Object

If this object's #changed? state is true, this method will notify observing objects.



128
129
130
131
132
# File 'lib/tusk/observable/drb.rb', line 128

def notify_observers(*args)
  return unless changed?
  @bus.signal channel, args
  changed false
end