[Nitro] [PATCH] Entity copying methods

Rob Pitt rob at motionpath.com
Fri Jan 27 09:36:06 EST 2006


I noticed a lot of people asked for this in the past and today I too
needed it so I made these set of patches (which probably need
refactoring but are working).

Adds a .og_clone method to every entity which passes any arguments you
supply onto the .new constructor when making the cloned object.

The cloned object contains copies of all properties and all relations
except HasMany which is impossible to copy as it involves modifying
other objects BelongsTo relation, breaking the original relationship.

BelongsTo/RefersTo etc (the inferior half of the relationship) will all
copy ok, as will joins.

Added the following methods onto Og::Entity to support the clone
function (could be called directly too if you needed more selective
usage):

def copy_properties(source,destination,ignore = [],use_setter_method =
false)

Takes a source object (model instance/record), a destination, an array
of property symbols to ignore. Copies properties from source to
destination.  Use_setter_method can be either false (in which case the
copy is done by setting destinations class variables) or true (in which
case it's done by using property= setter methods).

def copy_inferior_relations(source,destination,ignore = [])

As above but copies refers_to, belongs_to and has_one relationships
instead of properties.

def copy_equal_relations(source,destination,ignore = [])

As above but copies joins_many and many_to_many relationships.

def copy_relations(source,destination,ignore = [])

Calls the previous two methods.

def clone(source,*args)

Makes a new object by calling source.class.new(*args) and calling the
copy_properties and copy_relations on it. Returns the new object (saved,
because you must save for join table copying to work).

None of the other methods save.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: entity_copying_methods.patch.bz2
Type: application/x-bzip
Size: 5598 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/nitro-general/attachments/20060127/9e6827b2/attachment.bin 


More information about the Nitro-general mailing list