basic_tree 0.1.1 → 1.0.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.
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/README.rdoc CHANGED
@@ -1,9 +1,69 @@
1
- = basic_tree
1
+ = BasicTree
2
2
 
3
- Description goes here.
3
+ A basic Ruby tree structure with nice syntax.
4
+
5
+ == Example
6
+
7
+ fruit = BasicTree.new "Fruit" do
8
+ add "Apple" do
9
+ add "Red Delicious"
10
+ end
11
+ add "Banana" do
12
+ add "Manzano"
13
+ add "Plantain"
14
+ end
15
+ add "Orange"
16
+ end
17
+
18
+ fruit.object
19
+ # "Fruit"
20
+
21
+ banana = fruit.children[1]
22
+ banana.object
23
+ # "Banana"
24
+
25
+ plantain = banana.children[1]
26
+ plantain.object
27
+ # "Plantain"
28
+
29
+ plantain.parent.object
30
+ # "Banana""
31
+
32
+ plantain.path.map(&:object)
33
+ # ["Fruit", "Banana", "Plantain"]
34
+
35
+ plantain.ancestors.map(&:object) # ancestors is like path except it doesn't include itself
36
+ # ["Fruit", "Banana"]
37
+
38
+ banana.subtree.map(&:object)
39
+ # ["Banana", "Manzano", "Plantain"]
40
+
41
+ fruit.descendants.map(&:object) # descendants is like subtree except it doesn't include itself
42
+ # ["Apple", "Red Delicious", "Banana", "Manzano", "Plantain", "Orange"]
43
+
44
+ banana.siblings.map(&:object)
45
+ # ["Apple", "Orange"]
46
+
47
+ plantain.root.object
48
+ # "Fruit"
49
+
50
+ plantain.level
51
+ # 3
52
+
53
+ banana.root?
54
+ # false
55
+
56
+ fruit.root?
57
+ # true
58
+
59
+ banana.leaf?
60
+ # false
61
+
62
+ plantain.leaf?
63
+ # true
4
64
 
5
65
  == Note on Patches/Pull Requests
6
-
66
+
7
67
  * Fork the project.
8
68
  * Make your feature addition or bug fix.
9
69
  * Add tests for it. This is important so I don't break it in a
@@ -14,4 +74,4 @@ Description goes here.
14
74
 
15
75
  == Copyright
16
76
 
17
- Copyright (c) 2010 Austin Schneider. See LICENSE for details.
77
+ Copyright (c) 2010 Austin Schneider. See LICENSE for details.
data/lib/basic_tree.rb CHANGED
@@ -2,21 +2,21 @@ class BasicTree
2
2
 
3
3
  include Enumerable
4
4
 
5
- VERSION = "0.1.1"
5
+ VERSION = "1.0.0"
6
6
 
7
- def initialize(object, &block)
7
+ def initialize(object, parent = nil, &block)
8
8
  self.object = object
9
- yield self if block_given?
9
+ if parent
10
+ self.parent = parent
11
+ parent.children << self
12
+ end
13
+ instance_eval(&block) if block_given?
10
14
  end
11
15
 
12
16
  attr_accessor :object, :parent
13
17
 
14
18
  def add(object, &block)
15
- self.class.new(object) do |child|
16
- children << child
17
- child.parent = self
18
- yield child if block_given?
19
- end
19
+ self.class.new(object, self, &block)
20
20
  end
21
21
 
22
22
  def path
@@ -1,17 +1,21 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  describe BasicTree do
4
-
4
+
5
5
  before do
6
- @a = BasicTree.new "a" do |a|
7
- @a1 = a.add "a1" do |a1|
8
- @a11 = a1.add "a11"
9
- @a12 = a1.add "a12"
6
+ @a = BasicTree.new "a" do
7
+ add "a1" do
8
+ add "a11"
9
+ add "a12"
10
10
  end
11
- @a2 = a.add "a2"
11
+ add "a2"
12
12
  end
13
+ @a1 = @a.children[0]
14
+ @a2 = @a.children[1]
15
+ @a11 = @a1.children[0]
16
+ @a12 = @a1.children[1]
13
17
  end
14
-
18
+
15
19
  it "path" do
16
20
  @a.path.should == [@a]
17
21
  @a1.path.should == [@a, @a1]
@@ -19,7 +23,7 @@ describe BasicTree do
19
23
  @a12.path.should == [@a, @a1, @a12]
20
24
  @a2.path.should == [@a, @a2]
21
25
  end
22
-
26
+
23
27
  it "ancestors" do
24
28
  @a.ancestors.should == []
25
29
  @a1.ancestors.should == [@a]
@@ -27,7 +31,7 @@ describe BasicTree do
27
31
  @a12.ancestors.should == [@a, @a1]
28
32
  @a2.ancestors.should == [@a]
29
33
  end
30
-
34
+
31
35
  it "descendants" do
32
36
  @a.descendants.should == [@a1, @a11, @a12, @a2]
33
37
  @a1.descendants.should == [@a11, @a12]
@@ -35,7 +39,7 @@ describe BasicTree do
35
39
  @a12.descendants.should == []
36
40
  @a2.descendants.should == []
37
41
  end
38
-
42
+
39
43
  it "subtree" do
40
44
  @a.subtree.should == [@a, @a1, @a11, @a12, @a2]
41
45
  @a1.subtree.should == [@a1, @a11, @a12]
@@ -43,7 +47,7 @@ describe BasicTree do
43
47
  @a12.subtree.should == [@a12]
44
48
  @a2.subtree.should == [@a2]
45
49
  end
46
-
50
+
47
51
  it "siblings" do
48
52
  @a.siblings.should == []
49
53
  @a1.siblings.should == [@a2]
@@ -51,13 +55,13 @@ describe BasicTree do
51
55
  @a12.siblings.should == [@a11]
52
56
  @a2.siblings.should == [@a1]
53
57
  end
54
-
58
+
55
59
  it "root" do
56
60
  [@a, @a1, @a11, @a12, @a2].each do |m|
57
61
  m.root.should == @a
58
62
  end
59
63
  end
60
-
64
+
61
65
  it "level" do
62
66
  @a.level.should == 1
63
67
  @a1.level.should == 2
@@ -65,7 +69,7 @@ describe BasicTree do
65
69
  @a12.level.should == 3
66
70
  @a2.level.should == 2
67
71
  end
68
-
72
+
69
73
  it "root?" do
70
74
  @a.root?.should be_true
71
75
  @a1.root?.should be_false
@@ -73,7 +77,7 @@ describe BasicTree do
73
77
  @a12.root?.should be_false
74
78
  @a2.root?.should be_false
75
79
  end
76
-
80
+
77
81
  it "leaf?" do
78
82
  @a.leaf?.should be_false
79
83
  @a1.leaf?.should be_false
@@ -81,5 +85,5 @@ describe BasicTree do
81
85
  @a12.leaf?.should be_true
82
86
  @a2.leaf?.should be_true
83
87
  end
84
-
88
+
85
89
  end
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,8 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
1
+ require 'rspec'
2
+ # optionally add autorun support
3
+ # require 'rspec/autorun'
3
4
  require 'basic_tree'
4
- require 'spec'
5
- require 'spec/autorun'
6
5
 
7
- Spec::Runner.configure do |config|
8
-
6
+ Rspec.configure do |c|
7
+ c.mock_with :rspec
9
8
  end
metadata CHANGED
@@ -3,10 +3,10 @@ name: basic_tree
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
- - 0
7
- - 1
8
6
  - 1
9
- version: 0.1.1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Austin Schneider
@@ -41,6 +41,7 @@ extra_rdoc_files: []
41
41
 
42
42
  files:
43
43
  - .gitignore
44
+ - .rspec
44
45
  - .rvmrc
45
46
  - Gemfile
46
47
  - Gemfile.lock
@@ -49,8 +50,7 @@ files:
49
50
  - Rakefile
50
51
  - basic_tree.gemspec
51
52
  - lib/basic_tree.rb
52
- - spec/basic_tree_spec.rb
53
- - spec/spec.opts
53
+ - spec/lib/basic_tree_spec.rb
54
54
  - spec/spec_helper.rb
55
55
  has_rdoc: true
56
56
  homepage: http://github.com/soccer022483/basic_tree
data/spec/spec.opts DELETED
@@ -1 +0,0 @@
1
- --color