mongoid-tree 0.6.2 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,5 +2,5 @@ source :rubygems
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'bson_ext', '>= 1.0.4'
5
+ gem 'bson_ext', '>= 1.0.4', :platform => :ruby
6
6
  gem 'SystemTimer', '>= 1.2.0', :platform => :ruby_18
data/Rakefile CHANGED
@@ -7,8 +7,7 @@ RSpec::Core::RakeTask.new(:spec)
7
7
 
8
8
  task :default => :spec
9
9
 
10
- Rake::RDocTask.new do |rdoc|
11
- rdoc.generator = 'hanna'
10
+ RDoc::Task.new do |rdoc|
12
11
  rdoc.rdoc_dir = 'doc'
13
12
  rdoc.title = "#{spec.name} #{spec.version}"
14
13
  rdoc.options += spec.rdoc_options
@@ -76,9 +76,9 @@ module Mongoid # :nodoc:
76
76
  # They're extended into the base class automatically.
77
77
  module ClassMethods # :nodoc:
78
78
  def traverse(type = :depth_first, &block)
79
- raise ArgumentError, "No block given" unless block_given?
80
- roots.each { |root| root.traverse(type, &block) }
81
- nil
79
+ res = []
80
+ roots.each { |root| res.concat root.traverse(type, &block) }
81
+ res
82
82
  end
83
83
  end
84
84
 
@@ -94,9 +94,14 @@ module Mongoid # :nodoc:
94
94
  # root.traverse(:depth_first) do |node|
95
95
  # results << node
96
96
  # end
97
+ #
98
+ # root.traverse(:depth_first).map(&:name)
99
+ #
97
100
  def traverse(type = :depth_first, &block)
98
- raise ArgumentError, "No block given" unless block_given?
101
+ res = []
102
+ block ||= lambda { |node| res << node }
99
103
  send("#{type}_traversal", &block)
104
+ res
100
105
  end
101
106
 
102
107
  private
@@ -8,10 +8,6 @@ describe Mongoid::Tree::Traversal do
8
8
 
9
9
  subject { Node.new }
10
10
 
11
- it "should require a block" do
12
- expect { subject.traverse }.to raise_error(/No block given/)
13
- end
14
-
15
11
  [:depth_first, :breadth_first].each do |method|
16
12
  it "should support #{method} traversal" do
17
13
  expect { subject.traverse(method) {} }.to_not raise_error
@@ -86,12 +82,17 @@ describe Mongoid::Tree::Traversal do
86
82
  node(:node5).move_above(node(:node6))
87
83
  end
88
84
 
89
- it 'should return the nodes in the correct order' do
85
+ it 'should iterate through the nodes in the correct order' do
90
86
  result = []
91
87
  node(:node1).traverse(:depth_first) { |node| result << node }
92
88
  result.collect { |n| n.name.to_sym }.should == [:node1, :node2, :node3, :node4, :node5, :node6, :node7]
93
89
  end
94
90
 
91
+ it 'should return the nodes in the correct order' do
92
+ result = node(:node1).traverse(:depth_first)
93
+ result.collect { |n| n.name.to_sym }.should == [:node1, :node2, :node3, :node4, :node5, :node6, :node7]
94
+ end
95
+
95
96
  end
96
97
 
97
98
  end
@@ -117,21 +118,12 @@ describe Mongoid::Tree::Traversal do
117
118
  end
118
119
 
119
120
  describe '.traverse' do
120
-
121
- describe 'when not given a block' do
122
-
123
- it 'raises an error' do
124
- expect {Node.traverse}.to raise_error ArgumentError, 'No block given'
125
- end
126
- end
127
-
128
121
  before :each do
129
122
  setup_tree <<-ENDTREE
130
123
  - root1
131
124
  - root2
132
125
  ENDTREE
133
126
 
134
- @block = Proc.new {}
135
127
  @root1 = node(:root1)
136
128
  @root2 = node(:root2)
137
129
 
@@ -141,35 +133,31 @@ describe Mongoid::Tree::Traversal do
141
133
  it 'grabs each root' do
142
134
  Node.should_receive(:roots).and_return []
143
135
 
144
- Node.traverse &@block
136
+ Node.traverse.should == []
145
137
  end
146
138
 
147
139
  it 'defaults the "type" arg to :depth_first' do
148
- @root1.should_receive(:traverse).with(:depth_first)
149
- @root2.should_receive(:traverse).with(:depth_first)
140
+ @root1.should_receive(:traverse).with(:depth_first).and_return([])
141
+ @root2.should_receive(:traverse).with(:depth_first).and_return([])
150
142
 
151
- Node.traverse &@block
143
+ Node.traverse.should == []
152
144
  end
153
145
 
154
146
  it 'traverses each root' do
155
- @root1.should_receive(:traverse)
156
- @root2.should_receive(:traverse)
147
+ @root1.should_receive(:traverse).and_return([1, 2])
148
+ @root2.should_receive(:traverse).and_return([3, 4])
157
149
 
158
- Node.traverse &@block
150
+ Node.traverse.should == [1, 2, 3, 4]
159
151
  end
160
152
 
161
153
  describe 'when the "type" arg is :breadth_first' do
162
154
 
163
155
  it 'traverses breadth-first' do
164
- @root1.should_receive(:traverse).with(:breadth_first)
165
- @root2.should_receive(:traverse).with(:breadth_first)
156
+ @root1.should_receive(:traverse).with(:breadth_first).and_return([])
157
+ @root2.should_receive(:traverse).with(:breadth_first).and_return([])
166
158
 
167
- Node.traverse :breadth_first, &@block
159
+ Node.traverse :breadth_first
168
160
  end
169
161
  end
170
-
171
- it 'returns nil' do
172
- Node.traverse(&@block).should be nil
173
- end
174
162
  end
175
163
  end
@@ -45,7 +45,7 @@ describe Mongoid::Tree do
45
45
  it "should save its unsaved children" do
46
46
  root = Node.new(:name => 'root'); child = Node.new(:name => 'child')
47
47
  root.children << child
48
- child.should_receive(:save)
48
+ child.should_receive(:save).at_most(2).times
49
49
  root.save
50
50
  end
51
51
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.7.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-23 00:00:00.000000000Z
12
+ date: 2012-02-06 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
16
- requirement: &70159403726680 !ruby/object:Gem::Requirement
16
+ requirement: &70188241262500 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '2.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70159403726680
24
+ version_requirements: *70188241262500
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70159403726180 !ruby/object:Gem::Requirement
27
+ requirement: &70188241262000 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.8.7
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70159403726180
35
+ version_requirements: *70188241262000
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70159403725700 !ruby/object:Gem::Requirement
38
+ requirement: &70188241261520 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,29 +43,29 @@ dependencies:
43
43
  version: '2.3'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70159403725700
46
+ version_requirements: *70188241261520
47
47
  - !ruby/object:Gem::Dependency
48
- name: autotest
49
- requirement: &70159403755660 !ruby/object:Gem::Requirement
48
+ name: rdoc
49
+ requirement: &70188241261040 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
- - - ! '>='
52
+ - - ~>
53
53
  - !ruby/object:Gem::Version
54
- version: 4.3.2
54
+ version: '2.4'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70159403755660
57
+ version_requirements: *70188241261040
58
58
  - !ruby/object:Gem::Dependency
59
- name: hanna-nouveau
60
- requirement: &70159403755180 !ruby/object:Gem::Requirement
59
+ name: autotest
60
+ requirement: &70188241260560 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
64
64
  - !ruby/object:Gem::Version
65
- version: 0.2.2
65
+ version: 4.3.2
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70159403755180
68
+ version_requirements: *70188241260560
69
69
  description: A tree structure for Mongoid documents using the materialized path pattern
70
70
  email:
71
71
  - benedikt@synatic.net