dm-is-tree 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Timothy Bennett
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,4 @@
1
+ dm-is-tree
2
+ ==========
3
+
4
+ DataMapper plugin allowing the creation of tree structures from data models.
@@ -0,0 +1,65 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'rake/clean'
4
+ require 'rake/gempackagetask'
5
+ require 'spec/rake/spectask'
6
+ require 'pathname'
7
+
8
+ CLEAN.include '{log,pkg}/'
9
+
10
+ spec = Gem::Specification.new do |s|
11
+ s.name = 'dm-is-tree'
12
+ s.version = '0.9.2'
13
+ s.platform = Gem::Platform::RUBY
14
+ s.has_rdoc = true
15
+ s.extra_rdoc_files = %w[ README LICENSE TODO ]
16
+ s.summary = 'DataMapper plugin allowing the creation of tree structures from data models'
17
+ s.description = s.summary
18
+ s.author = 'Timothy Bennett'
19
+ s.email = 'leapord729@comcast.net'
20
+ s.homepage = 'http://github.com/sam/dm-more/tree/master/dm-is-tree'
21
+ s.require_path = 'lib'
22
+ s.files = FileList[ '{lib,spec}/**/*.rb', 'spec/spec.opts', 'Rakefile', *s.extra_rdoc_files ]
23
+ s.add_dependency('dm-core', "=#{s.version}")
24
+ end
25
+
26
+ task :default => [ :spec ]
27
+
28
+ WIN32 = (RUBY_PLATFORM =~ /win32|mingw|cygwin/) rescue nil
29
+ SUDO = WIN32 ? '' : ('sudo' unless ENV['SUDOLESS'])
30
+
31
+ Rake::GemPackageTask.new(spec) do |pkg|
32
+ pkg.gem_spec = spec
33
+ end
34
+
35
+ desc "Install #{spec.name} #{spec.version} (default ruby)"
36
+ task :install => [ :package ] do
37
+ sh "#{SUDO} gem install --local pkg/#{spec.name}-#{spec.version} --no-update-sources", :verbose => false
38
+ end
39
+
40
+ desc "Uninstall #{spec.name} #{spec.version} (default ruby)"
41
+ task :uninstall => [ :clobber ] do
42
+ sh "#{SUDO} gem uninstall #{spec.name} -v#{spec.version} -I -x", :verbose => false
43
+ end
44
+
45
+ namespace :jruby do
46
+ desc "Install #{spec.name} #{spec.version} with JRuby"
47
+ task :install => [ :package ] do
48
+ sh %{#{SUDO} jruby -S gem install --local pkg/#{spec.name}-#{spec.version} --no-update-sources}, :verbose => false
49
+ end
50
+ end
51
+
52
+ desc 'Run specifications'
53
+ Spec::Rake::SpecTask.new(:spec) do |t|
54
+ t.spec_opts << '--options' << 'spec/spec.opts' if File.exists?('spec/spec.opts')
55
+ t.spec_files = Pathname.glob(Pathname.new(__FILE__).dirname + 'spec/**/*_spec.rb')
56
+
57
+ begin
58
+ t.rcov = ENV.has_key?('NO_RCOV') ? ENV['NO_RCOV'] != 'true' : true
59
+ t.rcov_opts << '--exclude' << 'spec'
60
+ t.rcov_opts << '--text-summary'
61
+ t.rcov_opts << '--sort' << 'coverage' << '--sort-reverse'
62
+ rescue Exception
63
+ # rcov not installed
64
+ end
65
+ end
data/TODO ADDED
@@ -0,0 +1,8 @@
1
+ TODO
2
+ ====
3
+
4
+ * dm-is-tree requires specs
5
+
6
+ ---
7
+ TODO tickets may also be found in the DataMapper Issue Tracker:
8
+ http://wm.lighthouseapp.com/projects/4819-datamapper/overview
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'pathname'
3
+
4
+ gem 'dm-core', '=0.9.2'
5
+ require 'dm-core'
6
+
7
+ require Pathname(__FILE__).dirname.expand_path / 'dm-is-tree' / 'is' / 'tree.rb'
@@ -0,0 +1,134 @@
1
+ module DataMapper
2
+ module Is
3
+ module Tree
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ # An extension to DataMapper to easily allow the creation of tree
9
+ # structures from your DataMapper models.
10
+ # This requires a foreign key property for your model, which by default
11
+ # would be called :parent_id.
12
+ #
13
+ # Example:
14
+ #
15
+ # class Category
16
+ # include DataMapper::Resource
17
+ #
18
+ # property :id, Integer
19
+ # property :parent_id, Integer
20
+ # property :name, String
21
+ #
22
+ # is :tree :order => "name"
23
+ # end
24
+ #
25
+ # root
26
+ # +- child
27
+ # +- grandchild1
28
+ # +- grandchild2
29
+ #
30
+ # root = Category.create("name" => "root")
31
+ # child = root.children.create("name" => "child")
32
+ # grandchild1 = child1.children.create("name" => "grandchild1")
33
+ # grandchild2 = child2.children.create("name" => "grandchild2")
34
+ #
35
+ # root.parent # => nil
36
+ # child.parent # => root
37
+ # root.children # => [child]
38
+ # root.children.first.children.first # => grandchild1
39
+ # Category.first_root # => root
40
+ # Category.roots # => [root]
41
+ #
42
+ # The following instance methods are added:
43
+ # * <tt>children</tt> - Returns all nodes with the current node as their parent, in the order specified by
44
+ # <tt>:order</tt> (<tt>[grandchild1, grandchild2]</tt> when called on <tt>child</tt>)
45
+ # * <tt>parent</tt> - Returns the node referenced by the foreign key (<tt>:parent_id</tt> by
46
+ # default) (<tt>root</tt> when called on <tt>child</tt>)
47
+ # * <tt>siblings</tt> - Returns all the children of the parent, excluding the current node
48
+ # (<tt>[grandchild2]</tt> when called on <tt>grandchild1</tt>)
49
+ # * <tt>generation</tt> - Returns all the children of the parent, including the current node (<tt>
50
+ # [grandchild1, grandchild2]</tt> when called on <tt>grandchild1</tt>)
51
+ # * <tt>ancestors</tt> - Returns all the ancestors of the current node (<tt>[root, child1]</tt>
52
+ # when called on <tt>grandchild2</tt>)
53
+ # * <tt>root</tt> - Returns the root of the current node (<tt>root</tt> when called on <tt>grandchild2</tt>)
54
+ #
55
+ # Author:: Timothy Bennett (http://lanaer.com)
56
+
57
+ # Configuration options are:
58
+ #
59
+ # * <tt>child_key</tt> - specifies the column name to use for tracking of the tree (default: +parent_id+)
60
+ def is_tree(options = {})
61
+ configuration = { :child_key => :parent_id }
62
+ configuration.update(options) if Hash === options
63
+
64
+ belongs_to :parent, :class_name => name, :child_key => [ configuration[:child_key] ]
65
+ has n, :children, :class_name => name, :child_key => [ configuration[:child_key] ]
66
+
67
+ include DataMapper::Is::Tree::InstanceMethods
68
+ extend DataMapper::Is::Tree::ClassMethods
69
+
70
+ class_eval <<-CLASS, __FILE__, __LINE__
71
+ def self.roots
72
+ all :#{configuration[:child_key]} => nil, :order => #{configuration[:order].inspect}
73
+ end
74
+
75
+ def self.first_root
76
+ first :#{configuration[:child_key]} => nil, :order => #{configuration[:order].inspect}
77
+ end
78
+ CLASS
79
+
80
+ class << self
81
+ alias_method :root, :first_root # for people used to the ActiveRecord acts_as_tree
82
+ end
83
+ end
84
+
85
+ def is_a_tree(options = {})
86
+ warn('#is_a_tree is depreciated. use #is :tree instead.')
87
+ is :tree, options
88
+ end
89
+ alias_method :can_has_tree, :is_tree # just for fun ;)
90
+
91
+ module ClassMethods
92
+ end
93
+
94
+ module InstanceMethods
95
+ # Returns list of ancestors, starting with the root.
96
+ #
97
+ # grandchild1.ancestors # => [root, child]
98
+ def ancestors
99
+ node, nodes = self, []
100
+ nodes << node = node.parent while node.parent
101
+ nodes.reverse
102
+ end
103
+
104
+ # Returns the root node of the current node’s tree.
105
+ #
106
+ # grandchild1.root # => root
107
+ def root
108
+ node = self
109
+ node = node.parent while node.parent
110
+ node
111
+ end
112
+ alias_method :first_root, :root
113
+
114
+ # Returns all siblings of the current node.
115
+ #
116
+ # grandchild1.siblings # => [grandchild2]
117
+ def siblings
118
+ generation - [self]
119
+ end
120
+
121
+ # Returns all children of the current node’s parent.
122
+ #
123
+ # grandchild1.generation # => [grandchild1, grandchild2]
124
+ def generation
125
+ parent ? parent.children : self.class.roots
126
+ end
127
+ alias_method :self_and_siblings, :generation # for those used to the ActiveRecord acts_as_tree
128
+
129
+ end
130
+
131
+ Model.send(:include, self)
132
+ end # Tree
133
+ end # Is
134
+ end # DataMapper
@@ -0,0 +1,2 @@
1
+ --format specdoc
2
+ --colour
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dm-is-tree
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.2
5
+ platform: ruby
6
+ authors:
7
+ - Timothy Bennett
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-06-25 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: dm-core
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - "="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.9.2
23
+ version:
24
+ description: DataMapper plugin allowing the creation of tree structures from data models
25
+ email: leapord729@comcast.net
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README
32
+ - LICENSE
33
+ - TODO
34
+ files:
35
+ - lib/dm-is-tree/is/tree.rb
36
+ - lib/dm-is-tree.rb
37
+ - spec/spec.opts
38
+ - Rakefile
39
+ - README
40
+ - LICENSE
41
+ - TODO
42
+ has_rdoc: true
43
+ homepage: http://github.com/sam/dm-more/tree/master/dm-is-tree
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.0.1
65
+ signing_key:
66
+ specification_version: 2
67
+ summary: DataMapper plugin allowing the creation of tree structures from data models
68
+ test_files: []
69
+