<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-6590239117348358350</atom:id><lastBuildDate>Fri, 04 Oct 2024 21:36:21 +0000</lastBuildDate><title>Coding Kensho</title><description></description><link>http://codingkensho.blogspot.com/</link><managingEditor>noreply@blogger.com (John G. Ferguson)</managingEditor><generator>Blogger</generator><openSearch:totalResults>7</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6590239117348358350.post-5734116592483727251</guid><pubDate>Sat, 14 May 2011 15:53:00 +0000</pubDate><atom:updated>2011-05-14T09:35:09.976-07:00</atom:updated><title>Moving to Tumblr</title><description>Basically, if anyone is following this blog I wanted to let you know I am moving to Tumblr.  I am changing the focus of my blog to be more about design, drawing, Ruby, Rails and things like that.  Seemed like something to try and I have a friend in the business using Tumblr and find I agree with him a lot of things... so why not?&lt;br /&gt;&lt;br /&gt;You can now find me at: http://www.aninvisiblefriend.com/&lt;br /&gt;&lt;br /&gt;Thanks for reading!</description><link>http://codingkensho.blogspot.com/2011/05/moving-to-tumblr.html</link><author>noreply@blogger.com (John G. Ferguson)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6590239117348358350.post-9032671101113453789</guid><pubDate>Fri, 08 Oct 2010 14:50:00 +0000</pubDate><atom:updated>2010-10-08T14:07:37.569-07:00</atom:updated><title>Slacker Duck Typing for Properties in Java</title><description>So the whole &quot;walking, quacking, and giving advice on accident insurance&quot; thing that ducks are know to do is not only a &quot;cool&quot; thing but a sanity thing.  Once you have written your Nth DTO to Domain Object transformer, you get the feeling that you need some kind of framework to solve this problem.   Why do you even need transformers?  Well glad you asked.  Also, what about that title, how does a transformer relate to Duck Typing?   I&#39;ll get to that RSN.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;Why Transformers?&lt;/span&gt;&lt;br /&gt;Well it turns out that sometimes in Java land, where typing is static and strict, you need to deal with an ungainly domain object in sane, little pieces.  Imagine a &quot;roll-up&quot; inheritance scenario where one DB row is N classes (with a type selector column).   Imagine you have logic that wants to deal with specific types.  What is an easy way to get that data from the blob to the type of interest?&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;Slacker Duck Typing&lt;/span&gt;&lt;br /&gt;Given our problem statement,  here is my suggestion for using BeanUtils to implement what I like to call Slacker Duck Typing.  Let&#39;s assume we have a blobby type called Thingy,  which for some reason is an instance of roll-up inheritance and is declared like this (I have omitted the getter/setters for each class to save space):&lt;br /&gt;&lt;pre&gt;public class Thingy {&lt;br /&gt;  private String type;&lt;br /&gt;  private String name;&lt;br /&gt;  private String[] dna;&lt;br /&gt;  private Integer numOfCores;&lt;br /&gt;&lt;br /&gt;  public Thingy(String type, String name, Integer numOfCores, String[] dna) {&lt;br /&gt;      this.type = type;&lt;br /&gt;      this.name = name;&lt;br /&gt;      this.numOfCores = numOfCores;&lt;br /&gt;      this.dna = dna;&lt;br /&gt;  }&lt;br /&gt; // getter/setter pairs were not put here to save space.&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;Further assume we have these two types: Human and Robot:&lt;br /&gt;&lt;pre&gt;    public static class Human {&lt;br /&gt;   private String name;&lt;br /&gt;   private String[] dna;&lt;br /&gt;// getter/setter pairs were not put here to save space.&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static class Robot {&lt;br /&gt;   private String name;&lt;br /&gt;   private Integer numOfCores;&lt;br /&gt;// getter/setter pairs were not put here to save space.&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now, imagine that you need to unpack the blob class and you want to unpack it into the correct real type.  You&#39;ll notice I did not use inheritance because that would be wrong.  LSP would be violated if I asked a human for their &lt;code&gt;numOfCores&lt;/code&gt; or a robot for their &lt;code&gt;dna&lt;/code&gt;.    Also, Java won&#39;t let me narrow the scope of the property methods of Thingy.    &lt;br /&gt;&lt;br /&gt;What we need here is Duck Typing, we need to be able to assume if a class has some behavior (methods) then we should not care about the type and just invoke the methods.  In our case, Human has a &lt;code&gt;name&lt;/code&gt; and it has &lt;code&gt;dna&lt;/code&gt; which not-so-coincidently so does Thingy.  Hmmm, so let&#39;s follow the rule that if the types cannot be related at least let the property names be the same.&lt;br /&gt;&lt;br /&gt;Now, here&#39;s the slacker part shown in the &lt;code&gt;main&lt;/code&gt;:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt; public static void main (String[] args) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {&lt;br /&gt;&lt;br /&gt;     Thingy thingy = new Thingy(&quot;H&quot;, &quot;Carbon Unit 100&quot;, null, new String[]{&quot;CTGA&quot;, &quot;CTTG&quot;, &quot;AGTC&quot;});&lt;br /&gt;&lt;br /&gt;     Map humanThingy = describe(thingy);&lt;br /&gt;&lt;br /&gt;     Map robotThingy = describe(new Thingy(&quot;R&quot;, &quot;Joe&quot;, 10, null));&lt;br /&gt;&lt;br /&gt;     Human human = new Human();&lt;br /&gt;     populate(human, humanThingy);&lt;br /&gt;&lt;br /&gt;     String humanString = ToStringBuilder.reflectionToString(human).toString();&lt;br /&gt;     System.out.print(String.format(&quot;Humans are like this --&gt; %s\n&quot;, humanString));&lt;br /&gt;&lt;br /&gt;     Robot robot = new Robot();&lt;br /&gt;     populate(robot, robotThingy);&lt;br /&gt;&lt;br /&gt;     String robotString = ToStringBuilder.reflectionToString(robot).toString();&lt;br /&gt;     System.out.print(String.format(&quot;Robots are like that --&gt; %s\n&quot;, robotString));&lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Did you see how lazy I was?  I simply used:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;import static org.apache.commons.beanutils.BeanUtils.*;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;to empower my program to extract property name-value pairs from one type and apply them to the other type.  The best part for me is this, no one cares about types in this process because the transform logic, now deputized with the powers of BeanUtils, will happily get all the data in one object and apply it to another.  If.... if you follow naming conventions (type conventions not required though I did use them in this example).&lt;br /&gt;&lt;br /&gt;Now, this relies on BeanUtils not being too fussy about the types of data pushed to and pulled from the Map, but if it ever decided to do something that broke this, it is pretty easy to replace this code with your own.&lt;br /&gt;&lt;br /&gt;So we see by being lazy and conventional (the names) we can do more work with less effort.  At this point I must thank &lt;a href=&quot;http://blog.jbrains.ca/&quot;&gt;JBrains&lt;/a&gt; for making me aware of the power of BeanUtils and I&#39;ll thank some blobby code I had to deal with for inspiring me to think of using BeanUtils this way.</description><link>http://codingkensho.blogspot.com/2010/10/slacker-duck-typing-for-properties-in.html</link><author>noreply@blogger.com (John G. Ferguson)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6590239117348358350.post-4119880798667905234</guid><pubDate>Sat, 02 Oct 2010 16:43:00 +0000</pubDate><atom:updated>2010-10-02T10:13:35.422-07:00</atom:updated><title>Heroku the Great</title><description>&lt;span style=&quot;font-size:130%;&quot;&gt;Deployment Tears Be Gone...&lt;/span&gt;&lt;br /&gt;It is true that using Heroku made my pains go away.  My &quot;I am doing this side project - this ultra lean startup and I don&#39;t have time for this&quot; pains went away.   It is simple.   However, before walking in the door at Heroku you still need to assess your intentions.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;How Are You Being Served?&lt;/span&gt;&lt;br /&gt;This question is more about your use of Heroku as your customers see it.  Ask yourself - how is this application being reached?  Is it a stand alone?  Is it one of those Facebook apps hosted here with data per user?  Is it for a company?  Is it SAS for many companies?  Is it a hybrid?&lt;br /&gt;&lt;br /&gt;My interest lies in a corporation using our application to add some capability to their web site for &lt;span style=&quot;font-weight: bold;&quot;&gt;their own users&lt;/span&gt;.  It is in effect SAS but serving the UI not just SOA services.&lt;br /&gt;&lt;br /&gt;Serving many groupings of end users from one application means doing one of two things per Heroku Support (I asked after exhausting the docs and my patience searching at large):&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;ol&gt;&lt;li&gt;Create a separate deployment for each company - so you in effect are managing, paying for and billing for the cost of multiple applications on Heroku.  There are no tools yet to automate that.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Partition your data by corporation using it.&lt;/li&gt;&lt;/ol&gt;So we landed on using a database key to partition our data by company - in concept - we have to settle on the domain design which in our usage of Rails will be pretty much equal to the schema design.  We ain&#39;t fancy folks and we save our tears and toil for the UI and core application.  As for UI struggles maybe I&#39;ll post some thoughts on this later on.  Oddly, the villain in this isn&#39;t just poor old IE 6...</description><link>http://codingkensho.blogspot.com/2010/10/heroku-great.html</link><author>noreply@blogger.com (John G. Ferguson)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6590239117348358350.post-2997583822711607068</guid><pubDate>Wed, 02 Dec 2009 02:01:00 +0000</pubDate><atom:updated>2009-12-01T19:01:04.509-08:00</atom:updated><title>Art or Craft?</title><description>&lt;a style=&quot;&quot; onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;http://2.bp.blogspot.com/_625fYnLvUUA/SxXK5gpk-vI/AAAAAAAAAUo/WdpAlWIAsRI/s1600/images.jpg&quot;&gt;&lt;img style=&quot;margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 129px; height: 124px;&quot; src=&quot;http://2.bp.blogspot.com/_625fYnLvUUA/SxXK5gpk-vI/AAAAAAAAAUo/WdpAlWIAsRI/s320/images.jpg&quot; alt=&quot;&quot; id=&quot;BLOGGER_PHOTO_ID_5410453616721197810&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;p&gt;These days many of the programming legions are upon us with new criteria we have to meet to play &quot;their&quot; reindeer games.  Let&#39;s say I don&#39;t care, let&#39;s say I care more about the art than the craft.  For that is really true for me.&lt;/p&gt;&lt;p&gt;What&#39;s the difference? Well look at this picture, to the left.  You know what it is right?  It&#39;s a circle right?  But it&#39;s not exactly a circle is it?  It is also not totally finished and it is not wrong.  It is unique and organic.&lt;/p&gt;&lt;p&gt;So, what does it matter? It is a circle for most of us and it is more.  It is personal and is a stamp of identity of its creator.  If you asked them to tell you how to create such a thing you might not understand the answer and you might even think they were being evasive.&lt;br /&gt;&lt;/p&gt;&lt;a onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;http://1.bp.blogspot.com/_625fYnLvUUA/SxXRBw5OT5I/AAAAAAAAAVQ/7Gph95rY4rw/s1600/275px-Inscribed_circles.svg.png&quot;&gt;&lt;img style=&quot;float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 200px; height: 200px;&quot; src=&quot;http://1.bp.blogspot.com/_625fYnLvUUA/SxXRBw5OT5I/AAAAAAAAAVQ/7Gph95rY4rw/s200/275px-Inscribed_circles.svg.png&quot; border=&quot;0&quot; alt=&quot;&quot; id=&quot;BLOGGER_PHOTO_ID_5410460355590508434&quot; /&gt;&lt;/a&gt;Now, on to our next exhibit, this set of perfect circles is constructed using precision - using geometric rules and patterns.  It is one you could follow by using a formula and recipe from a book.  It might help to have a teacher present but you could still do it without any help.   It is pre-destiny.&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;To do it requires you to master some tools for drawing, measurement and so forth.  You need to adjust for changes in the drawing tools and medium.  To me it is craft, the craft of drafting that comes to bear here.  &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I certainly accept one can have and benefit from a teacher in learning such a craft (in fact in high school I took a drafting class and benefitted from having a teacher).  I admit there are masterful tricks and approaches.  However, there is no way I will be convinced that one can realize the first image, the organic one, using the tools of the second example.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Okay then where am I going with this?  Well, as I write this I see something like &quot;islands of order&quot; (craft) within the &quot;ocean of chaos&quot; (art).  When we put together little bits like DTOs, DAOs, Facades and Adapters we are &quot;craftsmen&quot; (like plumbers in fact).  When we go up higher and see the interfaces and user interfaces and the whole heaving ocean of parts that make up our application we are &quot;artists&quot;.  &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Artists, especially ones who paint images like my first example are beyond myriad details.  They see things beyond the well honed arcs and angles.  Craftsmen are down at it, doing each part with polish, pattern and often a good bit of &lt;i&gt;a priori &lt;/i&gt;knowledge of how all the parts will fit.  They have a blueprint.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I have experienced two kinds of coding:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;Emergent - &quot;Artistic&quot; code which comes from somewhere in the back of my head.&lt;/li&gt;&lt;li&gt;Convergent - &quot;Crafted&quot; code based on patterns I already saw and for which I could tell a detailed story.  &lt;/li&gt;&lt;/ul&gt;&lt;div&gt;The emergent kind came from my 10000 ft. flight in my mind over our problem space.  The convergent came from detailed reduction.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;We all have &quot;flavors&quot; or perspectives if you will, and around this center we inscribe our own &quot;circles&quot;.  I prefer the art to the craft.  I prefer the free which is based on high level principles to the more precise and crafted.  I like to experience the snarling tendrils of systems as they grow, wither and curl.  When am I done?  How about now?&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;It is personal and it is a preference,  and even in the face of a movement trying to define the &quot;right&quot; way to be... I will be an artist (of sorts) who writes code.  You can be what you want,  just please don&#39;t be a pain in my a$$ about it.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;</description><link>http://codingkensho.blogspot.com/2009/12/art-or-craft.html</link><author>noreply@blogger.com (John G. Ferguson)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/_625fYnLvUUA/SxXK5gpk-vI/AAAAAAAAAUo/WdpAlWIAsRI/s72-c/images.jpg" height="72" width="72"/><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6590239117348358350.post-3838994734996985900</guid><pubDate>Thu, 13 Aug 2009 23:51:00 +0000</pubDate><atom:updated>2009-08-13T17:41:40.396-07:00</atom:updated><title>No Function Overloading?  Not a Problem.</title><description>Recently, as I got back to Ruby, or should I say got back learning more about Ruby after skirting the language (in depth) and still getting Rails coding done... I decided I needed to kick it up a notch and get Rubier.  I enjoy this task, because I can focus and think.&lt;br /&gt;&lt;br /&gt;My source for this focus is &lt;span style=&quot;font-style: italic;&quot;&gt;&lt;a href=&quot;http://www.manning.com/black2/&quot;&gt;The Well Grounded Rubyist &lt;/a&gt;&lt;/span&gt;and while the book has a lot of &lt;span style=&quot;font-style: italic;&quot;&gt;OO-newbish&lt;/span&gt; stuff I find the succinct portions readable and so I skim and enjoy the book.  Anyway, one of the things that came up while reading it was the explanation of something that trips up most former C++ and Java programmers jumping into Ruby - no overloading (at least not the way that Java and C++ do it)!&lt;br /&gt;&lt;br /&gt;Seems like heresy, for an OO language right?  However, Ruby can realize the same concepts:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Ruby has default values for arguments so if you construct your parameter list correctly, you can realize overloading.&lt;/li&gt;&lt;li&gt;If you need optional arguments as part of the overloading option - Ruby has the optional argument support.&lt;/li&gt;&lt;/ul&gt;So you can get an overloading effect.&lt;br /&gt;&lt;br /&gt;However, I want to point out something I have done even when programming in Java or C++: &lt;span style=&quot;font-weight: bold;&quot;&gt;I have not not used overloading&lt;/span&gt;.  I started realizing the value of this approach working with some Math whiz who was helping me discuss an API design.  For him, overloading was a clever parlor trick that did nothing to tell the person reading your code what you mean by supporting n-ways to call the same logic - without extra documentation.&lt;br /&gt;&lt;br /&gt;His suggestion?  Name the overloaded functions to tell a story:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Overloaded: calc_net_pay(tax_rate,  the_401k_rate) and  calc_net_pay(tax_rate) &lt;/li&gt;&lt;li&gt;Informative: calc_net_pay_for_specific_401K_rate(tax_rate, the_401k_rate) and calc_net_pay_for_default_401K_rate(tax_rate)&lt;/li&gt;&lt;/ul&gt;Okay so it looks a little verbose, and when you read the declarations of the overloads the variables make things clear so why bother?  Well, consider reading the invocations of your function.  Now, with descriptive names you don&#39;t need more documentation, source code hot keys, or  hover over pop-ups to explain what you are reading.  &lt;br /&gt;&lt;br /&gt;If you name methods explicitly and you encounter a function invocation like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;calc_net_pay_for_specific_401K_percent(get_employee_tax_rate(),  get_employee_401_rate())&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;You have a real good idea what the function is doing and also why it needs the data that you give it.  In the overloading case its not just black box implementation - which is a good thing we call &lt;span style=&quot;font-weight: bold;&quot;&gt;encapsulation&lt;/span&gt;; it is also black box semantics, which is not good in any case and requires yet more data to untangle the intentions. &lt;br /&gt;&lt;br /&gt;Encapsulation should protect implementations, not obscure semantics.  In programs, clear intentions are what reduce the occurrence of the Lava Flow Anti-pattern or inadvertent deletion by refactoring to &quot;clean up the junk&quot;.&lt;br /&gt;&lt;br /&gt;I have used overloading but in the end I think it is less OO than some assert, I also think it is a bit of a cheap trick.  Most importantly, I know it can obscure semantics.&lt;br /&gt;&lt;br /&gt;So for me, Ruby &quot;not having overloading&quot; (like Java or C++) is not a problem.   In fact, what I just said about clarity of semantics  also applies to Ruby&#39;s &quot;overloading&quot; by default arguments pattern.  Maybe no &lt;span style=&quot;font-weight: bold;&quot;&gt;public&lt;/span&gt; function should leverage that trick either.  Perhaps it&#39;s just a tool to help keep the internals clean and lean?</description><link>http://codingkensho.blogspot.com/2009/08/no-function-overloading-not-problem.html</link><author>noreply@blogger.com (John G. Ferguson)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6590239117348358350.post-7805688921671878341</guid><pubDate>Fri, 03 Jul 2009 14:02:00 +0000</pubDate><atom:updated>2009-07-03T17:56:10.080-07:00</atom:updated><title>Pen v. Sword and now Pen v. Web</title><description>&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;A Word From Our Sponsor&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Today&#39;s post is more about an emerging pattern and a success in applying it vs. a struggle in applying it that is ongoing.  I will start out by saying I am not naming the other parties in the &quot;struggle&quot; because those negotiations are under way.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;Success&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;What I am about to share with you is a link to a web site which &lt;i&gt;The Art of Agile Development&lt;/i&gt; which is a very useful book, but it is still paper.  It cannot help you model or guide you.  Sure I love books, I have more than I can read, and surveys, profiles, and tests are useful.   They are more useful online.&lt;br /&gt;&lt;br /&gt;My friend &lt;a href=&quot;http://sbastn.com/&quot;&gt;Sebastian Hermida&lt;/a&gt; implemented what I feel is a great tool and the author feels is an awesome viral marketing tool for &lt;span style=&quot;font-style: italic;&quot;&gt;The Art of Agile Development.&lt;/span&gt;&lt;/span&gt; In fact, providing such a tool like his &lt;a href=&quot;http://www.abetterteam.org/&quot;&gt;Better Team Site&lt;/a&gt; free, leverages the principle reciprocity that can benefit sales of the book.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;The Struggle&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The struggle I face is convincing a publisher that the same thing will help their book - viral marketing and reciprocity.  The author has been pleading on my behalf and now it is down to me to leverage my influence skills to get some kind of web site using a survey from the book - a useful survey I might add.&lt;br /&gt;&lt;br /&gt;So wish me luck, I am obviously one of the first people this publisher has encountered asking for such permission. What year is it again?  2009?  Really?</description><link>http://codingkensho.blogspot.com/2009/07/pen-v-sword-and-now-pen-v-web.html</link><author>noreply@blogger.com (John G. Ferguson)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6590239117348358350.post-1174191168352008168</guid><pubDate>Thu, 18 Jun 2009 01:24:00 +0000</pubDate><atom:updated>2009-06-17T21:09:09.735-07:00</atom:updated><title>Rails pre-2.3 and Single Form Object Graph Pains</title><description>&lt;span style=&quot;font-size:130%;&quot;&gt;The Problem&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;Recently, I was working on part of a Rails application for which I found myself needing to use &lt;span style=&quot;font-weight: bold;&quot;&gt;polymorphic&lt;/span&gt; relationships and I wanted to give &lt;span style=&quot;font-weight: bold;&quot;&gt;value objects&lt;/span&gt; a try.&lt;br /&gt;&lt;br /&gt;Since the code is something under development, I will make up a simple case here.  Keep in mind some goals:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;The polymorphic child is meant to reduce a common concept to one table - so I want to have Rails help me track the type of parent using it.&lt;/li&gt;&lt;li&gt;I used value objects to decompose the polymorphic child into usable chunks - at this point I had little interest in breaking the subparts into tiny, annoying tables.  Though by leveraging value objects now, I get to defer that decision all while using a healthy decomposition.&lt;/li&gt;&lt;li&gt;One form - with several subsections.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;As stated in the title, I found out quickly that the recipes for using the beautiful features of Rails for form mapping, and getting all my validations either added &lt;a href=&quot;http://railscasts.com/episodes/75-complex-forms-part-3&quot;&gt;view gunk to my model &lt;/a&gt;or involved using gems supporting &lt;a href=&quot;http://jamesgolick.com/tags/presenter.html&quot;&gt;the powerful Presenter semantic&lt;/a&gt; I was not ready to take on at this time.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;My Approach - Do Next to Nothing&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;The reason for this approach is to do nothing extra to get this function done and do the right thing when I take on Rails 2.3.   Of course, I may decide the Presenter is the way to go.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The above resources gave me an no immediate answer for the Value Objects - these turned out to give me various problems which I began to realize required more view supporting fake model attributes to handle param hashes for the Value Objects.   Another option presented by &lt;a href=&quot;http://www.stephenchu.com/2008/03/introduction-to-rails-composedof.html&quot;&gt;this blog&lt;/a&gt; involved some stubbed out methods to make use of &lt;span style=&quot;font-weight: bold;&quot; class=&quot;constant&quot;&gt;ActiveRecord&lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot; class=&quot;symbol&quot;&gt;:Validations&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt; and that made me feel like I was getting a little off track.&lt;br /&gt;&lt;br /&gt;So I ended up with:&lt;br /&gt;&lt;/span&gt;&lt;ul&gt;&lt;li&gt;A controller managing multiple object reference variables.&lt;/li&gt;&lt;li&gt;Using a transaction block.&lt;/li&gt;&lt;li&gt;Not referring to my Value Objects in my form - but I kept them in the object because I hope to use them later.&lt;/li&gt;&lt;/ul&gt;Here is the parent class, Investor.  It has a polymorphic composition: Contact Info.&lt;br /&gt;&lt;br /&gt;&lt;div style=&quot;border-style: solid; border-width: 1px; padding: 5px; overflow: auto;&quot;&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;class Investor &lt; on =&quot;&quot;&gt; :save, :message =&gt; &quot;can&#39;t be blank&quot;&lt;br /&gt;&lt;br /&gt;validates_associated :contact_info, :on =&gt; :save&lt;br /&gt;&lt;br /&gt;composed_of :name, :mapping =&gt; [%w(contact_full_name, full_name)]&lt;br /&gt;&lt;br /&gt;has_one :contact_info, :as =&gt; :contactable&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Contact Info has a denormalized set of attributes - actually that is not totally true but lots of people do tend to break Phone and Address down into tiny pieces and distinct tables.  I did not do so and at this point you will notice my value object for phone has one attribute in use at this time - I am planning to using the other attributes later.&lt;br /&gt;&lt;br /&gt;&lt;div style=&quot;border-style: solid; border-width: 1px; padding: 5px; overflow: auto;&quot;&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;class ContactInfo &lt; on =&quot;&quot;&gt; :save, :message =&gt; &quot;can&#39;t be blank&quot;&lt;br /&gt;validates_presence_of :mailing_city, :on =&gt; :save, :message =&gt; &quot;can&#39;t be blank&quot;&lt;br /&gt;validates_presence_of :mailing_state, :on =&gt; :save, :message =&gt; &quot;can&#39;t be blank&quot;&lt;br /&gt;&lt;br /&gt;validates_presence_of :primary_number, :on =&gt; :save, :message =&gt; &quot;can&#39;t be blank&quot;&lt;br /&gt;&lt;br /&gt;validates_presence_of :contactable, :on =&gt; :save, :message =&gt; &quot;can&#39;t be unvalued&quot;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;composed_of :mailing_address, :class_name =&gt; &quot;Address&quot;, :mapping =&gt; [ %w(mailing_address1 address1), %w(mailing_city city), %w(mailing_state state), %w(mailing_zip_code zip_code), %w(mailing_address2 address2) ] do |params|&lt;br /&gt;  Address.new params[:mailing_address1], params[:mailing_city], params[:mailing_state], params[:mailing_zip_code], params[:mailing_address2]&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;composed_of :primary_phone, :class_name =&gt; &quot;Phone&quot;, :mapping =&gt; [ %w(primary_number number) ] do |params|&lt;br /&gt;  Phone.new params[:primary_number]&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;belongs_to :contactable, :polymorphic =&gt; true&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style=&quot;border-style: solid; border-width: 1px; padding: 5px; overflow: auto;&quot;&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;class Address&lt;br /&gt;attr_reader :address1, :address2, :city, :state, :zip_code&lt;br /&gt;def initialize(address1, city, state, zip_code, address2 = nil)&lt;br /&gt;   @address1, @address2, @city, @state, @zip_code = address1, address2, city, state, zip_code&lt;br /&gt;end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;class Phone&lt;br /&gt;attr_reader :number, :extension&lt;br /&gt;def initialize(number, extension = nil)&lt;br /&gt;   @number, @extension = number, extension&lt;br /&gt;end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;class Name&lt;br /&gt; attr_reader :full_name&lt;br /&gt; def initialize(full_name)&lt;br /&gt;     @full_name = full_name&lt;br /&gt; end&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;As of now,  I am only supporting create processing and I will show parts of the partial I use to render this data:&lt;br /&gt;&lt;br /&gt;&lt;div style=&quot;border-style: solid; border-width: 1px; padding: 5px; overflow: auto;&quot;&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;% form_for @investor do |f| -%&gt;&lt;br /&gt; &amp;lt;div class=&quot;data_sub_section&quot;&amp;gt;&lt;br /&gt;  &amp;lt;p&amp;gt;&lt;br /&gt;    &lt;%= f.label :contact_full_name, &quot;Full Name: &quot;, :class =&gt; &quot;edit_required_label&quot; %&gt;&lt;br /&gt;    &lt;%= f.text_field :contact_full_name, :class =&gt; &quot;data_field&quot; %&gt;&lt;br /&gt;  &amp;lt;/p&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;..... excerpt ......&gt;&lt;br /&gt;&lt;br /&gt; &lt;% fields_for @contact_info do |f_contact_info| -%&gt;&lt;br /&gt;   &amp;lt;div class=&quot;data_sub_section&quot;&amp;gt;&lt;br /&gt;    &amp;lt;p&amp;gt;&lt;br /&gt;   &lt;%= f_contact_info.label :mailing_address1, &quot;Address1: &quot;, :class =&gt; &quot;edit_required_label&quot; %&gt;&lt;br /&gt;   &lt;%= f_contact_info.text_field :mailing_address1, :class =&gt; &quot;data_field&quot; %&gt;&lt;br /&gt;    &amp;lt;/p&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;..... excerpt ......&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Here we show both an attribute from the parent (Investor) object and the child (ContactInfo) object.  We get around the whole pain with forms from using Value Objects by mapping to the attributes for the Investor and ContactInfo objects and not the Value Objects.    No I don&#39;t love it, but I prefer it to the alternatives.  Less code for me to write and looking to Rails 2.3 to fix this.&lt;br /&gt;&lt;br /&gt;Okay, so you see &lt;span style=&quot;font-weight: bold;&quot;&gt;@investor&lt;/span&gt; and &lt;span style=&quot;font-weight: bold;&quot;&gt;@contact_info&lt;/span&gt; right?  I maintain them as member variables of the investor controller.  Here is the create method I use and save transaction.  I took this approach to avoid saving the parent if there were errors in the child because the form treated the data as one entity - like an updateable view in a database.&lt;br /&gt;&lt;br /&gt;&lt;div style=&quot;border-style: solid; border-width: 1px; padding: 5px; overflow: auto;&quot;&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;def create&lt;br /&gt;    @investor = Investor.new(params[:investor])&lt;br /&gt;    begin&lt;br /&gt;        Investor.transaction do&lt;br /&gt;            @contact_info = ContactInfo.new(params[:contact_info])&lt;br /&gt;            @contact_info.contactable = @investor&lt;br /&gt;            @investor.contact_info = @contact_info&lt;br /&gt;            @investor.save!&lt;br /&gt;        end&lt;br /&gt;        all_valid = true&lt;br /&gt;    rescue ActiveRecord::RecordNotSaved, ActiveRecord::RecordInvalid&lt;br /&gt;        all_valid = false&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    respond_to do |format|&lt;br /&gt;        if all_valid&lt;br /&gt;            flash[:notice] = &#39;Investor was successfully created.&#39;&lt;br /&gt;            format.js&lt;br /&gt;            format.xml  { render :xml =&gt; @investor, :status =&gt; :created, :location =&gt; @investor }&lt;br /&gt;        else&lt;br /&gt;            flash[:notice] = &#39;Investor was not created.&#39;&lt;br /&gt;            format.js&lt;br /&gt;            format.xml  { render :xml =&gt; @investor.errors, :status =&gt; :unprocessable_entity }&lt;br /&gt;        end&lt;br /&gt;    end&lt;br /&gt;end&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;I admit I still need to go back and tweak the format.xml section for @contact_info errors, my immediate need for forms to work is met.  I am not overjoyed at this code, it works and I used Rails out of the box to do it.&lt;br /&gt;&lt;br /&gt;I still think maybe Presenter is a better bet, but that is a refactoring adventure for another time.</description><link>http://codingkensho.blogspot.com/2009/06/rails-pre-23-and-single-form-object.html</link><author>noreply@blogger.com (John G. Ferguson)</author><thr:total>0</thr:total></item></channel></rss>