redwood 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,6 @@
1
+ .DS_Store
2
+ coverage
3
+ rdoc
4
+ pkg
5
+ doc
6
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'http://rubygems.org'
2
+
3
+ group :test do
4
+ gem 'contest'
5
+ end
6
+
7
+ group :development do
8
+ gem 'rake'
9
+ gem 'jeweler'
10
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Mark Wunsch
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.
@@ -0,0 +1,17 @@
1
+ = redwood
2
+
3
+ Not quite baked yet.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (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)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Mark Wunsch. See LICENSE for details.
@@ -0,0 +1,48 @@
1
+ require "rubygems"
2
+ require "bundler"
3
+ Bundler.setup(:development)
4
+
5
+ require 'rake'
6
+
7
+ begin
8
+ require 'jeweler'
9
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
10
+ require "redwood"
11
+ Jeweler::Tasks.new do |gemspec|
12
+ gemspec.name = "redwood"
13
+ gemspec.summary = "Ruby trees"
14
+ gemspec.description = "A simple library to create and manage basic tree-esque structures."
15
+ gemspec.version = Redwood::VERSION
16
+ gemspec.homepage = "http://github.com/mwunsch/redwood"
17
+ gemspec.authors = ["Mark Wunsch"]
18
+ gemspec.email = ["mark@markwunsch.com"]
19
+ gemspec.add_development_dependency "bundler", ">= 0.9.7"
20
+ end
21
+ Jeweler::GemcutterTasks.new
22
+ rescue LoadError
23
+ puts "Jeweler not available. Install it with: gem install jeweler"
24
+ end
25
+
26
+ require 'rake/testtask'
27
+ Rake::TestTask.new(:test) do |test|
28
+ test.libs << 'lib' << 'test'
29
+ test.pattern = 'test/**/test_*.rb'
30
+ test.verbose = true
31
+ end
32
+
33
+ task :default => :test
34
+
35
+ require 'rake/rdoctask'
36
+ Rake::RDocTask.new do |rdoc|
37
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
38
+
39
+ rdoc.rdoc_dir = 'rdoc'
40
+ rdoc.title = "redwood #{version}"
41
+ rdoc.rdoc_files.include('README*')
42
+ rdoc.rdoc_files.include('lib/**/*.rb')
43
+ end
44
+
45
+ desc "Open an irb session preloaded with this library"
46
+ task :console do
47
+ sh "irb -rubygems -I lib -r redwood"
48
+ end
@@ -0,0 +1,114 @@
1
+ # The Redood module can be mixed in to give tree-like methods to an object.
2
+ # Its primary implementation is the Redwood::Node class. Its only requirement
3
+ # is that the tree-infused-object's children and parent is an object that
4
+ # also mixes in Redwood. See Redwood::Node for the canononical representation.
5
+
6
+ module Redwood
7
+ VERSION = "0.0.1"
8
+
9
+ def parent
10
+ @parent
11
+ end
12
+
13
+ def name
14
+ end
15
+
16
+ def value
17
+ end
18
+
19
+ def root?
20
+ parent.nil?
21
+ end
22
+
23
+ def leaf?
24
+ children.empty?
25
+ end
26
+
27
+ def root
28
+ if root?
29
+ self
30
+ else
31
+ parent.root
32
+ end
33
+ end
34
+
35
+ def children
36
+ @children ||= []
37
+ end
38
+
39
+ def siblings
40
+ if parent
41
+ parent.children.reject {|child| child == self }
42
+ end
43
+ end
44
+
45
+ def only_child?
46
+ if parent
47
+ siblings.empty?
48
+ end
49
+ end
50
+
51
+ def childless?
52
+ children.nil? || children.empty?
53
+ end
54
+
55
+ def has_children?
56
+ !childless?
57
+ end
58
+
59
+ def ancestors
60
+ return @ancestors if @ancestors
61
+ @ancestors = []
62
+ if parent
63
+ @ancestors << parent
64
+ parent.ancestors.each {|ancestor| @ancestors << ancestor }
65
+ end
66
+ @ancestors
67
+ end
68
+
69
+ def descendants
70
+ return @descendants if @descendants
71
+ @descendants = []
72
+ if !children.empty?
73
+ (@descendants << children).flatten!
74
+ children.each {|descendant| @descendants << descendant.descendants }
75
+ @descendants.flatten!
76
+ end
77
+ @descendants
78
+ end
79
+
80
+ def depth
81
+ ancestors.size + 1
82
+ end
83
+
84
+ def view(content = :name)
85
+ treeview = ''
86
+ if parent
87
+ treeview += parent.children.last.eql?(self) ? "`" : "|"
88
+ treeview << "--\s"
89
+ end
90
+ treeview << "#{self.send(content)}"
91
+ if !children.empty?
92
+ treeview << "\n"
93
+ children.each do |child|
94
+ if parent
95
+ child.ancestors.reverse_each do |ancestor|
96
+ if !ancestor.root?
97
+ if ancestor.only_child? || ancestor.parent.children.last.eql?(ancestor)
98
+ treeview << "\s\s\s\s"
99
+ else
100
+ treeview << "|\s\s\s"
101
+ end
102
+ end
103
+ end
104
+ end
105
+ treeview << child.view(content)
106
+ treeview << "\n" if !children.last.eql?(child)
107
+ end
108
+ end
109
+ treeview
110
+ end
111
+
112
+ end
113
+
114
+ require 'redwood/node'
@@ -0,0 +1,25 @@
1
+ module Redwood
2
+ class Node
3
+ include Redwood
4
+
5
+ attr_reader :name
6
+
7
+ def initialize(name=nil, parent=nil)
8
+ @name = name
9
+ @parent = parent
10
+ end
11
+
12
+ # Creates a child, adds it to children, and returns the child
13
+ def add_child(name)
14
+ child = self.class.new(name, self)
15
+ children << child
16
+ child
17
+ end
18
+
19
+ def [](key)
20
+ selected_child = children.select {|child| child.name == key }
21
+ selected_child.size.eql?(1) ? selected_child.first : selected_child
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,10 @@
1
+ require "rubygems"
2
+ require "bundler"
3
+ Bundler.setup(:test)
4
+
5
+ require 'test/unit'
6
+ require 'contest'
7
+
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
10
+ require 'redwood'
@@ -0,0 +1,131 @@
1
+ require 'helper'
2
+
3
+ class TestRedwood < Test::Unit::TestCase
4
+ describe 'Node' do
5
+ test 'has a name' do
6
+ node = Redwood::Node.new(:test)
7
+ assert_equal :test, node.name
8
+ end
9
+
10
+ test 'can be without children' do
11
+ node = Redwood::Node.new
12
+ assert node.childless?
13
+ assert !node.has_children?
14
+ end
15
+
16
+ test 'can be a root element' do
17
+ node = Redwood::Node.new
18
+ assert node.root?
19
+ end
20
+
21
+ test 'add a child' do
22
+ node = Redwood::Node.new
23
+ assert node.children.empty?
24
+ node.add_child(:child)
25
+ assert_equal :child, node.children.first.name
26
+ end
27
+
28
+ test 'has children' do
29
+ node = Redwood::Node.new
30
+ node.add_child(:child)
31
+ assert_equal 1, node.children.size
32
+ end
33
+
34
+ test 'lookup children by named key' do
35
+ node = Redwood::Node.new
36
+ child = node.add_child(:child)
37
+ assert_equal child, node[:child]
38
+ end
39
+
40
+ test 'has siblings' do
41
+ node = Redwood::Node.new
42
+ child = node.add_child(:child)
43
+ bro = node.add_child(:bro)
44
+ sis = node.add_child(:sis)
45
+ assert_equal 2, child.siblings.size
46
+ assert child.siblings.include?(bro)
47
+ assert child.siblings.include?(sis)
48
+ assert !child.siblings.include?(child)
49
+ end
50
+
51
+ test 'is an only child' do
52
+ parent_one = Redwood::Node.new
53
+ parent_two = Redwood::Node.new
54
+ parent_one.add_child(:one_child)
55
+ parent_one.add_child(:one_bro)
56
+ parent_two.add_child(:two_child)
57
+
58
+ assert !parent_one.children.first.only_child?
59
+ assert parent_two.children.first.only_child?
60
+ end
61
+
62
+ test 'has ancestors' do
63
+ node = Redwood::Node.new(:parent)
64
+ son = node.add_child(:son)
65
+ daughter = node.add_child(:daughter)
66
+ grandson = son.add_child(:grandson)
67
+ greatgrandson = grandson.add_child(:greatgrandson)
68
+
69
+ ancestors = greatgrandson.ancestors
70
+
71
+ assert ancestors.include?(grandson)
72
+ assert ancestors.include?(son)
73
+ assert ancestors.include?(node)
74
+ assert !ancestors.include?(greatgrandson)
75
+ assert !ancestors.include?(daughter)
76
+ end
77
+
78
+ test 'has a root' do
79
+ node = Redwood::Node.new(:parent)
80
+ son = node.add_child(:son)
81
+ grandson = son.add_child(:grandson)
82
+ greatgrandson = grandson.add_child(:greatgrandson)
83
+
84
+ assert_equal node, greatgrandson.root
85
+ assert_equal node, node.root
86
+ end
87
+
88
+ test 'has descendants' do
89
+ node = Redwood::Node.new(:parent)
90
+ son = node.add_child(:son)
91
+ daughter = node.add_child(:daughter)
92
+ grandson = son.add_child(:grandson)
93
+ granddaughter = daughter.add_child(:granddaughter)
94
+ greatgrandson = grandson.add_child(:greatgrandson)
95
+
96
+ descendants = node.descendants
97
+ assert descendants.include?(son)
98
+ assert descendants.include?(daughter)
99
+ assert descendants.include?(grandson)
100
+ assert descendants.include?(granddaughter)
101
+ assert descendants.include?(greatgrandson)
102
+ assert !descendants.include?(node)
103
+ end
104
+
105
+ test 'has a depth' do
106
+ node = Redwood::Node.new(:parent)
107
+ son = node.add_child(:son)
108
+ daughter = node.add_child(:daughter)
109
+ grandson = son.add_child(:grandson)
110
+ greatgrandson = grandson.add_child(:greatgrandson)
111
+
112
+ assert_equal 1, node.depth
113
+ assert_equal daughter.depth, son.depth
114
+ assert_equal 3, grandson.depth
115
+ assert_equal 4, greatgrandson.depth
116
+ end
117
+
118
+ test 'has a treeview' do
119
+ node = Redwood::Node.new(:parent)
120
+ dog = node.add_child(:dog)
121
+ puppy = dog.add_child(:puppy)
122
+ son = node.add_child(:son)
123
+ daughter = node.add_child(:daughter)
124
+ grandson = son.add_child(:grandson)
125
+
126
+ assert_respond_to node, :view
127
+ assert_equal String, node.view.class
128
+ end
129
+
130
+ end
131
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redwood
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
+ - Mark Wunsch
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-09 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: bundler
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ segments:
27
+ - 0
28
+ - 9
29
+ - 7
30
+ version: 0.9.7
31
+ type: :development
32
+ prerelease: false
33
+ version_requirements: *id001
34
+ description: A simple library to create and manage basic tree-esque structures.
35
+ email:
36
+ - mark@markwunsch.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.md
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - Gemfile
48
+ - LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - lib/redwood.rb
52
+ - lib/redwood/node.rb
53
+ - test/helper.rb
54
+ - test/test_redwood.rb
55
+ has_rdoc: true
56
+ homepage: http://github.com/mwunsch/redwood
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --charset=UTF-8
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.3.6
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Ruby trees
85
+ test_files:
86
+ - test/helper.rb
87
+ - test/test_redwood.rb