mongoid-tll 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/LICENSE +22 -0
  2. data/README.md +37 -0
  3. data/lib/mongoid/tll.rb +68 -0
  4. data/test/mongoid-tll.rb +83 -0
  5. metadata +81 -0
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2011 Joris van Rooij
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ Mongoid Top Linked List
2
+ =======================
3
+
4
+ A relatively simple module that provides a doubly top linked list for your
5
+ documents. Every change creates a new document, with a link to the old one.
6
+ All the documents have a link to the newest document. This setup should
7
+ provide better performance in applications that usually need the newest
8
+ version.
9
+
10
+ An example:
11
+
12
+ class MyDocument
13
+ include Mongoid::Document
14
+ include Mongoid::TLL
15
+
16
+ field :data
17
+ end
18
+
19
+ @doc = MyDocument.create(data: "first version")
20
+ @doc.data = "second version"
21
+ @doc.save
22
+
23
+ @doc.data
24
+ >> "second version"
25
+ @doc.prev.data
26
+ >> "first version"
27
+
28
+ A default scope is added to only find the newest versions by default. Use
29
+ the `scopeless` method to circumvent this.
30
+
31
+ Other helper methods have been added:
32
+
33
+ * `newest` - Returns the newest version of the document.
34
+ * `newest?` - Is this the newest version?
35
+ * `oldest?` - Is this the oldest (original) version?
36
+ * `prev` - Returns the previous version of the document.
37
+
@@ -0,0 +1,68 @@
1
+ module Mongoid
2
+
3
+ # Include this module to have documents automatically cloned when
4
+ # updated. It constructs a top linked list (like a doubly linked list
5
+ # with a pointer to the newest version) out of the chain of documents.
6
+ # Only use this when in 99% of the time only the newest version of a
7
+ # document is read! Mongoid::Versioning will take care of all the
8
+ # other usecases.
9
+ module TLL
10
+ def self.included(klass)
11
+ klass.class_exec do
12
+
13
+ # The linked list pointers.
14
+ field :tll_top
15
+ field :tll_prev
16
+
17
+ # By default, get the newest only.
18
+ default_scope where: {tll_top: nil}
19
+
20
+ set_callback :save, :before, :tll_commit
21
+ attr_protected :tll_top, :tll_prev
22
+
23
+ # Function that is called before saving a document.
24
+ # It's responsible for the versioning.
25
+ def tll_commit
26
+ oldself = self.class.unscoped.where(_id: id).first
27
+ if oldself # We've got a previous saved document.
28
+ self.class.unscoped.where(tll_top: oldself._id)\
29
+ .update_all(tll_top: id)
30
+ oldself = oldself.clone
31
+ oldself.tll_top = id
32
+ self.tll_prev = oldself._id
33
+ oldself.save
34
+ end
35
+ end
36
+
37
+ # Is this the newest version?
38
+ def newest?
39
+ self.tll_top.nil?
40
+ end
41
+
42
+ # Return the newest version.
43
+ def newest
44
+ unless self.tll_top.nil?
45
+ return self.class.unscoped.where(_id: self.tll_top).first
46
+ end
47
+
48
+ self
49
+ end
50
+
51
+ # Is this the oldest version?
52
+ def oldest?
53
+ self.tll_prev.nil?
54
+ end
55
+
56
+ # Return the previous version, if there is any.
57
+ # Will return nil when #oldest? is true.
58
+ def prev
59
+ unless self.tll_prev.nil?
60
+ return self.class.unscoped.where(_id: self.tll_prev).first
61
+ end
62
+
63
+ nil
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/env ruby1.9.1
2
+
3
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
4
+
5
+ require 'mongoid'
6
+ require 'mongoid-tll'
7
+ require 'test/unit'
8
+
9
+ Mongoid.configure do |config|
10
+ config.master = Mongo::Connection.new.db "testing"
11
+ config.persist_in_safe_mode = false
12
+ end
13
+
14
+ class MyDocument
15
+ include Mongoid::Document
16
+ include Mongoid::TLL
17
+
18
+ field :content
19
+ end
20
+
21
+ class TestMongoidTLL < Test::Unit::TestCase
22
+ def setup
23
+ @doc = MyDocument.create(content: "first version")
24
+ end
25
+
26
+ def teardown
27
+ MyDocument.delete_all
28
+ end
29
+
30
+ def test_basic_attributes
31
+ assert_kind_of Mongoid::TLL, @doc
32
+ assert_respond_to @doc, :content
33
+ assert_equal "first version", @doc.content
34
+ end
35
+
36
+ def test_extended_attributes
37
+ assert_respond_to @doc, :newest?
38
+ assert_respond_to @doc, :newest
39
+ assert_respond_to @doc, :oldest?
40
+ assert_respond_to @doc, :prev
41
+ end
42
+
43
+ def test_add_version
44
+ @doc.content = "second version"
45
+ @doc.save
46
+ assert @doc.newest?
47
+ assert !@doc.oldest?
48
+ assert_kind_of Mongoid::TLL, @doc.prev
49
+ assert_equal "first version", @doc.prev.content
50
+ assert @doc.prev.oldest?
51
+ assert !@doc.prev.newest?
52
+ end
53
+
54
+ def test_add_version_twice
55
+ @doc.content = "second version"
56
+ @doc.save
57
+ @doc.content = "third version"
58
+ @doc.save
59
+
60
+ assert @doc.newest?
61
+ assert_equal "second version", @doc.prev.content
62
+ assert_equal "first version", @doc.prev.prev.content
63
+ assert_equal "third version", @doc.prev.newest.content
64
+ assert_equal "third version", @doc.prev.prev.newest.content
65
+ assert_equal nil, @doc.prev.prev.prev
66
+ end
67
+
68
+ def test_query_only_newest
69
+ @doc.content = "first document, second version"
70
+ @doc.save
71
+ @doc2 = MyDocument.create(content: "second document, first version")
72
+ @doc2.content = "second document, second version"
73
+ @doc2.save
74
+
75
+ assert_equal 2, MyDocument.count
76
+ assert_equal 4, MyDocument.unscoped.count
77
+
78
+ t = ["first document, second version", "second document, second version"]
79
+ MyDocument.all.each_with_index do |d, i|
80
+ assert_equal t[i], d.content
81
+ end
82
+ end
83
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-tll
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Joris van Rooij
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-04-09 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: mongoid
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 2
30
+ - 0
31
+ - 0
32
+ version: 2.0.0
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: Creates a (doubly) top linked list out of your documents. Every change makes a new revision. Basically a read optimized versioning system.
36
+ email: jorrizza@jrrzz.net
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - lib/mongoid/tll.rb
45
+ - LICENSE
46
+ - README.md
47
+ - test/mongoid-tll.rb
48
+ has_rdoc: true
49
+ homepage: https://github.com/jorrizza/mongoid-tll
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.3.7
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Top linked list plugin for Mongoid
80
+ test_files:
81
+ - test/mongoid-tll.rb