mongoid-genesis 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -28,7 +28,7 @@ Usage
28
28
  Mongoid Genesis is compatible with any mongoid collection or embedded object.
29
29
 
30
30
 
31
- **Model integration**
31
+ #### Model integration
32
32
 
33
33
  ```ruby
34
34
  class Book
@@ -39,47 +39,58 @@ end
39
39
 
40
40
  This will create an embedded object that will store the original data.
41
41
 
42
- **Basic structure**
42
+ #### Basic structure
43
43
 
44
44
  ```ruby
45
- book = Book.new(:title => 'Art of war', :author => 'Sun Tzu')
46
- #=> #<Book _id: 1, title: "Art of war", author: "Sun Tzu">
45
+ book = Book.new(:title => 'The Art of War', :author => 'Sun Tzu')
46
+ #=> #<Book _id: 1, title: "The Art of War", author: "Sun Tzu">
47
47
 
48
48
  book.genesis
49
49
  #=> #<BookGenesis _id: 1>
50
50
  ```
51
51
 
52
- **Preserve the original attribute**
52
+ #### Preserve the original attribute
53
53
 
54
54
  ```ruby
55
55
  book.write_and_preserve_attribute(:author, 'Sun Zi')
56
- #=> #<Book _id: 1, title: "Art of war", author: "Sun Zi">
56
+ #=> #<Book _id: 1, title: "The Art of War", author: "Sun Zi">
57
57
 
58
58
  book.genesis
59
59
  #=> #<BookGenesis _id: 1, author: "Sun Tzu">
60
60
  ```
61
61
 
62
- **After preserving the original attribute, it will not be overwritten**
62
+ #### After preserving the original attribute, it will not be overwritten
63
63
 
64
64
  ```ruby
65
65
  book.write_and_preserve_attribute(:author, 'Sun Wu')
66
- #=> #<Book _id: 1, title: "Art of war", author: "Sun Wu">
66
+ #=> #<Book _id: 1, title: "The Art of War", author: "Sun Wu">
67
67
 
68
68
  book.genesis
69
69
  #=> #<BookGenesis _id: 1, author: "Sun Tzu">
70
70
  ```
71
71
 
72
- **You can restore the original attribute**
72
+ #### At all time, you can read the original attribute
73
+
74
+ ```ruby
75
+ book.read_attribute_genesis(:title)
76
+ #=> "The Art of War"
77
+
78
+ book.write_and_preserve_attribute(:title, 'The Art of Peace')
79
+ book.read_attribute_genesis(:title)
80
+ #=> "The Art of War"
81
+ ```
82
+
83
+ #### You can restore the original attribute
73
84
 
74
85
  ```ruby
75
86
  book.restore_genesis(:author)
76
- #=> #<Book _id: 1, title: "Art of war", author: "Sun Tzu">
87
+ #=> #<Book _id: 1, title: "The Art of War", author: "Sun Tzu">
77
88
 
78
89
  book.genesis
79
90
  #=> #<BookGenesis _id: 1, author: nil>
80
91
  ```
81
92
 
82
- **To update the original document without losing the current state**
93
+ #### To update the original document without losing the current state
83
94
 
84
95
  ```ruby
85
96
  book.write_and_preserve_attribute(:title, 'The Art of Peace')
@@ -95,6 +106,25 @@ book.reverse_genesis
95
106
  #=> #<BookGenesis _id: 1, title: "The Art of War : Revisited">
96
107
  ```
97
108
 
109
+ Cheat Sheet
110
+ ---------
111
+
112
+ #### read_attribute_genesis(field_name)
113
+ Read the original attribute of the record. If the attribute wasn't overwritten, it will return the same thing as .read_attribute.
114
+
115
+ #### restore_genesis(field_name)
116
+ Restore the original value for the given field
117
+
118
+
119
+ #### reverse_genesis
120
+ Restore the record to its original state
121
+
122
+
123
+ #### write_and_preserve_attribute(field_name, value)
124
+ Overwrite the attribute with the *value* and saves the original value in the genesis object.
125
+
126
+
127
+
98
128
  Copyright
99
129
  ---------
100
130
 
@@ -17,8 +17,14 @@ module Mongoid
17
17
  self.genesis = "#{self.class.name}Genesis".constantize.new
18
18
  end
19
19
 
20
+ def read_attribute_genesis(field_name)
21
+ source = (self.genesis and self.genesis.field_preserved?(field_name)) ? self.genesis : self
22
+
23
+ return source.read_attribute field_name
24
+ end
25
+
20
26
  def restore_genesis(field_name)
21
- self.genesis.restore(field_name)
27
+ self.genesis.restore field_name
22
28
  end
23
29
 
24
30
  def reverse_genesis
@@ -17,34 +17,42 @@ describe Mongoid::Genesis do
17
17
  end
18
18
  end
19
19
 
20
- describe "#write_and_preserve_attribute" do
20
+ describe "#read_attribute_genesis" do
21
+ context "when reading the genesis title" do
22
+ subject { book.read_attribute_genesis(:title) }
23
+
24
+ it { should eql 'The Art of War' }
25
+ end
26
+
27
+ context "when changing the author to 'Sun Wu'" do
28
+ before { book.write_and_preserve_attribute :author, 'Sun Wu' }
29
+
30
+ context "when reading the genesis title" do
31
+ subject { book.read_attribute_genesis(:title) }
32
+
33
+ it { should eql 'The Art of War' }
34
+ end
35
+ end
36
+
21
37
  context "when changing title to 'The Art of Peace'" do
22
38
  before { book.write_and_preserve_attribute :title, 'The Art of Peace' }
23
39
 
24
- its(:title) { should eql 'The Art of Peace' }
25
- its('genesis.title') { should eql 'The Art of War' }
40
+ context "when reading the genesis title" do
41
+ subject { book.read_attribute_genesis(:title) }
26
42
 
27
- context "and changing title to 'The Art of Neutrality'" do
28
- before { book.write_and_preserve_attribute :title, 'The Art of Neutrality' }
43
+ it { should eql 'The Art of War' }
44
+ end
29
45
 
30
- its(:title) { should eql 'The Art of Neutrality' }
31
- its('genesis.title') { should eql 'The Art of War' }
46
+ context "and restoring the title" do
47
+ before { book.restore_genesis :title }
32
48
 
33
- context "and rechanging it to 'The Art of War'" do
34
- before { book.write_and_preserve_attribute :title, 'The Art of War' }
49
+ context "when reading the genesis title" do
50
+ subject { book.read_attribute_genesis(:title) }
35
51
 
36
- its(:title) { should eql 'The Art of War' }
37
- its('genesis.title') { should be_nil }
52
+ it { should eql 'The Art of War' }
38
53
  end
39
54
  end
40
55
  end
41
-
42
- context "when changing title to nil" do
43
- before { book.write_and_preserve_attribute :title, nil }
44
-
45
- its(:title) { should be_nil }
46
- its('genesis.title') { should eql 'The Art of War' }
47
- end
48
56
  end
49
57
 
50
58
  describe "#restore_genesis" do
@@ -83,7 +91,36 @@ describe Mongoid::Genesis do
83
91
  end
84
92
  end
85
93
  end
94
+ end
86
95
 
96
+ describe "#write_and_preserve_attribute" do
97
+ context "when changing title to 'The Art of Peace'" do
98
+ before { book.write_and_preserve_attribute :title, 'The Art of Peace' }
99
+
100
+ its(:title) { should eql 'The Art of Peace' }
101
+ its('genesis.title') { should eql 'The Art of War' }
102
+
103
+ context "and changing title to 'The Art of Neutrality'" do
104
+ before { book.write_and_preserve_attribute :title, 'The Art of Neutrality' }
105
+
106
+ its(:title) { should eql 'The Art of Neutrality' }
107
+ its('genesis.title') { should eql 'The Art of War' }
108
+
109
+ context "and rechanging it to 'The Art of War'" do
110
+ before { book.write_and_preserve_attribute :title, 'The Art of War' }
111
+
112
+ its(:title) { should eql 'The Art of War' }
113
+ its('genesis.title') { should be_nil }
114
+ end
115
+ end
116
+ end
117
+
118
+ context "when changing title to nil" do
119
+ before { book.write_and_preserve_attribute :title, nil }
120
+
121
+ its(:title) { should be_nil }
122
+ its('genesis.title') { should eql 'The Art of War' }
123
+ end
87
124
  end
88
125
  end
89
126
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-genesis
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sebastien Rosa