Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Add alternate constructor support to aggregations

#212

The composed_of macro assumes that the value object can be instantiated with new. But for some aggregate classes, new is not the appropriate constructor and instead something like parse is required.

This patch adds support for a :constructor option to the composed_of method. It accepts a symbol identifying a constructor method on the aggregate class. All aggregate object are then instantiated using this constructor method.

A typical example of this new functionality:

require 'uuid'

class Widget < ActiveRecord::Base
  composed_of :uuid, :constructor => :parse
end

Of course, the default value for the constructor is :new.

[This is identical in function to the enhancement documented on Trac ticket 11122. It has been updated to work on Edge and includes less brittle tests and a git-compatible patch.]

Reported by Chris Hapgood · May 16th, 2008 @ 10:15 PM

State: wontfix
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Pratik
    Pratik
    • State changed from new to wontfix
    • Tag set to activerecord, edge, enhancement, patch, tests

    You can just call parse from #initialize itself. Not convinced about the provided use case for this.

    Thanks.

    July 1st, 2008 @ 11:44 PM

  2. Chris Hapgood
    Chris Hapgood

    Agreed, you can call parse from MyARModel#initialize. But that seems like a reason for scrapping aggregations entirely because you could also instantiate the composed_of objects in #initialize as well.

    I would be happy to see aggregations disappear entirely -they seem like a very thin veneer on top of existing Ruby/AR constructors. But if they are worth keeping, why not make them useful for the large population of classes that use constructors like parse and from_x? The patch I propose is pretty trivial...

    July 2nd, 2008 @ 03:24 PM

  3. Pratik
    Pratik

    If we have enough people asking for this, we can surely roll it in.

    July 2nd, 2008 @ 03:41 PM