mm_tree 0.1.0 → 0.2.0

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.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/mm_tree.rb +72 -0
  3. data/mm_tree.gemspec +52 -0
  4. metadata +6 -2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -0,0 +1,72 @@
1
+ module MMTree
2
+ module InstanceMethods
3
+ def root?
4
+ self.depth.zero?
5
+ end
6
+
7
+ def root
8
+ node = self
9
+ node = node.parent while node.parent_id
10
+ node
11
+ end
12
+
13
+ def ancestors
14
+ node, nodes = self, []
15
+ nodes << node = node.parent while node.parent_id
16
+ nodes
17
+ end
18
+
19
+ def self_and_siblings
20
+ parent ? parent.children : self.class.roots
21
+ end
22
+
23
+ def siblings
24
+ siblings = self_and_siblings.dup
25
+ siblings.shift
26
+ siblings
27
+ end
28
+
29
+ private
30
+ def set_depth
31
+ unless self.parent_id.blank?
32
+ parent = self.parent
33
+ self.depth = parent.depth + 1
34
+ self.display_order = parent.children.count
35
+ save
36
+ end
37
+ end
38
+
39
+ end
40
+
41
+ def self.included klass
42
+ klass.class_eval do
43
+ belongs_to :parent,
44
+ :class_name => name,
45
+ :foreign_key => :parent_id
46
+
47
+ many :children,
48
+ :as => :parent,
49
+ :class_name => name,
50
+ :foreign_key => :parent_id
51
+
52
+ key :parent_id, ObjectId
53
+ key :parent_type, String
54
+ key :depth, Integer, :default => 0
55
+
56
+
57
+ after_create :set_depth
58
+
59
+ class_eval <<-EOV
60
+ def self.roots
61
+ find(:all, :conditions => {:parent_id=>nil}, :order => "depth")
62
+ end
63
+
64
+ def self.root
65
+ find(:first,:conditions => {:parent_id=>nil},:order => "depth")
66
+ end
67
+ EOV
68
+
69
+ include InstanceMethods
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,52 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mm_tree}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["twoism"]
12
+ s.date = %q{2009-12-18}
13
+ s.description = %q{acts_as_tree port for MongoMapper}
14
+ s.email = %q{signalstatic@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "README.rdoc",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "lib/mm_tree.rb",
24
+ "mm_tree.gemspec",
25
+ "test/schema.rb",
26
+ "test/test_helper.rb",
27
+ "test/traversal_test.rb",
28
+ "test/tree_test.rb"
29
+ ]
30
+ s.homepage = %q{http://github.com/twoism/mm_tree}
31
+ s.rdoc_options = ["--charset=UTF-8"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = %q{1.3.5}
34
+ s.summary = %q{acts_as_tree port for MongoMapper}
35
+ s.test_files = [
36
+ "test/schema.rb",
37
+ "test/test_helper.rb",
38
+ "test/traversal_test.rb",
39
+ "test/tree_test.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
+ else
48
+ end
49
+ else
50
+ end
51
+ end
52
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mm_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - twoism
@@ -26,10 +26,14 @@ files:
26
26
  - README.rdoc
27
27
  - Rakefile
28
28
  - VERSION
29
+ - lib/mm_tree.rb
30
+ - mm_tree.gemspec
29
31
  - test/schema.rb
30
32
  - test/test_helper.rb
33
+ - test/traversal_test.rb
34
+ - test/tree_test.rb
31
35
  has_rdoc: true
32
- homepage: http://github.com/twoism/tree
36
+ homepage: http://github.com/twoism/mm_tree
33
37
  licenses: []
34
38
 
35
39
  post_install_message: