<?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>Wed, 20 Aug 2008 09:21:24 GMT</pubDate>
    <description>DZone Snippets: listener code</description>
    <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>
  </channel>
</rss>
