Project

recloner

0.0
No commit activity in last 3 years
No release in over 3 years
This gem exposes a 'Recloner' module containing a 'clone' method, allowing you to easily clone CouchRest::Model::Base docs.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

#Introduction

Exposes a "Recloner" module containing a "clone" method that you can mixin to your CouchRest::Model::Base derived objects.

#Installation

$ gem install recloner

#Usage

There are two types of clones: soft clones (the clone method) and persisted clones (the clone! method).

Soft clones

require 'recloner'
class MyDocument < CouchRest::Model::Base
  include Recloner
  property :name
end

m = MyDocument.create :name => "m"
p m.id #==> "78439279432743279"
p m.name #==> "m"
m1 = m.clone

m1.new_record? #==> true
m1.id #==> nil

As you can see, a soft clone simply creates a copy in-memory - it's up to you to persist it (save) to the database.

Persisted clones

m = MyDocument.create :name => "m"
p m.id #==> "78439279432743279"
p m.name #==> "m"
m1 = m.clone!
p m1.id #==> "45353762637126371"
p m.name #==> "m"

Modifying your clone

You can pass modifications to the clone (either a soft clone or a persisted clone) in the following manner:

m = MyDocument.create :name => "m"
m1 = m.clone do |doc|
  doc.name += " clone"
end