<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>TheWebFellas - Ruby on Rails Development Specialists UK - Giving composed_of some much needed lovin&#8217; Comments</title>
  <id>tag:thewebfellas.com,2008:/blog/2008/8/24/giving-composed_of-some-much-needed-lovin/comments</id>
  <generator version="0.7.3" uri="http://mephistoblog.com">Mephisto Noh-Varr</generator>
  <link href="http://thewebfellas.com/blog/2008/8/24/giving-composed_of-some-much-needed-lovin/comments.xml" rel="self" type="application/atom+xml"/>
  <link href="/blog/2008/8/24/giving-composed_of-some-much-needed-lovin" rel="alternate" type="text/html"/>
  <updated>2008-10-23T21:58:18Z</updated>
  <entry xml:base="http://thewebfellas.com/">
    <author>
      <name>Rob Anderton</name>
    </author>
    <id>tag:thewebfellas.com,2008-08-24:1384:2156</id>
    <published>2008-10-23T21:58:18Z</published>
    <updated>2008-10-23T21:58:18Z</updated>
    <category term="Blog"/>
    <link href="http://thewebfellas.com/blog/2008/8/24/giving-composed_of-some-much-needed-lovin" rel="alternate" type="text/html"/>
    <title>Comment on 'Giving composed_of some much needed lovin&#8217;' by Rob Anderton</title>
<content type="html">&lt;p&gt;Hi - I haven't yet but I'll see if I can knock something together over the weekend for people on Rails 2.0/2.1. It looks like Rails 2.2 is just around the corner so it won't be long until the new, improved composed_of appears in an official release!&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://thewebfellas.com/">
    <author>
      <name>nachokb</name>
    </author>
    <id>tag:thewebfellas.com,2008-08-24:1384:2139</id>
    <published>2008-10-22T18:19:33Z</published>
    <updated>2008-10-22T18:19:33Z</updated>
    <category term="Blog"/>
    <link href="http://thewebfellas.com/blog/2008/8/24/giving-composed_of-some-much-needed-lovin" rel="alternate" type="text/html"/>
    <title>Comment on 'Giving composed_of some much needed lovin&#8217;' by nachokb</title>
<content type="html">&lt;p&gt;Did you release a plugin for Rails 2.1?  Do you have a monkey-patch version of it (against 2.1)?&lt;/p&gt;

&lt;p&gt;Thanks!&lt;/p&gt;

&lt;p&gt;-- nachokb&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://thewebfellas.com/">
    <author>
      <name>Rob Anderton</name>
    </author>
    <id>tag:thewebfellas.com,2008-08-24:1384:1793</id>
    <published>2008-09-11T10:25:25Z</published>
    <updated>2008-09-11T10:25:25Z</updated>
    <category term="Blog"/>
    <link href="http://thewebfellas.com/blog/2008/8/24/giving-composed_of-some-much-needed-lovin" rel="alternate" type="text/html"/>
    <title>Comment on 'Giving composed_of some much needed lovin&#8217;' by Rob Anderton</title>
<content type="html">&lt;p&gt;Great news - my patch &lt;a href=&quot;http://rails.lighthouseapp.com/projects/8994/tickets/892-composed_of-constructor-and-converter-options#ticket-892-8&quot; title=&quot;See the patch ticket&quot;&gt;made it into core&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;I updated the original patch with additional documentation and also took the opportunity to tweak some of the existing documentation too, so hopefully the options for &lt;code&gt;composed_of&lt;/code&gt; are now a little bit clearer.&lt;/p&gt;

&lt;p&gt;Perhaps now &lt;code&gt;composed_of&lt;/code&gt; can enjoy a little more popularity.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://thewebfellas.com/">
    <author>
      <name>Rob Anderton</name>
    </author>
    <id>tag:thewebfellas.com,2008-08-24:1384:1477</id>
    <published>2008-08-27T09:51:06Z</published>
    <updated>2008-08-27T09:51:06Z</updated>
    <category term="Blog"/>
    <link href="http://thewebfellas.com/blog/2008/8/24/giving-composed_of-some-much-needed-lovin" rel="alternate" type="text/html"/>
    <title>Comment on 'Giving composed_of some much needed lovin&#8217;' by Rob Anderton</title>
<content type="html">&lt;p&gt;Yay! Some feedback has been received from &lt;a href=&quot;http://www.koziarski.net/&quot; title=&quot;Visit Michael’s site&quot;&gt;Michael Koziarski&lt;/a&gt; on Lighthouse and from &lt;a href=&quot;http://cho.hapgoods.com/wordpress&quot; title=&quot;Visit Chris’ site&quot;&gt;Chris Cruft&lt;/a&gt;, author of the original patches for composed_of that I linked to above, on the &lt;a href=&quot;http://groups.google.com/group/rubyonrails-core&quot; title=&quot;Visit the mailing list&quot;&gt;Rails core mailing list&lt;/a&gt;. It emerges from this feedback that my IP address example doesn’t fully illustrate why there is a need for separate constructors and converters, so to clarify:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code&gt;:constructor&lt;/code&gt; is called when instantiating the aggregate object and is therefore passed all of the mapped attributes in the order they are defined in the &lt;code&gt;:mapping&lt;/code&gt; option.&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;:converter&lt;/code&gt; is called when a value is assigned to the aggregate attribute and therefore passed the single value that is used in the assignment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s an example, that I’ve also added to my &lt;a href=&quot;http://rails.lighthouseapp.com/projects/8994/tickets/892-composed_of-constructor-and-converter-options&quot; title=&quot;See the patch&quot;&gt;patch ticket&lt;/a&gt;, that hopefully makes this difference more obvious.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;class NetworkResource &amp;lt; ActiveRecord::Base

 composed_of :cidr,
             :class_name =&amp;gt; 'NetAddr::CIDR',
             :mapping =&amp;gt; [ %w(network_address network), %w(cidr_range bits) ],
             :allow_nil =&amp;gt; true,
             :constructor =&amp;gt; Proc.new { |network_address, cidr_range| NetAddr::CIDR.create(&quot;#{network_address}/#{cidr_range}&quot;) },
             :converter =&amp;gt; Proc.new { |value| NetAddr::CIDR.create(value.is_a?(Array) ? value.join('/') : value) }

end

# Calls the :constructor proc
n = NetworkResource.new(:network_address =&amp;gt; '192.168.0.1', :cidr_range =&amp;gt; 24)

# Calls the :converter proc
n.cidr = [ '192.168.2.1', 8 ]
n.cidr = '192.168.0.1/24'

# Doesn't call the :converter proc as the class matches the aggregate class
n.cidr = NetAddr::CIDR.create('192.168.2.1/8')

# Save and reload - uses the :constructor proc on reload
n.save
n.reload&lt;/code&gt;&lt;/pre&gt;</content>  </entry>
</feed>
