This project is archived and is in readonly mode.
composed_of constructor and converter options
-
Michael Koziarski
I don't quite follow what :converter is compared to :constructor
-
Rob Anderton
My IPAddress example is probably to blame. The differences are:
- :constructor is called when instantiating the aggregate object and is therefore passed all of the mapped attributes in the order they are defined in the :mapping option
- :converter is called when a value is assigned to the aggregate attribute and therefore passed the single value that is used in the assignment
Here's an example that hopefully makes this difference more obvious - it uses the NetAddr gem and is slightly convoluted for emphasis :)
class NetworkResource < ActiveRecord::Base composed_of :cidr, :class_name => 'NetAddr::CIDR', :mapping => [ %w(network_address network), %w(cidr_range bits) ], :allow_nil => true, :constructor => Proc.new { |network_address, cidr_range| NetAddr::CIDR.create("#{network_address}/#{cidr_range}") }, :converter => Proc.new { |value| NetAddr::CIDR.create(value.is_a?(Array) ? value.join('/') : value) } end # Calls the :constructor proc n = NetworkResource.new(:network_address => '192.168.0.1', :cidr_range => 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 -
Rob Anderton
Given that this patch fixes what is really a bug in ActiveRecord, doesn’t break existing code and is now the third such patch to address the issue of constructors in composed_of (the other two of course not being applied), is there a good reason not to apply it now?
I appreciate the efforts of Eloy to provide a complete replacement for composed_of and, while I don’t necessarily agree with the syntax, I think this is something that should be investigated further using plugins for now with a view to incorporating the ‘best’ approach into Rails at some point in the future.
But in the interim can the two or three of us who want to use composed_of with classes that don’t conform to its narrow view of how objects should be constructed, actually be allowed to?
-
Michael Koziarski
- Assigned user set to Michael Koziarski
- Milestone cleared.
OK, I think I agree, it's probably a good idea to apply this.
Could you flesh out the documentation with a few more examples though.
-
Michael Koziarski
Basically if you turn that comment above into rdoc, I think we're good
-
Rob Anderton
That's great news, thanks!
I've attached an updated patch which includes the original changes plus the example from above and some additional explanations for how things work that hopefully make things a bit clearer.
-
Repository
- State changed from new to committed
(from [b518b6c0d3e7796e303c2396de97a8d901aeb308]) Expanded documentation for new composed_of options
Signed-off-by: Michael Koziarski michael@koziarski.com [#892 composed_of constructor and converter options state:committed] http://github.com/rails/rails/co...
