mongoid-tree-rational 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 05a801b4f6d70f729fa1f32d6836dc5404beb672
4
- data.tar.gz: 500f13152270fe33af60bb9536f796f790082310
3
+ metadata.gz: 88f9bff4026794de9ad93338fe6179d545afa3cd
4
+ data.tar.gz: 46ecc1059a6810f063840be8cfaf06ee0d327303
5
5
  SHA512:
6
- metadata.gz: 868ea89f0264f823d4deb7046e0078a2b2b74059326dc8b477299856539ea7d265f058912eecd8160190eca5e8b434922b85b5e57d79b79f6c34e4d6d737ceaa
7
- data.tar.gz: 72d879b3c8ac06062bde4ebfb2ddcb68e832ea517744fe8130cd1846728ffdb2a879842f686e6e007a9a85216f7897873765fcea5f61130cf59cba29aac97d52
6
+ metadata.gz: 31247f27931a5024419b8842a9acce0c7b0f45f0dc02ca9090b3ebd7efa2d1bd39f9ecf25928c976f29c435cbf1f090649940cefc2f79f0d78e435097b224ec8
7
+ data.tar.gz: b48ca68a54c1a8a4c3bdfd6112564536653d4d47ae6a8a4f328e85ffd39642c8058a3b02915e37d520eb9bd995246c50b9ba1562829f734d8812fa51a73dc73e
data/Gemfile CHANGED
@@ -14,13 +14,12 @@ group :development do
14
14
  gem 'wdm', :platforms => [:mswin, :mingw], :require => false
15
15
  gem 'awesome_print'
16
16
  gem 'timecop'
17
+ platforms :rbx do
18
+ gem 'rubysl-rake', '~> 2.0'
19
+ end
17
20
  end
18
21
 
19
22
  group :development, :test do
20
23
  gem 'coveralls', :require => false
21
24
  gem 'simplecov', :require => false
22
25
  end
23
-
24
- platforms :rbx do
25
- gem 'rubysl-rake', '~> 2.0'
26
- end
data/README.md CHANGED
@@ -78,8 +78,7 @@ See `Mongoid::Tree` for more information on these methods.
78
78
 
79
79
  To use rational ordering, include the `Mongoid::Tree::RationalNumbering` module. This will add a `position` field to your document and provide additional utility methods:
80
80
 
81
- While rational numbering requires more processing when saving, it does give the benefit of querying an entire tree in one go.
82
-
81
+ While rational numbering requires more processing when saving, it does give the benefit of querying an entire tree in one query. This is really use
83
82
 
84
83
  Mathematical details about rational numbers in nested trees can be found here: [http://arxiv.org/pdf/0806.3115v1.pdf](http://arxiv.org/pdf/0806.3115v1.pdf)
85
84
 
@@ -125,7 +124,7 @@ Node.rekey_all! # Will iterate over the entire tree and rekey every single node.
125
124
  end
126
125
  ```
127
126
 
128
- You can get the entire tree in one go like this:
127
+ Examples on querying trees:
129
128
 
130
129
  ```ruby
131
130
  # - node_1
@@ -141,6 +140,14 @@ You can get the entire tree in one go like this:
141
140
 
142
141
  Node.all # Get the entire tree
143
142
  # -> [node_1, node_1_1, node_1_2, node_2, node_2_1, node_2_1_1, node_2_1_2, node_2_2, node_2_2_1, node_2_2_2]
143
+ node_1.tree # Get tree below node 1
144
+ # -> [node_1_1, node_1_2]
145
+ node_1.tree_and_self # Get tree below node 1 including node_1
146
+ # -> [node_1, node_1_1, node_1_2]
147
+ node_2.tree # Get tree below node 1 including node_1
148
+ # -> [node_2_1, node_2_1_1, node_2_1_2, node_2_2, node_2_2_1, node_2_2_2]
149
+ node_2.tree_and_self # Get tree below node 1 including node_1
150
+ # -> [node_2, node_2_1, node_2_1_1, node_2_1_2, node_2_2, node_2_2_1, node_2_2_2]
144
151
  end
145
152
  ```
146
153
 
data/Rakefile CHANGED
@@ -1,43 +1,14 @@
1
1
  #!/usr/bin/env rake
2
2
  begin
3
3
  require 'bundler/setup'
4
+ require "bundler/gem_tasks"
4
5
  rescue LoadError
5
6
  puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
7
  end
7
- begin
8
- require 'rdoc/task'
9
- rescue LoadError
10
- require 'rdoc/rdoc'
11
- require 'rake/rdoctask'
12
- RDoc::Task = Rake::RDocTask
13
- end
14
-
15
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
16
-
17
- require 'jeweler'
18
- Jeweler::Tasks.new do |gem|
19
- # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
20
- gem.name = "mongoid-tree-rational"
21
- gem.version = version
22
- gem.homepage = "https://github.com/boxcms/mongoid-tree-rational"
23
- gem.license = "MIT"
24
- gem.summary = %Q{A tree structure for Mongoid documents with rational numbers}
25
- gem.description = %Q{A tree structure for Mongoid documents using the materialized path pattern and rational number sorting.}
26
- gem.email = "leifcr@gmail.com"
27
- gem.authors = ['Leif Ringstad', 'Benedikt Deicke']
28
- # dependencies defined in Gemfile
29
- end
30
- Jeweler::RubygemsDotOrgTasks.new
31
8
 
32
- # RDoc::Task.new(:rdoc) do |rdoc|
33
- # rdoc.rdoc_dir = 'rdoc'
34
- # rdoc.title = "Mongoid Tree Rational #{version}"
35
- # rdoc.options << '--line-numbers'
36
- # rdoc.rdoc_files.include('README.rdoc')
37
- # rdoc.rdoc_files.include('lib/**/*.rb')
38
- # end
9
+ require 'yard'
39
10
 
40
- # Bundler::GemHelper.install_tasks
11
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
12
 
42
13
  require 'rspec/core'
43
14
  require 'rspec/core/rake_task'
@@ -45,17 +16,14 @@ RSpec::Core::RakeTask.new(:spec) do |spec|
45
16
  spec.pattern = FileList['spec/**/*_spec.rb']
46
17
  end
47
18
 
48
- RSpec::Core::RakeTask.new(:rcov) do |spec|
49
- spec.pattern = 'spec/**/*_spec.rb'
50
- spec.rcov = true
51
- end
52
-
53
19
  task :default => :spec
54
20
 
55
- # YARD::Rake::YardocTask.new(:doc)
21
+ YARD::Rake::YardocTask.new(:doc)
56
22
 
57
- desc "Open an irb session"
23
+ desc "Open an irb session with spec_helper loaded"
58
24
  task :test_console do
59
25
  require 'ap'
60
26
  sh "irb -rubygems -I lib -r ./spec/spec_helper.rb"
61
- end
27
+ end
28
+
29
+ gemspec = eval(File.read("mongoid-tree-rational.gemspec"))
@@ -361,7 +361,7 @@ module Mongoid
361
361
  ##
362
362
  # Forces rearranging of all children after next save
363
363
  #
364
- # @return [undefined]
364
+ # @return [void]
365
365
  def rearrange_children!
366
366
  @rearrange_children = true
367
367
  end
@@ -377,7 +377,7 @@ module Mongoid
377
377
  ##
378
378
  # Nullifies all children's parent_id
379
379
  #
380
- # @return [undefined]
380
+ # @return [void]
381
381
  def nullify_children
382
382
  children.each do |c|
383
383
  c.parent = c.parent_id = nil
@@ -388,7 +388,7 @@ module Mongoid
388
388
  ##
389
389
  # Moves all children to this document's parent
390
390
  #
391
- # @return [undefined]
391
+ # @return [void]
392
392
  def move_children_to_parent
393
393
  children.each do |c|
394
394
  c.parent = self.parent
@@ -399,7 +399,7 @@ module Mongoid
399
399
  ##
400
400
  # Deletes all descendants using the database (doesn't invoke callbacks)
401
401
  #
402
- # @return [undefined]
402
+ # @return [void]
403
403
  def delete_descendants
404
404
  base_class.delete_all(:conditions => { :parent_ids => self.id })
405
405
  end
@@ -407,7 +407,7 @@ module Mongoid
407
407
  ##
408
408
  # Destroys all children by calling their #destroy method (does invoke callbacks)
409
409
  #
410
- # @return [undefined]
410
+ # @return [void]
411
411
  def destroy_children
412
412
  children.destroy_all
413
413
  end
@@ -419,7 +419,7 @@ module Mongoid
419
419
  # rearrangement when the parent_ids changed
420
420
  #
421
421
  # @private
422
- # @return [undefined]
422
+ # @return [void]
423
423
  def rearrange
424
424
  if self.parent_id
425
425
  self.parent_ids = parent.parent_ids + [self.parent_id]
@@ -113,7 +113,7 @@ module Mongoid
113
113
  ##
114
114
  # Move this node above all its siblings
115
115
  #
116
- # @return [undefined]
116
+ # @return [void]
117
117
  def move_to_top
118
118
  return true if at_top?
119
119
  move_above(first_sibling_in_list)
@@ -122,7 +122,7 @@ module Mongoid
122
122
  ##
123
123
  # Move this node below all its siblings
124
124
  #
125
- # @return [undefined]
125
+ # @return [void]
126
126
  def move_to_bottom
127
127
  return true if at_bottom?
128
128
  move_below(last_sibling_in_list)
@@ -131,7 +131,7 @@ module Mongoid
131
131
  ##
132
132
  # Move this node one position up
133
133
  #
134
- # @return [undefined]
134
+ # @return [void]
135
135
  def move_up
136
136
  switch_with_sibling_at_offset(-1) unless at_top?
137
137
  end
@@ -139,7 +139,7 @@ module Mongoid
139
139
  ##
140
140
  # Move this node one position down
141
141
  #
142
- # @return [undefined]
142
+ # @return [void]
143
143
  def move_down
144
144
  switch_with_sibling_at_offset(1) unless at_bottom?
145
145
  end
@@ -151,7 +151,7 @@ module Mongoid
151
151
  #
152
152
  # @param [Mongoid::Tree] other document to move this document above
153
153
  #
154
- # @return [undefined]
154
+ # @return [void]
155
155
  def move_above(other)
156
156
  ensure_to_be_sibling_of(other)
157
157
 
@@ -175,7 +175,7 @@ module Mongoid
175
175
  #
176
176
  # @param [Mongoid::Tree] other document to move this document below
177
177
  #
178
- # @return [undefined]
178
+ # @return [void]
179
179
  def move_below(other)
180
180
  ensure_to_be_sibling_of(other)
181
181
 
@@ -9,6 +9,31 @@ module Mongoid
9
9
  ##
10
10
  # = Mongoid::Tree::RationalNumbering
11
11
  #
12
+ # Provides rational number sorting on your tree structure.
13
+ # This makes it simple to query for a tree given a node in a single query.
14
+ # Given this tree
15
+ # Node 1
16
+ # Node 1-1
17
+ # Node 1-2
18
+ # Node 1-2-1
19
+ # Node 1-2-2
20
+ # Node 1-2-2-1
21
+ # Node 1-2-2-2
22
+ # Node 1-2-3
23
+ # Node 1-3
24
+ # Node 2
25
+ # Node 2-1
26
+ # Node 2-1-1
27
+ # Node 2-1-2
28
+ # Node 2-2
29
+ # Node 3
30
+ # Node 4
31
+ #
32
+ # The entire tree can be queried like this:
33
+ # node_2 = Node.where(title: "Node 2").first
34
+ # node_1.tree returns:
35
+ # ["Node 2", "Node 2-1", "Node 2-1-1", "Node 2-1-2", "Node 2-2"]
36
+ #
12
37
  # Mongoid::Tree doesn't use rational numbers by default. To enable rational numbering
13
38
  # of children include both Mongoid::Tree and Mongoid::Tree::RationalNumbering into
14
39
  # your document.
@@ -61,7 +86,7 @@ module Mongoid
61
86
  ##
62
87
  # Set options for rational numbers
63
88
  #
64
- # @param [Hash] options a hash
89
+ # @param opts [Hash] a hash of options
65
90
  #
66
91
  # :auto_tree_timestamping (true/false)
67
92
  # Per default timestamps are only updated on the a node that is changed, and not siblings that are moved/shifted
@@ -110,7 +135,7 @@ module Mongoid
110
135
  ##
111
136
  # Initialize the rational tree document
112
137
  #
113
- # @return [undefined]
138
+ # @return [void]
114
139
  #
115
140
  def initialize(*args)
116
141
  @_forced_rational_number = false
@@ -141,10 +166,10 @@ module Mongoid
141
166
  # if a document exists on the new position, all siblings are shifted right before moving this document
142
167
  # can move without updating conflicting siblings by using :force in options
143
168
  #
144
- # @param [Integer] The positional value
145
- # @param [Hash] Options: :force (defaults to false)
169
+ # @param _position [Integer] The positional value
170
+ # @param opts [Hash] :force (defaults to false)
146
171
  #
147
- # @return [undefined]
172
+ # @return [void]
148
173
  #
149
174
  def move_to_position(_position, opts = {})
150
175
  new_rational_number = parent_rational_number.child_from_position(_position)
@@ -158,11 +183,11 @@ module Mongoid
158
183
  # if a document exists on the new position, all siblings are shifted right before moving this document
159
184
  # can move without updating conflicting siblings by using :ignore_conflicts in options
160
185
  #
161
- # @param [Integer] The nominator value
162
- # @param [Integer] The denominator value
163
- # @param [Hash] Options: :force (defaults to false)
186
+ # @param nv [Integer] The nominator value
187
+ # @param dv [Integer] The denominator value
188
+ # @param opts [Hash] Options: :force (defaults to false)
164
189
  #
165
- # @return [undefined]
190
+ # @return [void]
166
191
  #
167
192
  def move_to_rational_number(nv, dv, opts = {})
168
193
  # don't check for conflict if forced move
@@ -186,6 +211,12 @@ module Mongoid
186
211
  # If the given nv/dv is higher than the last sibling under the parent, the nv/dv will be recalculated
187
212
  # to appropriate nv/dv values
188
213
  #
214
+ # @param nv [Integer] The nominator value
215
+ # @param dv [Integer] The denominator value
216
+ # @param do_save [Boolean] true/false if the model should be saved or just updated
217
+ #
218
+ # @return [Boolean] returns the save value or true if do_save is set to false
219
+ #
189
220
  def set_rational_number(nv,dv, do_save = true)
190
221
  # return true of already at the right spot
191
222
  # puts "#{self.name} - set_rational_number #{nv}/#{dv} self:#{self.rational_number_nv}/#{self.rational_number_dv}"
@@ -250,6 +281,11 @@ module Mongoid
250
281
  # Will return true if the parent is "root" and the node should be created
251
282
  # as a root element
252
283
  #
284
+ # @param nv [Integer] The nominator value
285
+ # @param dv [Integer] The denominator value
286
+ #
287
+ # @return [Boolean] returns true if the parent exists
288
+ #
253
289
  def parent_exists?(nv,dv)
254
290
  q_parent = base_class.where(:rational_number_nv => nv).where(:rational_number_dv => dv).excludes(:id => self.id).first
255
291
  if q_parent.nil?
@@ -264,8 +300,10 @@ module Mongoid
264
300
  #
265
301
  # Move conflicting nodes for a given value
266
302
  #
267
- # @param [Integer] The nominator value
268
- # @param [Integer] The denominator value
303
+ # @param nv [Integer] The nominator value
304
+ # @param dv [Integer] The denominator value
305
+ #
306
+ # @return [void]
269
307
  #
270
308
  def move_conflicting_nodes(nv,dv)
271
309
  # As we are moving to the position of the conflicting sibling, it all items can be shifted similar to "move_above"
@@ -301,6 +339,9 @@ module Mongoid
301
339
  #
302
340
  # Verifies parent keys from calculation and query
303
341
  #
342
+ # @param nv [Integer] The nominator value
343
+ # @param dv [Integer] The denominator value
344
+ #
304
345
  # @return [Boolean] true for correct, else false
305
346
  #
306
347
  def correct_rational_parent?(nv, dv)
@@ -328,7 +369,7 @@ module Mongoid
328
369
  #
329
370
  # Rekey each of the children (usually forcefully if a tree has gone "crazy")
330
371
  #
331
- # @return [undefined]
372
+ # @return [void]
332
373
  #
333
374
  def rekey_children
334
375
  _pos = 1
@@ -340,10 +381,22 @@ module Mongoid
340
381
  end
341
382
  end
342
383
 
384
+ ##
385
+ #
386
+ # Should the former siblings be rekeyed?
387
+ #
388
+ # @return [Boolean] true if needed, else false
389
+ #
343
390
  def rekey_former_siblings?
344
391
  persisted? && self.previous_changes.include?("parent_id")
345
392
  end
346
393
 
394
+ ##
395
+ #
396
+ # Rekey former siblings after a move
397
+ #
398
+ # @return [void]
399
+ #
347
400
  def rekey_former_siblings
348
401
  former_siblings = base_class.where(:parent_id => attribute_was('parent_id')).
349
402
  and(:rational_number_value.gt => (attribute_was('rational_number_value') || 0)).
@@ -354,6 +407,12 @@ module Mongoid
354
407
  end
355
408
  end
356
409
 
410
+ ##
411
+ #
412
+ # Move a node to given rational number and save/update the node
413
+ #
414
+ # @return [void]
415
+ #
357
416
  def move_node_and_save_if_changed(node, new_rational_number)
358
417
  if new_rational_number != node.rational_number
359
418
  node.move_to_rational_number(new_rational_number.nv, new_rational_number.dv, {:force => true})
@@ -374,8 +433,9 @@ module Mongoid
374
433
  ##
375
434
  # Convert from rational number and set keys accordingly
376
435
  #
377
- # @param [RationalNumber] The rational number for this node
378
- # @return [undefined]
436
+ # @param rational_number [RationalNumber] The rational number for this node
437
+ #
438
+ # @return [void]
379
439
  #
380
440
  def from_rational_number(rational_number)
381
441
  self.rational_number_nv = rational_number.nv
@@ -389,14 +449,16 @@ module Mongoid
389
449
  # Returns a chainable criteria for this document's ancestors
390
450
  #
391
451
  # @return [Mongoid::Criteria] Mongoid criteria to retrieve the document's ancestors
452
+ #
392
453
  def ancestors
393
454
  base_class.unscoped { super }
394
455
  end
395
456
 
396
457
  ##
397
- #
398
458
  # Returns the positional value for the current node
399
459
  #
460
+ # @return [integer] The positional value calculated from the rational number
461
+ #
400
462
  def position
401
463
  self.rational_number.position
402
464
  end
@@ -406,6 +468,7 @@ module Mongoid
406
468
  # Siblings with a position greater than this document's position.
407
469
  #
408
470
  # @return [Mongoid::Criteria] Mongoid criteria to retrieve the document's lower siblings
471
+ #
409
472
  def lower_siblings
410
473
  self.siblings.where(:rational_number_value.gt => self.rational_number_value)
411
474
  end
@@ -415,6 +478,7 @@ module Mongoid
415
478
  # Siblings with a position lower than this document's position.
416
479
  #
417
480
  # @return [Mongoid::Criteria] Mongoid criteria to retrieve the document's higher siblings
481
+ #
418
482
  def higher_siblings
419
483
  self.siblings.where(:rational_number_value.lt => self.rational_number_value)
420
484
  end
@@ -423,7 +487,10 @@ module Mongoid
423
487
  # Returns siblings between the current document and the other document
424
488
  # Siblings with a position between this document's position and the other document's position.
425
489
  #
490
+ # @param other [Mongoid:Document] The other mongoid document
491
+ #
426
492
  # @return [Mongoid::Criteria] Mongoid criteria to retrieve the documents between this and the other document
493
+ #
427
494
  def siblings_between(other)
428
495
  range = [self.rational_number_value, other.rational_number_value].sort
429
496
  self.siblings.where(:rational_number_value.gt => range.first, :rational_number_value.lt => range.last)
@@ -432,6 +499,10 @@ module Mongoid
432
499
  ##
433
500
  # Return the siblings between this and other + other
434
501
  #
502
+ # @param other [Mongoid:Document] The other mongoid document
503
+ #
504
+ # @return [Mongoid::Criteria] Mongoid criteria to retrieve the documents between this and the other document
505
+ #
435
506
  def siblings_between_including_other(other)
436
507
  range = [self.rational_number_value, other.rational_number_value].sort
437
508
  self.siblings.where(:rational_number_value.gte => range.first, :rational_number_value.lte => range.last)
@@ -441,6 +512,7 @@ module Mongoid
441
512
  # Returns the lowest sibling (could be self)
442
513
  #
443
514
  # @return [Mongoid::Document] The lowest sibling
515
+ #
444
516
  def last_sibling_in_list
445
517
  siblings_and_self.last
446
518
  end
@@ -449,6 +521,7 @@ module Mongoid
449
521
  # Returns the highest sibling (could be self)
450
522
  #
451
523
  # @return [Mongoid::Document] The highest sibling
524
+ #
452
525
  def first_sibling_in_list
453
526
  siblings_and_self.first
454
527
  end
@@ -457,6 +530,7 @@ module Mongoid
457
530
  # Is this the highest sibling?
458
531
  #
459
532
  # @return [Boolean] Whether the document is the highest sibling
533
+ #
460
534
  def at_top?
461
535
  higher_siblings.empty?
462
536
  end
@@ -465,6 +539,7 @@ module Mongoid
465
539
  # Is this the lowest sibling?
466
540
  #
467
541
  # @return [Boolean] Whether the document is the lowest sibling
542
+ #
468
543
  def at_bottom?
469
544
  lower_siblings.empty?
470
545
  end
@@ -472,7 +547,8 @@ module Mongoid
472
547
  ##
473
548
  # Move this node above all its siblings
474
549
  #
475
- # @return [undefined]
550
+ # @return [void]
551
+ #
476
552
  def move_to_top
477
553
  return true if at_top?
478
554
  move_above(first_sibling_in_list)
@@ -481,7 +557,8 @@ module Mongoid
481
557
  ##
482
558
  # Move this node below all its siblings
483
559
  #
484
- # @return [undefined]
560
+ # @return [void]
561
+ #
485
562
  def move_to_bottom
486
563
  return true if at_bottom?
487
564
  move_below(last_sibling_in_list)
@@ -490,7 +567,8 @@ module Mongoid
490
567
  ##
491
568
  # Move this node one position up
492
569
  #
493
- # @return [undefined]
570
+ # @return [void]
571
+ #
494
572
  def move_up
495
573
  unless at_top?
496
574
  prev_sibling = higher_siblings.last
@@ -501,7 +579,8 @@ module Mongoid
501
579
  ##
502
580
  # Move this node one position down
503
581
  #
504
- # @return [undefined]
582
+ # @return [void]
583
+ #
505
584
  def move_down
506
585
  unless at_bottom?
507
586
  next_sibling = lower_siblings.first
@@ -512,10 +591,11 @@ module Mongoid
512
591
  ##
513
592
  # Shift nodes between self and other (or including other) in one or the other direction
514
593
  #
515
- # @param [Mongoid::Tree] other document to move this document above
516
- # @param [Integer] +1 / -1 for the direction to shift nodes
517
- # @param [Boolean] exclude the other object in the shift or not.
594
+ # @param other [Mongoid::Document] Other document to move this document above
595
+ # @param direction [Integer] +1 / -1 for the direction to shift nodes
596
+ # @param exclude_other [Boolean] exclude the other object in the shift or not.
518
597
  #
598
+ # @return [void]
519
599
  #
520
600
  def shift_nodes_position(other, direction, exclude_other = false)
521
601
  without_timestamping do
@@ -529,6 +609,14 @@ module Mongoid
529
609
  end
530
610
  end
531
611
 
612
+ ##
613
+ # Shift nodes between self and other (or including other) in one or the other direction
614
+ #
615
+ # @param other [Mongoid::Document] Other document to move this document above
616
+ # @param direction [Integer] +1 / -1 for the direction to shift nodes
617
+ #
618
+ # @return [void]
619
+ #
532
620
  def shift_lower_nodes_from_other(other, direction)
533
621
  # puts "#{self.name} shift_lower_nodes_from_other other: #{other.name} direction: #{direction} other.siblings_and_self.count: #{other.siblings_and_self.count}"
534
622
  range = [other.rational_number_value, other.siblings_and_self.last.rational_number_value].sort
@@ -536,6 +624,14 @@ module Mongoid
536
624
  shift_nodes(nodes_to_shift, direction)
537
625
  end
538
626
 
627
+ ##
628
+ # Shift nodes in a direction
629
+ #
630
+ # @param nodes_to_shift [Array] Array of documents to shift in a given direction
631
+ # @param direction [Integer] +1 / -1 for the direction to shift nodes
632
+ #
633
+ # @return [void]
634
+ #
539
635
  def shift_nodes(nodes_to_shift, direction)
540
636
  # puts "#{self.name} shift_nodes direction: #{direction}"
541
637
  without_timestamping do
@@ -553,9 +649,10 @@ module Mongoid
553
649
  #
554
650
  # This method changes the node's parent if nescessary.
555
651
  #
556
- # @param [Mongoid::Tree] other document to move this document above
652
+ # @param other [Mongoid::Document] document to move this document above
653
+ #
654
+ # @return [void]
557
655
  #
558
- # @return [undefined]
559
656
  def move_above(other)
560
657
  ensure_to_be_sibling_of(other)
561
658
  return if other.position == self.position + 1
@@ -576,9 +673,10 @@ module Mongoid
576
673
  #
577
674
  # This method changes the node's parent if nescessary.
578
675
  #
579
- # @param [Mongoid::Tree] other document to move this document below
676
+ # @param other [Mongoid::Document] document to move this document above
677
+ #
678
+ # @return [void]
580
679
  #
581
- # @return [undefined]
582
680
  def move_below(other)
583
681
  ensure_to_be_sibling_of(other)
584
682
  return if other.position + 1 == self.position
@@ -599,8 +697,10 @@ module Mongoid
599
697
 
600
698
  ##
601
699
  # Call block without triggeringtimestamps
602
- # @param [&block] code block to call
603
-
700
+ # @param block code block to call
701
+ #
702
+ # @return [void]
703
+ #
604
704
  def without_timestamping(&block)
605
705
  # # puts "without_timestamping: Automagic timpestamping enabled? #{self.class.auto_tree_timestamping}"
606
706
  disable_timestamp_callback() if self.class.auto_tree_timestamping
@@ -612,7 +712,7 @@ module Mongoid
612
712
  # Disable the timestamps for the document type, and increase the disable count
613
713
  # Will only disable once, even if called multiple times
614
714
  #
615
- # @return [undefined]
715
+ # @return [void]
616
716
  def disable_timestamp_callback
617
717
  # # puts "Disabling timestamp callback count: #{@@_disable_timestamp_count}"
618
718
  if self.respond_to?("updated_at")
@@ -625,7 +725,7 @@ module Mongoid
625
725
  # Enable the timestamps for the document type, and decrease the disable count
626
726
  # Will only enable once, even if called multiple times
627
727
  #
628
- # @return [undefined]
728
+ # @return [void]
629
729
  def enable_timestamp_callback
630
730
  # # puts "Enabling timestamp callback count: #{@@_disable_timestamp_count}"
631
731
  if self.respond_to?("updated_at")
@@ -646,7 +746,7 @@ module Mongoid
646
746
  # If there are both changes to nv/dv and parent_id, nv/dv settings takes
647
747
  # precedence over parent_id changes
648
748
  #
649
- # @return [undefined]
749
+ # @return [void]
650
750
  #
651
751
  def update_rational_number
652
752
  if self.rational_number_nv_changed? && self.rational_number_dv_changed? && !self.rational_number_value.nil? && !set_initial_rational_number?
@@ -745,9 +845,9 @@ module Mongoid
745
845
  #
746
846
  # Switch location with a given sibling
747
847
  #
748
- # @param [Mongoid::Tree] other document to switch places with
848
+ # @param sibling [Mongoid:Document] The other sibling document to switch place with
749
849
  #
750
- # @return [undefined]
850
+ # @return [void]
751
851
  #
752
852
  def switch_with_sibling(sibling)
753
853
  self_pos = self.position
@@ -764,9 +864,9 @@ module Mongoid
764
864
  #
765
865
  # Ensure this is a sibling of given other, if not, move it to the same parent
766
866
  #
767
- # @param [Mongoid::Tree] other document to ensure sibling relation
867
+ # @param other [Mongoid:Document] The other mongoid document to ensure sibling relation
768
868
  #
769
- # @return [undefined]
869
+ # @return [void]
770
870
  #
771
871
  def ensure_to_be_sibling_of(other)
772
872
  return if sibling_of?(other)
@@ -774,6 +874,15 @@ module Mongoid
774
874
  save!
775
875
  end
776
876
 
877
+ ##
878
+ #
879
+ # Make sure the correct parent is set, if not, update the parent accordingly
880
+ #
881
+ # @param nv [Integer] The nominator value
882
+ # @param dv [Integer] The denominator value
883
+ #
884
+ # @return [void]
885
+ #
777
886
  def ensure_to_have_correct_parent(nv,dv)
778
887
  # puts "#{self.name} ensure_to_have_correct_parent #{nv}/#{dv}"
779
888
  new_rational_number = RationalNumber.new(nv,dv)
@@ -798,7 +907,10 @@ module Mongoid
798
907
  self.parent = new_parent
799
908
  end
800
909
 
910
+ ##
911
+ #
801
912
  # Shifting/rekeying of lower siblings on destroy
913
+ #
802
914
  def move_lower_siblings
803
915
  without_timestamping do
804
916
  lower_siblings.each do |sibling|
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module RationalTree
3
+ VERSION = '2.0.1'
4
+ end
5
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-tree-rational
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leif Ringstad
@@ -32,214 +32,83 @@ dependencies:
32
32
  - !ruby/object:Gem::Version
33
33
  version: '4.0'
34
34
  - !ruby/object:Gem::Dependency
35
- name: rational_number
35
+ name: i18n
36
36
  requirement: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '0.6'
41
41
  type: :runtime
42
42
  prerelease: false
43
43
  version_requirements: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
48
- - !ruby/object:Gem::Dependency
49
- name: rubysl-rake
50
- requirement: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '2.0'
55
- type: :runtime
56
- prerelease: false
57
- version_requirements: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '2.0'
47
+ version: '0.6'
62
48
  - !ruby/object:Gem::Dependency
63
49
  name: rake
64
50
  requirement: !ruby/object:Gem::Requirement
65
51
  requirements:
66
52
  - - ">="
67
53
  - !ruby/object:Gem::Version
68
- version: '0'
54
+ version: 0.9.2
69
55
  type: :development
70
56
  prerelease: false
71
57
  version_requirements: !ruby/object:Gem::Requirement
72
58
  requirements:
73
59
  - - ">="
74
60
  - !ruby/object:Gem::Version
75
- version: '0'
61
+ version: 0.9.2
76
62
  - !ruby/object:Gem::Dependency
77
63
  name: rspec
78
64
  requirement: !ruby/object:Gem::Requirement
79
65
  requirements:
80
- - - ">="
66
+ - - "~>"
81
67
  - !ruby/object:Gem::Version
82
- version: '0'
68
+ version: '3.0'
83
69
  type: :development
84
70
  prerelease: false
85
71
  version_requirements: !ruby/object:Gem::Requirement
86
72
  requirements:
87
- - - ">="
73
+ - - "~>"
88
74
  - !ruby/object:Gem::Version
89
- version: '0'
75
+ version: '3.0'
90
76
  - !ruby/object:Gem::Dependency
91
77
  name: yard
92
78
  requirement: !ruby/object:Gem::Requirement
93
79
  requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
- type: :development
98
- prerelease: false
99
- version_requirements: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- - !ruby/object:Gem::Dependency
105
- name: jeweler
106
- requirement: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
- type: :development
112
- prerelease: false
113
- version_requirements: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - ">="
116
- - !ruby/object:Gem::Version
117
- version: '0'
118
- - !ruby/object:Gem::Dependency
119
- name: guard-rspec
120
- requirement: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - ">="
123
- - !ruby/object:Gem::Version
124
- version: 2.6.0
125
- type: :development
126
- prerelease: false
127
- version_requirements: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - ">="
130
- - !ruby/object:Gem::Version
131
- version: 2.6.0
132
- - !ruby/object:Gem::Dependency
133
- name: rb-inotify
134
- requirement: !ruby/object:Gem::Requirement
135
- requirements:
136
- - - ">="
137
- - !ruby/object:Gem::Version
138
- version: '0'
139
- type: :development
140
- prerelease: false
141
- version_requirements: !ruby/object:Gem::Requirement
142
- requirements:
143
- - - ">="
144
- - !ruby/object:Gem::Version
145
- version: '0'
146
- - !ruby/object:Gem::Dependency
147
- name: rb-fsevent
148
- requirement: !ruby/object:Gem::Requirement
149
- requirements:
150
- - - ">="
151
- - !ruby/object:Gem::Version
152
- version: '0'
153
- type: :development
154
- prerelease: false
155
- version_requirements: !ruby/object:Gem::Requirement
156
- requirements:
157
- - - ">="
158
- - !ruby/object:Gem::Version
159
- version: '0'
160
- - !ruby/object:Gem::Dependency
161
- name: wdm
162
- requirement: !ruby/object:Gem::Requirement
163
- requirements:
164
- - - ">="
165
- - !ruby/object:Gem::Version
166
- version: '0'
167
- type: :development
168
- prerelease: false
169
- version_requirements: !ruby/object:Gem::Requirement
170
- requirements:
171
- - - ">="
172
- - !ruby/object:Gem::Version
173
- version: '0'
174
- - !ruby/object:Gem::Dependency
175
- name: awesome_print
176
- requirement: !ruby/object:Gem::Requirement
177
- requirements:
178
- - - ">="
80
+ - - "~>"
179
81
  - !ruby/object:Gem::Version
180
- version: '0'
82
+ version: '0.8'
181
83
  type: :development
182
84
  prerelease: false
183
85
  version_requirements: !ruby/object:Gem::Requirement
184
86
  requirements:
185
- - - ">="
87
+ - - "~>"
186
88
  - !ruby/object:Gem::Version
187
- version: '0'
89
+ version: '0.8'
188
90
  - !ruby/object:Gem::Dependency
189
91
  name: timecop
190
92
  requirement: !ruby/object:Gem::Requirement
191
93
  requirements:
192
94
  - - ">="
193
95
  - !ruby/object:Gem::Version
194
- version: '0'
195
- type: :development
196
- prerelease: false
197
- version_requirements: !ruby/object:Gem::Requirement
198
- requirements:
199
- - - ">="
200
- - !ruby/object:Gem::Version
201
- version: '0'
202
- - !ruby/object:Gem::Dependency
203
- name: coveralls
204
- requirement: !ruby/object:Gem::Requirement
205
- requirements:
206
- - - ">="
207
- - !ruby/object:Gem::Version
208
- version: '0'
96
+ version: '0.7'
209
97
  type: :development
210
98
  prerelease: false
211
99
  version_requirements: !ruby/object:Gem::Requirement
212
100
  requirements:
213
101
  - - ">="
214
102
  - !ruby/object:Gem::Version
215
- version: '0'
216
- - !ruby/object:Gem::Dependency
217
- name: simplecov
218
- requirement: !ruby/object:Gem::Requirement
219
- requirements:
220
- - - ">="
221
- - !ruby/object:Gem::Version
222
- version: '0'
223
- type: :development
224
- prerelease: false
225
- version_requirements: !ruby/object:Gem::Requirement
226
- requirements:
227
- - - ">="
228
- - !ruby/object:Gem::Version
229
- version: '0'
103
+ version: '0.7'
230
104
  description: A tree structure for Mongoid documents using the materialized path pattern
231
105
  and rational number sorting.
232
106
  email: leifcr@gmail.com
233
107
  executables: []
234
108
  extensions: []
235
- extra_rdoc_files:
236
- - LICENSE
237
- - README.md
109
+ extra_rdoc_files: []
238
110
  files:
239
- - ".rspec"
240
- - ".travis.yml"
241
111
  - Gemfile
242
- - Guardfile
243
112
  - LICENSE
244
113
  - README.md
245
114
  - Rakefile
@@ -250,7 +119,7 @@ files:
250
119
  - lib/mongoid/tree/ordering.rb
251
120
  - lib/mongoid/tree/rational_numbering.rb
252
121
  - lib/mongoid/tree/traversal.rb
253
- - mongoid-tree-rational.gemspec
122
+ - lib/mongoid/tree/version.rb
254
123
  - spec/mongoid/tree/ordering_spec.rb
255
124
  - spec/mongoid/tree/rational_numbering_spec.rb
256
125
  - spec/mongoid/tree/traversal_spec.rb
@@ -282,4 +151,12 @@ rubygems_version: 2.2.2
282
151
  signing_key:
283
152
  specification_version: 4
284
153
  summary: A tree structure for Mongoid documents with rational numbers
285
- test_files: []
154
+ test_files:
155
+ - spec/mongoid/tree/rational_numbering_spec.rb
156
+ - spec/mongoid/tree/traversal_spec.rb
157
+ - spec/mongoid/tree/ordering_spec.rb
158
+ - spec/mongoid/tree_spec.rb
159
+ - spec/spec_helper.rb
160
+ - spec/support/models/node.rb
161
+ - spec/support/macros/tree_macros.rb
162
+ has_rdoc:
data/.rspec DELETED
@@ -1,2 +0,0 @@
1
- --color
2
- --format documentation
@@ -1,10 +0,0 @@
1
- before_install:
2
- - gem update --system
3
- - gem --version
4
- language: ruby
5
- rvm:
6
- - 2.0.0
7
- - 2.1.2
8
- - rbx-2
9
- services:
10
- - mongodb
data/Guardfile DELETED
@@ -1,6 +0,0 @@
1
- guard 'rspec' do
2
- watch(%r{^spec/.+_spec\.rb$})
3
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
4
- watch(%r{^spec/support/.+\.rb$}) { "spec" }
5
- watch('spec/spec_helper.rb') { "spec" }
6
- end
@@ -1,104 +0,0 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
- # -*- encoding: utf-8 -*-
5
- # stub: mongoid-tree-rational 2.0.0 ruby lib
6
-
7
- Gem::Specification.new do |s|
8
- s.name = "mongoid-tree-rational"
9
- s.version = "2.0.0"
10
-
11
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
- s.require_paths = ["lib"]
13
- s.authors = ["Leif Ringstad", "Benedikt Deicke"]
14
- s.date = "2014-08-20"
15
- s.description = "A tree structure for Mongoid documents using the materialized path pattern and rational number sorting."
16
- s.email = "leifcr@gmail.com"
17
- s.extra_rdoc_files = [
18
- "LICENSE",
19
- "README.md"
20
- ]
21
- s.files = [
22
- ".rspec",
23
- ".travis.yml",
24
- "Gemfile",
25
- "Guardfile",
26
- "LICENSE",
27
- "README.md",
28
- "Rakefile",
29
- "VERSION",
30
- "lib/mongoid/locale/en.yml",
31
- "lib/mongoid/locale/nb.yml",
32
- "lib/mongoid/tree.rb",
33
- "lib/mongoid/tree/ordering.rb",
34
- "lib/mongoid/tree/rational_numbering.rb",
35
- "lib/mongoid/tree/traversal.rb",
36
- "mongoid-tree-rational.gemspec",
37
- "spec/mongoid/tree/ordering_spec.rb",
38
- "spec/mongoid/tree/rational_numbering_spec.rb",
39
- "spec/mongoid/tree/traversal_spec.rb",
40
- "spec/mongoid/tree_spec.rb",
41
- "spec/spec_helper.rb",
42
- "spec/support/macros/tree_macros.rb",
43
- "spec/support/models/node.rb"
44
- ]
45
- s.homepage = "https://github.com/boxcms/mongoid-tree-rational"
46
- s.licenses = ["MIT"]
47
- s.rubygems_version = "2.2.2"
48
- s.summary = "A tree structure for Mongoid documents with rational numbers"
49
-
50
- if s.respond_to? :specification_version then
51
- s.specification_version = 4
52
-
53
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
54
- s.add_runtime_dependency(%q<mongoid>, ["<= 5.0", ">= 4.0"])
55
- s.add_runtime_dependency(%q<rational_number>, [">= 0"])
56
- s.add_runtime_dependency(%q<rubysl-rake>, ["~> 2.0"])
57
- s.add_development_dependency(%q<rake>, [">= 0"])
58
- s.add_development_dependency(%q<rspec>, [">= 0"])
59
- s.add_development_dependency(%q<yard>, [">= 0"])
60
- s.add_development_dependency(%q<jeweler>, [">= 0"])
61
- s.add_development_dependency(%q<guard-rspec>, [">= 2.6.0"])
62
- s.add_development_dependency(%q<rb-inotify>, [">= 0"])
63
- s.add_development_dependency(%q<rb-fsevent>, [">= 0"])
64
- s.add_development_dependency(%q<wdm>, [">= 0"])
65
- s.add_development_dependency(%q<awesome_print>, [">= 0"])
66
- s.add_development_dependency(%q<timecop>, [">= 0"])
67
- s.add_development_dependency(%q<coveralls>, [">= 0"])
68
- s.add_development_dependency(%q<simplecov>, [">= 0"])
69
- else
70
- s.add_dependency(%q<mongoid>, ["<= 5.0", ">= 4.0"])
71
- s.add_dependency(%q<rational_number>, [">= 0"])
72
- s.add_dependency(%q<rubysl-rake>, ["~> 2.0"])
73
- s.add_dependency(%q<rake>, [">= 0"])
74
- s.add_dependency(%q<rspec>, [">= 0"])
75
- s.add_dependency(%q<yard>, [">= 0"])
76
- s.add_dependency(%q<jeweler>, [">= 0"])
77
- s.add_dependency(%q<guard-rspec>, [">= 2.6.0"])
78
- s.add_dependency(%q<rb-inotify>, [">= 0"])
79
- s.add_dependency(%q<rb-fsevent>, [">= 0"])
80
- s.add_dependency(%q<wdm>, [">= 0"])
81
- s.add_dependency(%q<awesome_print>, [">= 0"])
82
- s.add_dependency(%q<timecop>, [">= 0"])
83
- s.add_dependency(%q<coveralls>, [">= 0"])
84
- s.add_dependency(%q<simplecov>, [">= 0"])
85
- end
86
- else
87
- s.add_dependency(%q<mongoid>, ["<= 5.0", ">= 4.0"])
88
- s.add_dependency(%q<rational_number>, [">= 0"])
89
- s.add_dependency(%q<rubysl-rake>, ["~> 2.0"])
90
- s.add_dependency(%q<rake>, [">= 0"])
91
- s.add_dependency(%q<rspec>, [">= 0"])
92
- s.add_dependency(%q<yard>, [">= 0"])
93
- s.add_dependency(%q<jeweler>, [">= 0"])
94
- s.add_dependency(%q<guard-rspec>, [">= 2.6.0"])
95
- s.add_dependency(%q<rb-inotify>, [">= 0"])
96
- s.add_dependency(%q<rb-fsevent>, [">= 0"])
97
- s.add_dependency(%q<wdm>, [">= 0"])
98
- s.add_dependency(%q<awesome_print>, [">= 0"])
99
- s.add_dependency(%q<timecop>, [">= 0"])
100
- s.add_dependency(%q<coveralls>, [">= 0"])
101
- s.add_dependency(%q<simplecov>, [">= 0"])
102
- end
103
- end
104
-