Navigation Menu

Skip to content
This repository has been archived by the owner on Jul 27, 2021. It is now read-only.

innoq/dm-deep_cloning

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Datamapper deep cloning extention

This DataMapper extension adds deep cloning functionality to your resource objects. You can use it to recursively clone objects following spefic relations which need to be specified explicitly.

Usage

Given the following models:

class Blog
  has n, :posts
end

class Post
  belongs_to :blog
end   

Cloning a blog including all its posts would look like:

new_blog = blog.deep_clone(:posts)

This would initialize a new Blog object with all attrbutes taken from the original blog. new_blog.posts would consist of (unsaved) clones of the original entrys from blog.posts.

If you don't want just to initialize the clones but to create them to, use :create as first parameter:

new_blog = blog.deep_clone(:create, :posts)

Nested recursion

Given a further model Comment:

class Comment
  belongs_to :post
end

Post.has n, :comments

You can also specify nested parameters like:

blog.deep_clone(:posts => :comments)

It's also possible to spcify Arrays of relation names.