<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>opensoul.org - Making #composed_of more useful Changes</title>
  <id>tag:opensoul.org,2009:/2006/11/16/making-code-composed_of-code-more-useful/changes</id>
  <generator uri="http://mephistoblog.com" version="0.8.0">Mephisto Drax</generator>
  <link href="http://opensoul.org/2006/11/16/making-code-composed_of-code-more-useful/changes.xml" rel="self" type="application/atom+xml"/>
  <link href="/2006/11/16/making-code-composed_of-code-more-useful" rel="alternate" type="text/html"/>
  <updated>2007-10-24T04:02:55Z</updated>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2007-10-24:380:4</id>
    <published>2006-11-16T00:54:00Z</published>
    <updated>2007-10-24T04:02:55Z</updated>
    <link href="http://opensoul.org/2006/11/16/making-code-composed_of-code-more-useful" rel="alternate" type="text/html"/>
    <title>Making #composed_of more useful</title>
<content type="html">&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; my patch &lt;a href=&quot;http://dev.rubyonrails.org/changeset/8003&quot;&gt;finally got added to edge rails&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Active Record allows you to abstract fields into an aggregate object by using the  &lt;a href=&quot;http://api.rubyonrails.org/classes/ActiveRecord/Aggregations/ClassMethods.html#M000829&quot;&gt;&lt;code&gt;composed_of&lt;/code&gt;&lt;/a&gt; declaration. This is handy, but the current implementation can be a real pain.&lt;/p&gt;


	&lt;p&gt;The first and somewhat trivial issue is the &lt;tt&gt;composed_of&lt;/tt&gt; declaration builds a string and evals it to define the attribute accessors.  It&#8217;s not a big deal, but it&#8217;s dirty.  The second issue is that aggregate objects are not easy to manipulate in Rails, especially in forms.&lt;/p&gt;


	&lt;h3&gt;Fixing it&lt;/h3&gt;


	&lt;p&gt;So, I&#8217;ve written a plugin that overrides the Active Record implementation of &lt;code&gt;composed_of&lt;/code&gt;, which allows you to specify a block to convert incoming parameters to the correct type.&lt;/p&gt;


	&lt;p&gt;Personally, this has been most helpful when using the &lt;a href=&quot;http://rubyforge.org/projects/money/&quot;&gt;Money gem&lt;/a&gt;:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;class Account &amp;lt; ActiveRecord::Base
  composed_of :balance, :class_name =&amp;gt; &amp;quot;Money&amp;quot;, :mapping =&amp;gt; %w(cents cents) do |amount|
      amount.to_money
  end
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;If &lt;code&gt;#balance=&lt;/code&gt; receives anything besides a &lt;code&gt;Money&lt;/code&gt; object, it will call the block to try to convert the parameter to a &lt;code&gt;Money&lt;/code&gt; object.&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&amp;gt;&amp;gt; account = Account.new :balance =&amp;gt; 100
&amp;gt;&amp;gt; account.balance
=&amp;gt; #&amp;amp;lt;Money:0x2612770 @cents=10000, @currency=&amp;quot;USD&amp;quot;&amp;amp;gt;&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;And now it can transparently be used in forms:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;irb&quot;&gt;  &amp;lt;%= text_field :account, :balance %&amp;gt;&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This can even be used for more advanced aggregations:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;class User &amp;lt; ActiveRecord::Base
  composed_of :address, :class_name =&amp;gt; &amp;quot;Address&amp;quot;
        :mapping =&amp;gt; [%w(street street), %w(city city), %w(state state), %w(zip zip)] do |addr|
    Address.new(addr[:street], addr[:city], addr[:state], addr[:zip])
  end
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;A user can now be created from a hash:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;  User.new(:address =&amp;gt; {:street =&amp;gt; &amp;quot;123 A Street&amp;quot;, :city =&amp;gt; &amp;quot;Somewhere&amp;quot;, :state =&amp;gt; &amp;quot;NO&amp;quot;, :zip =&amp;gt; 12345})&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;I didn&#8217;t quite realize all the implications of this extension until I was writing up the docs for this plugin.  Active Record magically does type casting for a limited set of types, namely dates and numbers.  But this essentially allows you to have a form of type casting for any attribute. Interesting&#8230;&lt;/p&gt;


	&lt;h3&gt;Installation&lt;/h3&gt;


	&lt;p&gt;This has also been submitted as a &lt;a href=&quot;http://dev.rubyonrails.org/ticket/6322&quot;&gt;patch to the Rails trac&lt;/a&gt; but hasn&#8217;t been accepted yet.&lt;/p&gt;


&lt;pre&gt;
  script/plugin install http://source.collectiveidea.com/public/rails/plugins/composed_of_conversion
&lt;/pre&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2006-11-16:169:3</id>
    <published>2006-11-16T00:32:00Z</published>
    <updated>2006-11-16T00:44:41Z</updated>
    <link href="http://opensoul.org/2006/11/16/making-code-composed_of-code-more-useful" rel="alternate" type="text/html"/>
    <title>Making #composed_of more useful</title>
<content type="html">&lt;p&gt;Active Record allows you to abstract fields into an aggregate object by using the  &lt;a href=&quot;http://api.rubyonrails.org/classes/ActiveRecord/Aggregations/ClassMethods.html#M000829&quot;&gt;&lt;code&gt;composed_of&lt;/code&gt;&lt;/a&gt; declaration. This is handy, but the current implementation can be a real pain.&lt;/p&gt;


	&lt;p&gt;The first and somewhat trivial issue is the &lt;tt&gt;composed_of&lt;/tt&gt; declaration builds a string and evals it to define the attribute accessors.  It&#8217;s not a big deal, but it&#8217;s dirty.  The second issue is that aggregate objects are not easy to manipulate in Rails, especially in forms.&lt;/p&gt;


	&lt;h3&gt;Fixing it&lt;/h3&gt;


	&lt;p&gt;So, I&#8217;ve written a plugin that overrides the Active Record implementation of &lt;code&gt;composed_of&lt;/code&gt;, which allows you to specify a block to convert incoming parameters to the correct type.&lt;/p&gt;


	&lt;p&gt;Personally, this has been most helpful when using the &lt;a href=&quot;http://rubyforge.org/projects/money/&quot;&gt;Money gem&lt;/a&gt;:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;class Account &amp;lt; ActiveRecord::Base
  composed_of :balance, :class_name =&gt; &quot;Money&quot;, :mapping =&gt; %w(cents cents) do |amount|
      amount.to_money
  end
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;If &lt;code&gt;#balance=&lt;/code&gt; receives anything besides a &lt;code&gt;Money&lt;/code&gt; object, it will call the block to try to convert the parameter to a &lt;code&gt;Money&lt;/code&gt; object.&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&gt;&gt; account = Account.new :balance =&gt; 100
&gt;&gt; account.balance
=&gt; #&amp;lt;Money:0x2612770 @cents=10000, @currency=&quot;USD&quot;&amp;gt;&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;And now it can transparently be used in forms:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;irb&quot;&gt;  &amp;lt;%= text_field :account, :balance %&gt;&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This can even be used for more advanced aggregations:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;class User &amp;lt; ActiveRecord::Base
  composed_of :address, :class_name =&gt; &quot;Address&quot; 
        :mapping =&gt; [%w(street street), %w(city city), %w(state state), %w(zip zip)] do |addr|
    Address.new(addr[:street], addr[:city], addr[:state], addr[:zip])
  end
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;A user can now be created from a hash:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;  User.new(:address =&gt; {:street =&gt; &quot;123 A Street&quot;, :city =&gt; &quot;Somewhere&quot;, :state =&gt; &quot;NO&quot;, :zip =&gt; 12345})&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;I didn&#8217;t quite realize all the implications of this extension until I was writing up the docs for this plugin.  Active Record magically does type casting for a limited set of types, namely dates and numbers.  But this essentially allows you to have a form of type casting for any attribute. Interesting&#8230;&lt;/p&gt;


	&lt;h3&gt;Installation&lt;/h3&gt;


	&lt;p&gt;This has also been submitted as a &lt;a href=&quot;http://dev.rubyonrails.org/ticket/6322&quot;&gt;patch to the Rails trac&lt;/a&gt; but hasn&#8217;t been accepted yet.&lt;/p&gt;


&lt;pre&gt;
  script/plugin install http://source.collectiveidea.com/public/rails/plugins/composed_of_conversion
&lt;/pre&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2006-11-16:168:2</id>
    <published>2006-11-16T00:32:00Z</published>
    <updated>2006-11-16T00:37:59Z</updated>
    <link href="http://opensoul.org/2006/11/16/making-code-composed_of-code-more-useful" rel="alternate" type="text/html"/>
    <title>Making #composed_of more useful</title>
<content type="html">&lt;p&gt;Active Record allows you to abstract fields into an aggregate object by using the  &lt;a href=&quot;http://api.rubyonrails.org/classes/ActiveRecord/Aggregations/ClassMethods.html#M000829&quot;&gt;&lt;code&gt;composed_of&lt;/code&gt;&lt;/a&gt; declaration. This is handy, but the current implementation can be a real pain.&lt;/p&gt;


	&lt;p&gt;The first and somewhat trivial issue is the &lt;tt&gt;composed_of&lt;/tt&gt; declaration builds a string and evals it to define the attribute accessors.  It&#8217;s not a big deal, but it&#8217;s dirty.  The second issue is that aggregate objects are not easy to manipulate in Rails, especially in forms.&lt;/p&gt;


	&lt;p&gt;So, I&#8217;ve written a plugin that overrides the Active Record implementation of &lt;code&gt;composed_of&lt;/code&gt;, which allows you to specify a block to convert incoming parameters to the correct type.&lt;/p&gt;


	&lt;p&gt;Personally, this has been most helpful when using the &lt;a href=&quot;http://rubyforge.org/projects/money/&quot;&gt;Money gem&lt;/a&gt;:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;class Account &amp;lt; ActiveRecord::Base
  composed_of :balance, :class_name =&gt; &quot;Money&quot;, :mapping =&gt; %w(cents cents) do |amount|
      amount.to_money
  end
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;If &lt;code&gt;#balance=&lt;/code&gt; receives anything besides a &lt;code&gt;Money&lt;/code&gt; object, it will call the block to try to convert the parameter to a &lt;code&gt;Money&lt;/code&gt; object.&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&gt;&gt; account = Account.new :balance =&gt; 100
&gt;&gt; account.balance
=&gt; #&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;And now it can transparently be used in forms:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;irb&quot;&gt;  &amp;lt;%= text_field :account, :balance %&gt;&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This can even be used for more advanced aggregations:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;class User &amp;lt; ActiveRecord::Base
  composed_of :address, :class_name =&gt; &quot;Address&quot; 
        :mapping =&gt; [%w(street street), %w(city city), %w(state state), %w(zip zip)] do |addr|
    Address.new(addr[:street], addr[:city], addr[:state], addr[:zip])
  end
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;A user can now be created from a hash:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;  User.new(:address =&gt; {:street =&gt; &quot;123 A Street&quot;, :city =&gt; &quot;Somewhere&quot;, :state =&gt; &quot;NO&quot;, :zip =&gt; 12345})&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;I didn&#8217;t quite realize all the implications of this extension until I was writing up the docs for this plugin.  Active Record magically does type casting for a limited set of types, namely dates and numbers.  But this essentially allows you to have a form of type casting for any attribute. Interesting&#8230;&lt;/p&gt;


	&lt;h3&gt;Installation&lt;/h3&gt;


&lt;pre&gt;
  script/plugin install http://source.collectiveidea.com/public/rails/plugins/composed_of_conversion
&lt;/pre&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2006-11-16:167:1</id>
    <published>2006-11-16T00:32:08Z</published>
    <updated>2006-11-16T00:32:09Z</updated>
    <link href="http://opensoul.org/2006/11/16/making-code-composed_of-code-more-useful" rel="alternate" type="text/html"/>
    <title>Making #composed_of more useful</title>
<content type="html">&lt;p&gt;Active Record allows you to abstract fields into an aggregate object by using the  &lt;a href=&quot;http://api.rubyonrails.org/classes/ActiveRecord/Aggregations/ClassMethods.html#M000829&quot;&gt;&lt;code&gt;composed_of&lt;/code&gt;&lt;/a&gt; declaration. This is handy, but the current implementation can be a real pain.&lt;/p&gt;


	&lt;p&gt;The first and somewhat trivial issue is the &lt;tt&gt;composed_of&lt;/tt&gt; declaration builds a string and evals it to define the attribute accessors.  It&#8217;s not a big deal, but it&#8217;s dirty.  The second issue is that aggregate objects are not easy to manipulate in Rails, especially in forms.&lt;/p&gt;


	&lt;p&gt;So, I&#8217;ve written a plugin that overrides the Active Record implementation of &lt;code&gt;composed_of&lt;/code&gt;, which allows you to specify a block to convert incoming parameters to the correct type.&lt;/p&gt;


	&lt;p&gt;Personally, this has been most helpful when using the &lt;a href=&quot;http://rubyforge.org/projects/money/&quot;&gt;Money gem&lt;/a&gt;:&lt;/p&gt;


	&lt;p&gt;If &lt;code&gt;#balance=&lt;/code&gt; receives anything besides a &lt;code&gt;Money&lt;/code&gt; object, it will call the block to try to convert the parameter to a &lt;code&gt;Money&lt;/code&gt; object.&lt;/p&gt;


	&lt;p&gt;And now it can transparently be used in forms:&lt;/p&gt;


	&lt;p&gt;This can even be used for more advanced aggregations:&lt;/p&gt;


	&lt;p&gt;A user can now be created from a hash:&lt;/p&gt;


	&lt;p&gt;I didn&#8217;t quite realize all the implications of this extension until I was writing up the docs for this plugin.  Active Record magically does type casting for a limited set of types, namely dates and numbers.  But this essentially allows you to have a form of type casting for any attribute. Interesting&#8230;&lt;/p&gt;


	&lt;h3&gt;Installation&lt;/h3&gt;


&lt;pre&gt;
  script/plugin install http://source.collectiveidea.com/public/rails/plugins/composed_of_conversion
&lt;/pre&gt;</content>  </entry>
</feed>
