File: trackable.rb

package info (click to toggle)
ruby-gitlab-experiment 0.9.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 260 kB
  • sloc: ruby: 1,202; makefile: 7
file content (69 lines) | stat: -rw-r--r-- 1,489 bytes parent folder | download | duplicates (2)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# frozen_string_literal: true

module Gitlab
  class Experiment
    module TestBehaviors
      module Trackable
        private

        def manage_nested_stack
          TrackedStructure.push(self)
          super
        ensure
          TrackedStructure.pop
        end
      end

      class TrackedStructure
        include Singleton

        # dependency tracking
        @flat = {}
        @stack = []

        # structure tracking
        @tree = { name: nil, count: 0, children: {} }
        @node = @tree

        class << self
          def reset!
            # dependency tracking
            @flat = {}
            @stack = []

            # structure tracking
            @tree = { name: nil, count: 0, children: {} }
            @node = @tree
          end

          def hierarchy
            @tree[:children]
          end

          def dependencies
            @flat
          end

          def push(instance)
            # dependency tracking
            @flat[instance.name] = ((@flat[instance.name] || []) + @stack.map(&:name)).uniq
            @stack.push(instance)

            # structure tracking
            @last = @node
            @node = @node[:children][instance.name] ||= { name: instance.name, count: 0, children: {} }
            @node[:count] += 1
          end

          def pop
            # dependency tracking
            @stack.pop

            # structure tracking
            @node = @last
          end
        end
      end
    end
  end
end