recloner 0.0.0 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/recloner.rb +1 -0
- data/lib/recloner/recloner.rb +13 -1
- data/readme.markdown +25 -3
- metadata +4 -4
data/lib/recloner.rb
CHANGED
data/lib/recloner/recloner.rb
CHANGED
@@ -1,7 +1,19 @@
|
|
1
1
|
module Recloner
|
2
2
|
def clone(&block)
|
3
3
|
block ||= Proc.new {}
|
4
|
-
|
4
|
+
property_names = properties.map(&:name) - protected_properties.map(&:name)
|
5
|
+
attrs = property_names.inject({}){|hash, x|
|
6
|
+
val = send(x)
|
7
|
+
val = val.to_a if val.class == CouchRest::Model::CastedArray
|
8
|
+
hash[x] = val
|
9
|
+
hash
|
10
|
+
}
|
11
|
+
self.class.new(attrs).tap(&block)
|
12
|
+
end
|
13
|
+
|
14
|
+
def clone!(&block)
|
15
|
+
block ||= Proc.new {}
|
16
|
+
next_id = database.server.next_uuid
|
5
17
|
copy next_id
|
6
18
|
self.class.get(next_id).tap(&block).tap {|d| d.save}
|
7
19
|
end
|
data/readme.markdown
CHANGED
@@ -8,6 +8,10 @@ Exposes a "Recloner" module containing a "clone" method that you can mixin to yo
|
|
8
8
|
|
9
9
|
#Usage
|
10
10
|
|
11
|
+
There are two types of clones: soft clones (the `clone` method) and persisted clones (the `clone!` method).
|
12
|
+
|
13
|
+
## Soft clones
|
14
|
+
|
11
15
|
require 'recloner'
|
12
16
|
class MyDocument < CouchRest::Model::Base
|
13
17
|
include Recloner
|
@@ -18,9 +22,27 @@ Exposes a "Recloner" module containing a "clone" method that you can mixin to yo
|
|
18
22
|
p m.id #==> "78439279432743279"
|
19
23
|
p m.name #==> "m"
|
20
24
|
m1 = m.clone
|
25
|
+
|
26
|
+
m1.new_record? #==> true
|
27
|
+
m1.id #==> nil
|
28
|
+
|
29
|
+
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.
|
30
|
+
|
31
|
+
## Persisted clones
|
32
|
+
|
33
|
+
m = MyDocument.create :name => "m"
|
34
|
+
p m.id #==> "78439279432743279"
|
35
|
+
p m.name #==> "m"
|
36
|
+
m1 = m.clone!
|
21
37
|
p m1.id #==> "45353762637126371"
|
22
38
|
p m.name #==> "m"
|
23
|
-
m2 = m.clone {|doc| doc.name = "m2"}
|
24
|
-
p m2.id #==> "1234566789090"
|
25
|
-
p m2.name #==> "m2"
|
26
39
|
|
40
|
+
|
41
|
+
## Modifying your clone
|
42
|
+
|
43
|
+
You can pass modifications to the clone (either a soft clone or a persisted clone) in the following manner:
|
44
|
+
|
45
|
+
m = MyDocument.create :name => "m"
|
46
|
+
m1 = m.clone do |doc|
|
47
|
+
doc.name += " clone"
|
48
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: recloner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.0
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Matt Parker
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-09-29 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|