public
Rubygem
Fork of sam/dm-more
Description: Extras for DataMapper, including bridges to DataObjects::Migrations and Merb::DataMapper
Homepage: http://datamapper.org
Clone URL: git://github.com/dysinger/dm-more.git
Search Repo:
Make dm-is-tree more gem alike
oleg dashevskii (author)
Mon May 05 10:19:33 -0700 2008
commit  7b24d84a53c4f33a8e39788b937d339f5d46ee13
tree    d815aadccdf242ac201c86eb5b5f6b03d5d2099d
parent  ad401992c490ecf22f968bdcf53ac2821db27ef9
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -1,128 +0,0 @@
0
-module DataMapper
0
- module Is
0
- module Tree
0
- def self.included(base)
0
- base.extend(ClassMethods)
0
- end
0
-
0
- # An extension to DataMapper to easily allow the creation of tree
0
- # structures from your DataMapper models.
0
- # This requires a foreign key property for your model, which by default
0
- # would be called :parent_id.
0
- #
0
- # Example:
0
- #
0
- # class Category
0
- # include DataMapper::Resource
0
- # include DataMapper::Is::Tree
0
- #
0
- # property :id, Fixnum
0
- # property :parent_id, Fixnum
0
- # property :name, String
0
- #
0
- # is_a_tree :order => "name"
0
- # end
0
- #
0
- # root
0
- # +- child
0
- # +- grandchild1
0
- # +- grandchild2
0
- #
0
- # root = Category.create("name" => "root")
0
- # child = root.children.create("name" => "child")
0
- # grandchild1 = child1.children.create("name" => "grandchild1")
0
- # grandchild2 = child2.children.create("name" => "grandchild2")
0
- #
0
- # root.parent # => nil
0
- # child.parent # => root
0
- # root.children # => [child]
0
- # root.children.first.children.first # => grandchild1
0
- # Category.first_root # => root
0
- # Category.roots # => [root]
0
- #
0
- # The following instance methods are added:
0
- # * <tt>children</tt> - Returns all nodes with the current node as their parent, in the order specified by
0
- # <tt>:order</tt> (<tt>[grandchild1, grandchild2]</tt> when called on <tt>child</tt>)
0
- # * <tt>parent</tt> - Returns the node referenced by the foreign key (<tt>:parent_id</tt> by
0
- # default) (<tt>root</tt> when called on <tt>child</tt>)
0
- # * <tt>siblings</tt> - Returns all the children of the parent, excluding the current node
0
- # (<tt>[grandchild2]</tt> when called on <tt>grandchild1</tt>)
0
- # * <tt>generation</tt> - Returns all the children of the parent, including the current node (<tt>
0
- # [grandchild1, grandchild2]</tt> when called on <tt>grandchild1</tt>)
0
- # * <tt>ancestors</tt> - Returns all the ancestors of the current node (<tt>[root, child1]</tt>
0
- # when called on <tt>grandchild2</tt>)
0
- # * <tt>root</tt> - Returns the root of the current node (<tt>root</tt> when called on <tt>grandchild2</tt>)
0
- #
0
- # Author:: Timothy Bennett (http://lanaer.com)
0
- module ClassMethods
0
- # Configuration options are:
0
- #
0
- # * <tt>child_key</tt> - specifies the column name to use for tracking of the tree (default: +parent_id+)
0
- # * <tt>order</tt> - makes it possible to sort the children according to this SQL snippet.
0
- # * <tt>counter_cache</tt> - keeps a count in a +children_count+ column if set to +true+ (default: +false+).
0
- def is_a_tree(options = {})
0
- configuration = { :child_key => "parent_id" }
0
- configuration.update(options) if Hash === options
0
-
0
- belongs_to :parent, :class_name => name, :child_key => configuration[:child_key], :counter_cache => configuration[:counter_cache]
0
- has n, :children, :class_name => name, :child_key => configuration[:child_key], :order => configuration[:order]
0
-
0
- include DataMapper::Is::Tree::InstanceMethods
0
-
0
- class_eval <<-CLASS, __FILE__, __LINE__
0
- def self.roots
0
- all :#{configuration[:child_key]} => nil, :order => #{configuration[:order].inspect}
0
- end
0
-
0
- def self.first_root
0
- first :#{configuration[:child_key]} => nil, :order => #{configuration[:order].inspect}
0
- end
0
- CLASS
0
-
0
- class << self
0
- alias_method :root, :first_root # for people used to the ActiveRecord acts_as_tree
0
- end
0
- end
0
-
0
- alias_method :can_has_tree, :is_a_tree # just for fun ;)
0
- end
0
-
0
- module InstanceMethods
0
- # Returns list of ancestors, starting with the root.
0
- #
0
- # grandchild1.ancestors # => [root, child]
0
- def ancestors
0
- node, nodes = self, []
0
- nodes << node = node.parent while node.parent
0
- nodes.reverse
0
- end
0
-
0
- # Returns the root node of the current node’s tree.
0
- #
0
- # grandchild1.root # => root
0
- def root
0
- node = self
0
- node = node.parent while node.parent
0
- node
0
- end
0
-
0
- # Returns all siblings of the current node.
0
- #
0
- # grandchild1.siblings # => [grandchild2]
0
- def siblings
0
- generation - [self]
0
- end
0
-
0
- # Returns all children of the current node’s parent.
0
- #
0
- # grandchild1.generation # => [grandchild1, grandchild2]
0
- def generation
0
- parent ? parent.children : self.class.roots
0
- end
0
-
0
- alias_method :self_and_siblings, :generation # for those used to the ActiveRecord acts_as_tree
0
- end
0
-
0
- end # Tree
0
- end # Is
0
-end # DataMapper
...
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
0
@@ -0,0 +1,7 @@
0
+require 'rubygems'
0
+require 'pathname'
0
+
0
+gem 'dm-core', '=0.9.0'
0
+require 'data_mapper'
0
+
0
+require Pathname(__FILE__).dirname.expand_path / 'dm-is-tree' / 'is' / 'tree.rb'
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
0
@@ -0,0 +1,128 @@
0
+module DataMapper
0
+ module Is
0
+ module Tree
0
+ def self.included(base)
0
+ base.extend(ClassMethods)
0
+ end
0
+
0
+ # An extension to DataMapper to easily allow the creation of tree
0
+ # structures from your DataMapper models.
0
+ # This requires a foreign key property for your model, which by default
0
+ # would be called :parent_id.
0
+ #
0
+ # Example:
0
+ #
0
+ # class Category
0
+ # include DataMapper::Resource
0
+ # include DataMapper::Is::Tree
0
+ #
0
+ # property :id, Fixnum
0
+ # property :parent_id, Fixnum
0
+ # property :name, String
0
+ #
0
+ # is_a_tree :order => "name"
0
+ # end
0
+ #
0
+ # root
0
+ # +- child
0
+ # +- grandchild1
0
+ # +- grandchild2
0
+ #
0
+ # root = Category.create("name" => "root")
0
+ # child = root.children.create("name" => "child")
0
+ # grandchild1 = child1.children.create("name" => "grandchild1")
0
+ # grandchild2 = child2.children.create("name" => "grandchild2")
0
+ #
0
+ # root.parent # => nil
0
+ # child.parent # => root
0
+ # root.children # => [child]
0
+ # root.children.first.children.first # => grandchild1
0
+ # Category.first_root # => root
0
+ # Category.roots # => [root]
0
+ #
0
+ # The following instance methods are added:
0
+ # * <tt>children</tt> - Returns all nodes with the current node as their parent, in the order specified by
0
+ # <tt>:order</tt> (<tt>[grandchild1, grandchild2]</tt> when called on <tt>child</tt>)
0
+ # * <tt>parent</tt> - Returns the node referenced by the foreign key (<tt>:parent_id</tt> by
0
+ # default) (<tt>root</tt> when called on <tt>child</tt>)
0
+ # * <tt>siblings</tt> - Returns all the children of the parent, excluding the current node
0
+ # (<tt>[grandchild2]</tt> when called on <tt>grandchild1</tt>)
0
+ # * <tt>generation</tt> - Returns all the children of the parent, including the current node (<tt>
0
+ # [grandchild1, grandchild2]</tt> when called on <tt>grandchild1</tt>)
0
+ # * <tt>ancestors</tt> - Returns all the ancestors of the current node (<tt>[root, child1]</tt>
0
+ # when called on <tt>grandchild2</tt>)
0
+ # * <tt>root</tt> - Returns the root of the current node (<tt>root</tt> when called on <tt>grandchild2</tt>)
0
+ #
0
+ # Author:: Timothy Bennett (http://lanaer.com)
0
+ module ClassMethods
0
+ # Configuration options are:
0
+ #
0
+ # * <tt>child_key</tt> - specifies the column name to use for tracking of the tree (default: +parent_id+)
0
+ # * <tt>order</tt> - makes it possible to sort the children according to this SQL snippet.
0
+ # * <tt>counter_cache</tt> - keeps a count in a +children_count+ column if set to +true+ (default: +false+).
0
+ def is_a_tree(options = {})
0
+ configuration = { :child_key => "parent_id" }
0
+ configuration.update(options) if Hash === options
0
+
0
+ belongs_to :parent, :class_name => name, :child_key => configuration[:child_key], :counter_cache => configuration[:counter_cache]
0
+ has n, :children, :class_name => name, :child_key => configuration[:child_key], :order => configuration[:order]
0
+
0
+ include DataMapper::Is::Tree::InstanceMethods
0
+
0
+ class_eval <<-CLASS, __FILE__, __LINE__
0
+ def self.roots
0
+ all :#{configuration[:child_key]} => nil, :order => #{configuration[:order].inspect}
0
+ end
0
+
0
+ def self.first_root
0
+ first :#{configuration[:child_key]} => nil, :order => #{configuration[:order].inspect}
0
+ end
0
+ CLASS
0
+
0
+ class << self
0
+ alias_method :root, :first_root # for people used to the ActiveRecord acts_as_tree
0
+ end
0
+ end
0
+
0
+ alias_method :can_has_tree, :is_a_tree # just for fun ;)
0
+ end
0
+
0
+ module InstanceMethods
0
+ # Returns list of ancestors, starting with the root.
0
+ #
0
+ # grandchild1.ancestors # => [root, child]
0
+ def ancestors
0
+ node, nodes = self, []
0
+ nodes << node = node.parent while node.parent
0
+ nodes.reverse
0
+ end
0
+
0
+ # Returns the root node of the current node’s tree.
0
+ #
0
+ # grandchild1.root # => root
0
+ def root
0
+ node = self
0
+ node = node.parent while node.parent
0
+ node
0
+ end
0
+
0
+ # Returns all siblings of the current node.
0
+ #
0
+ # grandchild1.siblings # => [grandchild2]
0
+ def siblings
0
+ generation - [self]
0
+ end
0
+
0
+ # Returns all children of the current node’s parent.
0
+ #
0
+ # grandchild1.generation # => [grandchild1, grandchild2]
0
+ def generation
0
+ parent ? parent.children : self.class.roots
0
+ end
0
+
0
+ alias_method :self_and_siblings, :generation # for those used to the ActiveRecord acts_as_tree
0
+ end
0
+
0
+ end # Tree
0
+ end # Is
0
+end # DataMapper

Comments

    No one has commented yet.