dm-is-cloneable

I just pushed dm-is-cloneable.

A DataMapper plugin that adds the ability to clone any model. As this alone wouldn’t really be a big deal, it is also possible to specifiy a 1:n relation with so called clone_specs. In these, the attributes_to_clone are persisted, thus leaving clients with the choice of selecting predefined sets of attributes to clone. Currently, associated objects don’t get cloned, although this might change if I need this in the future.

Client code looks something like this:


require 'rubygems'

gem 'dm-core',         '=0.9.11'
gem 'dm-validations',  '=0.9.11'
gem 'dm-is-remixable', '=0.9.11'
gem 'dm-is-cloneable', '=0.0.1'

require 'dm-core'
require 'dm-validations'
require 'dm-is-remixable'
require 'dm-is-cloneable'

DataMapper::Logger.new(STDOUT, :debug)

DataMapper.setup(:default, 'sqlite3:memory:')

class Item

  include DataMapper::Resource

  property :id,             Serial
  property :master_item_id, Integer # add this property if backlinks are desired

  property :name,           String
  property :description,    String

  is :cloneable

  # adds the following api to this class
  #
  # # attributes_to_clone can be one of
  # # <:all|String|Array[String|Symbol]|ItemCloneSpec>
  # Item#clone_resource(nr_of_clones, attributes_to_clone = :all)
  # Item#master_resource
  # Item#cloned_resources
  # Item#has_backlinks_to_master?
  # Item#has_clone_specs?

end


Leave a Reply

Formatting: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>