<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: listener code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 01:18:46 GMT</pubDate>
    <description>DZone Snippets: listener code</description>
    <item>
      <title>Introduction to Distributed Ruby</title>
      <link>http://snippets.dzone.com/posts/show/5233</link>
      <description>This code demonstrates a client server architecture. I executed the file simple_service.rb on my Ubuntu server (Donatello - 192.168.1.10), then from the CLI output I copied the server uri into the clipboard. I then executed the simple_client.rb on my Ubuntu desktop (Cryton - 192.168.1.3) while passing in the uri as an argument. &lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby -w&lt;br /&gt;# simple_service.rb&lt;br /&gt;# A simple DRb service&lt;br /&gt;&lt;br /&gt;# load DRb&lt;br /&gt;require 'drb'&lt;br /&gt;&lt;br /&gt;# start up the DRb service&lt;br /&gt;DRb.start_service nil, []&lt;br /&gt;&lt;br /&gt;# We need the uri of the service to connect a client&lt;br /&gt;puts DRb.uri&lt;br /&gt;&lt;br /&gt;# wait for the DRb service to finish before exiting&lt;br /&gt;DRb.thread.join&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;output: druby://donatello.mydomain.com:47159&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby -w&lt;br /&gt;# simple_client.rb&lt;br /&gt;# A simple DRb client&lt;br /&gt;&lt;br /&gt;require 'drb'&lt;br /&gt;&lt;br /&gt;DRb.start_service&lt;br /&gt;&lt;br /&gt;# attach to the DRb server via a URI given on the command line&lt;br /&gt;remote_array = DRbObject.new nil, ARGV.shift&lt;br /&gt;&lt;br /&gt;puts remote_array.size&lt;br /&gt;&lt;br /&gt;remote_array &lt;&lt; 1&lt;br /&gt;&lt;br /&gt;puts remote_array.size&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;from the command line&lt;br /&gt;&gt; ./simple_client.rb druby://192.168.1.10:47159&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;output:&lt;br /&gt;0&lt;br /&gt;1&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Note: I substituted the domain name with the ip address because the name in question was not stored within the DNS settings.&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://segment7.net/projects/ruby/drb/introduction.html"&gt;Introduction to Distributed Ruby (DRb)&lt;/a&gt; [segment7.net]</description>
      <pubDate>Sat, 15 Mar 2008 00:59:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5233</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Property change listener</title>
      <link>http://snippets.dzone.com/posts/show/3480</link>
      <description>How to implement a Property change listener on ruby class&lt;br /&gt;Origin : http://rawblock.blogspot.com/2007/02/transparent-property-change-listeners.html &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class ListenerSupport&lt;br /&gt; &lt;br /&gt;  def self.listen_to(klass, *properties)&lt;br /&gt;    setup(klass)&lt;br /&gt;    properties.each do |p|&lt;br /&gt;      klass.instance_eval do&lt;br /&gt;        #rename original method&lt;br /&gt;        alias_method "#{p}_orig=", "#{p}="&lt;br /&gt;        #create proxy method in it's place that will fire&lt;br /&gt;        #changes, and delegate to the original implementation&lt;br /&gt;        define_method "#{p}=" do |value|&lt;br /&gt;          old_value = send "#{p}"&lt;br /&gt;          send "#{p}_orig=",value&lt;br /&gt;          fire_property_changed p, old_value, value&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt; &lt;br /&gt;  def self.setup(klass)&lt;br /&gt;    klass.instance_eval do&lt;br /&gt;     &lt;br /&gt;      define_method :fire_property_changed do |prop, pre, post|&lt;br /&gt;        return if pre == post&lt;br /&gt;        eval %{&lt;br /&gt;          @listeners[prop].each do |l|&lt;br /&gt;              l.property_changed(self, prop, pre, post)&lt;br /&gt;          end unless @listeners.nil?&lt;br /&gt;        }&lt;br /&gt;      end unless instance_methods.include? :fire_property_changed&lt;br /&gt;     &lt;br /&gt;      define_method :add_property_listener do |prop, listener|&lt;br /&gt;        eval %{&lt;br /&gt;          @listeners = {} if @listeners.nil?&lt;br /&gt;          prop_listeners = @listeners.include?(prop) ? @listeners[prop] : []&lt;br /&gt;          @listeners[prop] = prop_listeners&lt;br /&gt;          prop_listeners &lt;&lt; listener unless prop_listeners.include? listener&lt;br /&gt;        }&lt;br /&gt;      end unless instance_methods.include? :add_property_listener&lt;br /&gt;     &lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt; &lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The general idea is that the original class is modified so that all properties that are listened to have their accessor write methods intercepted, and any property change listeners are notified when that property changes.&lt;br /&gt;&lt;br /&gt;Let's walk through how it works:&lt;br /&gt;&lt;br /&gt;    * A standard Ruby class with property accessors is created. e.g.&lt;br /&gt;&lt;br /&gt;      class RubyBean&lt;br /&gt;        attr_accessor :foo, :bar&lt;br /&gt;      end&lt;br /&gt;&lt;br /&gt;    * A call to ListenerSupport.listen_to is made to modify the class to allow properties to be listened to.&lt;br /&gt;&lt;br /&gt;      ListenerSupport.listen_to RubyBean, :foo, :bar&lt;br /&gt;&lt;br /&gt;    * listen_to adds methods add_property_listener and fire_property_changed to allow listeners to be added to RubyBean for specific properties.&lt;br /&gt;    * listen_to then aliases the original methods foo= and bar= to foo_orig= and bar_orig=. New methods for foo= and bar= are defined that delegate the work to the original method, then call fire_property_changed to notify the listeners.&lt;br /&gt;    * Listeners are called, and any custom action (such as GUI component binding) can be carried out.&lt;br /&gt;</description>
      <pubDate>Thu, 08 Feb 2007 16:00:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3480</guid>
      <author>Mickael (Mickael)</author>
    </item>
    <item>
      <title>Javascript Window OnLoad Listener</title>
      <link>http://snippets.dzone.com/posts/show/3282</link>
      <description>This script gives the ability to run javascript functions after the page has loaded.  I definitely can't take credit for this, as it's one I found, but I use it constantly.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;window.onloadListeners=new Array();&lt;br /&gt;&lt;br /&gt;window.addOnLoadListener = function (listener) {&lt;br /&gt;	window.onloadListeners[window.onloadListeners.length]=listener;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;window.onload=function(){&lt;br /&gt;	for(var i=0;i&lt;window.onloadListeners.length;i++){&lt;br /&gt;		func = window.onloadListeners[i];&lt;br /&gt;		func.call();&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;//window.addOnLoadListener(function);&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 14 Jan 2007 12:19:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3282</guid>
      <author>bgidge (Bryan Gidge)</author>
    </item>
  </channel>
</rss>
