mongoid_tree 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +116 -0
- data/VERSION +1 -1
- data/mongoid_tree.gemspec +3 -3
- metadata +4 -4
- data/README.rdoc +0 -25
data/README.textile
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
h1. mongoid_tree
|
2
|
+
|
3
|
+
For our application we need a proper tree structure with Depth-First and Breadth-First searches, parent and child information. Also subtrees need to be exported to JSON. This gem will receive long-term support since we use it in our commercial long-term application.
|
4
|
+
|
5
|
+
Initially I thought of an embedded solution, but this will only be possible once MongoDB supports embedded collections, and even deep-embedded collections.
|
6
|
+
|
7
|
+
However this tree is right now on the top of our priority list, means we will put effort into this and release everything in this public gem, as soon as we implement and test it. It will be fully tested with Cucumber and RSpec.
|
8
|
+
|
9
|
+
h2. Installation
|
10
|
+
|
11
|
+
Install as Gem
|
12
|
+
<code>
|
13
|
+
gem install mongoid_tree
|
14
|
+
</code>
|
15
|
+
|
16
|
+
via Gemfile
|
17
|
+
<code>
|
18
|
+
gem 'mongoid_tree', '0.3.1'
|
19
|
+
</code>
|
20
|
+
|
21
|
+
h2. Usage
|
22
|
+
|
23
|
+
mongoid_tree can be included as a module
|
24
|
+
|
25
|
+
<pre><code>
|
26
|
+
class Category
|
27
|
+
include Mongoid::Document
|
28
|
+
include Mongoid::Acts::Tree
|
29
|
+
|
30
|
+
field :name
|
31
|
+
validates_presence_of :name
|
32
|
+
end
|
33
|
+
</code></pre>
|
34
|
+
|
35
|
+
The following methods and fields are provided:
|
36
|
+
|
37
|
+
h3. Adding Children
|
38
|
+
|
39
|
+
I use the _ :references_many, :stored_as => :array_ association Mongoid provides. Association names are fixed to .children and .parent at the moment, but I might make this optional later on.
|
40
|
+
|
41
|
+
<pre><code>
|
42
|
+
# Appending a child node
|
43
|
+
root_node.children << Category.new(:name => "node")
|
44
|
+
|
45
|
+
# Inserting a child node before another node
|
46
|
+
node_2.insert_before(Category.new(:name => "node_1"))
|
47
|
+
|
48
|
+
# Inserting a child node after another node
|
49
|
+
node_2.insert_before(Category.new(:name => "node_3"))
|
50
|
+
</code></pre>
|
51
|
+
|
52
|
+
h2. Accessing the Parent Object
|
53
|
+
|
54
|
+
<pre><code>
|
55
|
+
node.parent # --> root_node
|
56
|
+
</code></pre>
|
57
|
+
|
58
|
+
h2. Deleting Subtrees
|
59
|
+
|
60
|
+
Just use the usual Association Methods provided by Mongoid. The entire subtree will also be deleted
|
61
|
+
<pre><code>
|
62
|
+
node.delete
|
63
|
+
#or
|
64
|
+
node.destroy
|
65
|
+
</code></pre>
|
66
|
+
|
67
|
+
h2. Moving Subtrees
|
68
|
+
|
69
|
+
You unhinge an entire subtree from it's parent and move it to a new parent. The subtrees path information will automatically be rebuild.
|
70
|
+
<pre><code>
|
71
|
+
node4.move_to(node_1)
|
72
|
+
</code></pre>
|
73
|
+
|
74
|
+
h2. Depth First
|
75
|
+
|
76
|
+
<p><img alt="File:Depth-first-tree.svg" src="http://upload.wikimedia.org/wikipedia/commons/thumb/1/1f/Depth-first-tree.svg/390px-Depth-first-tree.svg.png" width="390" height="250" /></p>
|
77
|
+
This is probably your standard call if you want to loop to your tree. E.g. when building a menu. Calling Depth First will return the entire subtree *including* the node you called it on.
|
78
|
+
<pre><code>
|
79
|
+
root_node.depth_first
|
80
|
+
#or
|
81
|
+
root_node.dfs
|
82
|
+
|
83
|
+
#returns
|
84
|
+
[ root_node, child_1, child_1.1, child_2, child_2.1, child_2.2 ]
|
85
|
+
</code></pre>
|
86
|
+
|
87
|
+
h2. Breadth First
|
88
|
+
|
89
|
+
<p><img alt="File:Breadth-first-tree.svg" src="http://upload.wikimedia.org/wikipedia/commons/thumb/3/33/Breadth-first-tree.svg/390px-Breadth-first-tree.svg.png" width="390" height="250" /></p>
|
90
|
+
This is a hardly used option, but hey, why not? Calling Breadth First will return the entire subtree *including* the node you called it on.
|
91
|
+
<pre><code>
|
92
|
+
root_node.breadth_first
|
93
|
+
#or
|
94
|
+
root_node.bfs
|
95
|
+
|
96
|
+
#returns
|
97
|
+
[ root_node, child_1, child_2, child_1.1, child_2.1, child_2.2 ]
|
98
|
+
</code></pre>
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
h2. Jeweler Standard Text
|
104
|
+
|
105
|
+
h3. Note on Patches/Pull Requests
|
106
|
+
|
107
|
+
* Fork the project.
|
108
|
+
* Make your feature addition or bug fix.
|
109
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
110
|
+
* Commit, do not mess with rakefile, version, or history.
|
111
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
112
|
+
* Send me a pull request. Bonus points for topic branches.
|
113
|
+
|
114
|
+
h3. Copyright
|
115
|
+
|
116
|
+
Copyright (c) 2010 Rainer Kuhn, LittleWebLab.com. See LICENSE for details.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.1
|
data/mongoid_tree.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid_tree}
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Rainer Kuhn"]
|
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
|
|
14
14
|
s.email = %q{rkuhn@littleweblab.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
|
-
"README.
|
17
|
+
"README.textile"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
".bundle/config",
|
@@ -26,7 +26,7 @@ Gem::Specification.new do |s|
|
|
26
26
|
"Gemfile",
|
27
27
|
"Gemfile.lock",
|
28
28
|
"LICENSE",
|
29
|
-
"README.
|
29
|
+
"README.textile",
|
30
30
|
"Rakefile",
|
31
31
|
"VERSION",
|
32
32
|
"autotest/discover.rb",
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 1
|
9
|
+
version: 0.3.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Rainer Kuhn
|
@@ -26,7 +26,7 @@ extensions: []
|
|
26
26
|
|
27
27
|
extra_rdoc_files:
|
28
28
|
- LICENSE
|
29
|
-
- README.
|
29
|
+
- README.textile
|
30
30
|
files:
|
31
31
|
- .bundle/config
|
32
32
|
- .gitignore
|
@@ -37,7 +37,7 @@ files:
|
|
37
37
|
- Gemfile
|
38
38
|
- Gemfile.lock
|
39
39
|
- LICENSE
|
40
|
-
- README.
|
40
|
+
- README.textile
|
41
41
|
- Rakefile
|
42
42
|
- VERSION
|
43
43
|
- autotest/discover.rb
|
data/README.rdoc
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
= mongoid_tree
|
2
|
-
|
3
|
-
For our commercial application we need a proper tree structure with Depth-First and Breadth-First searches, parent and child information. Also subtrees need to be exported to JSON.
|
4
|
-
|
5
|
-
Initially I thought of an embedded solution, but this will only be possible once MongoDB supports embedded collections, and even deep-embedded collections.
|
6
|
-
|
7
|
-
However this tree is right now on the top of our priority list, means we will put effort into this and release everything in this public gem, as soon as we implement and test it. It will be fully tested with RSpec.
|
8
|
-
The lack of a proper tree structure is quite noticeable in the mongoid forums.
|
9
|
-
|
10
|
-
This gem will receive long-term support since we use it in a commercial long-term application.
|
11
|
-
|
12
|
-
|
13
|
-
== Note on Patches/Pull Requests
|
14
|
-
|
15
|
-
* Fork the project.
|
16
|
-
* Make your feature addition or bug fix.
|
17
|
-
* Add tests for it. This is important so I don't break it in a
|
18
|
-
future version unintentionally.
|
19
|
-
* Commit, do not mess with rakefile, version, or history.
|
20
|
-
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
21
|
-
* Send me a pull request. Bonus points for topic branches.
|
22
|
-
|
23
|
-
== Copyright
|
24
|
-
|
25
|
-
Copyright (c) 2010 Rainer Kuhn. See LICENSE for details.
|