Ruby Fleebie

Rediscovering the joy of programming

XMPP4r 0.4 has been released

It’s been a while since the latest release of XMPP4r. I was starting to think that the development for this great library had been stopped.

Fortunately, version 0.4 has been released on August 5th 2008. We’re going to try this new version internally and eventually use it for TimmyOnTime. I am personnally hoping for less memory consumption, more speed and more stability. This new version highlights are :

  1. The beginning of ruby 1.9 support
  2. Refactoring of error classes (I’m really looking forward to this)

Here is the full changelog :

XMPP4R 0.4 (05/08/2008)
=======================
* Initial support for Ruby 1.9 (see README_ruby19.txt)
* Complete PubSub API Change – more logical and better for
childclasses, support for collection node creation
* a Helper to assist with XEP-0115 Entity Capabilities
* SASL anonymous support
* File transfer fixes
* MUC room configuration fixes
* initial support for XEP-0118 User Tune
* fix for an xmlrpc exception-during-serialisation bug, which would cause
a hang
* Support auto-gem generation on GitHub with improved and DRY’er RakeFile and
gemspec.
* Add support for the old SSL protocol (needed to connect to GTalk)
* Changed API for Client, Component, Connection, Stream to remove
need for antiquated ‘threaded’ param in the initializer.
* Use a Logger instance instead of directly writing to stdout
* Re-factored & consolidated Error classes. See xmpp4r/errors.rb for all
custom errors that can be caught. All inherit from Jabber::Error which
itself inherits from Ruby’s StandardError. This is a first step in
re-factoring errors. The next step will be to convert all ‘raise’ calls to
raise a custom Jabber::Error or one of its children instead of anonymous
RuntimeErrors. This allows much more granularity in catching and handling
errors later.
If you were catching Jabber::ErrorException before you should probably
change that in your code to now catch Jabber::Error if you want to
catch everything or one of the custom children of Jabber::Error defined in
‘lib/xmpp4r/errors.rb’. Additionally, the Error class which encapsulated
the xmpp error response, has been renamed to ErrorResponse to reflect its
real usage. This free’s up ‘Jabber::Error’ for use as our base Error class.

Get the intersection of 2 arrays

Pretty easy stuff today, but this is not something you may need to do often so maybe you don’t know how to do it.

Say I have these 2 arrays :

colors1 = [:blue, :red, :green, :orange, :purple]
colors2 = [:yellow, :cyan, :green, :blue, :purple]

Now what if I want a new array that contains only the elements present in both arrays?

beautiful_colors = colors1 & colors2
#beautiful_colors contains [:blue, :green, :purple]

Have a nice weekend!

Need support for has_many through with has_one in the “bridge table”

Let me expose the problem with an example :

Cart model (as in : shopping cart)

class Cart < ActiveRecord::Base
  has_one :bill
  belongs_to :user
end

Bill model

class Bill < ActiveRecord::Base
  belongs_to :cart
end

User

class User < ActiveRecord::Base
  has_many :carts
  has_many :bills, :through => :carts #<== Doesn't work... no user.bills for you!
end

Basically, because the "through" table links the destination table with a has_one relationship, ActiveRecord complains with : Invalid source reflection macro :has_one for has_many :bills, :through => :carts. Use :source to specify the source reflection.

Someone on Rails Trac already created a ticket about this issue 2 years ago. Can we expect this feature to be officially supported in the future? Let's hope so.