File: iterator.rb

package info (click to toggle)
ruby-awesome-nested-set 3.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 172 kB
  • sloc: ruby: 1,044; makefile: 2
file content (29 lines) | stat: -rw-r--r-- 730 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
module CollectiveIdea #:nodoc:
  module Acts #:nodoc:
    module NestedSet #:nodoc:
      class Iterator
        attr_reader :objects

        def initialize(objects)
          @objects = objects
        end

        def each_with_level
          path = [nil]
          objects.each do |o|
            if o.parent_id != path.last
              # we are on a new level, did we descend or ascend?
              if path.include?(o.parent_id)
                # remove wrong tailing paths elements
                path.pop while path.last != o.parent_id
              else
                path << o.parent_id
              end
            end
            yield(o, path.length - 1)
          end
        end
      end
    end
  end
end