Module: Reflekt

Defined in:
lib/Reflekt.rb

Defined Under Namespace

Modules: SingletonClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure {|@@reflekt.config| ... } ⇒ Object

Provide Config instance to block.

Yields:

  • (@@reflekt.config)


157
158
159
# File 'lib/Reflekt.rb', line 157

def self.configure
  yield(@@reflekt.config)
end

Instance Method Details

#initialize(*args) ⇒ Object



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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/Reflekt.rb', line 35

def initialize(*args)

  # TODO: Store counts on @@reflekt and key by instance ID.
  @reflekt_counts = {}

  # Get child and parent instance methods.
  parent_instance_methods = self.class.superclass.instance_methods(false)
  child_instance_methods = self.class.instance_methods(false)
  instance_methods = parent_instance_methods + child_instance_methods

  # TODO: Include core methods like "Array.include?".
  instance_methods.each do |method|

    @reflekt_counts[method] = 0

    # When method called in flow.
    self.define_singleton_method(method) do |*args|

      # When Reflekt enabled and control reflection has executed without error.
      if @@reflekt.config.enabled && !@@reflekt.error

        # Get current execution.
        execution = @@reflekt.stack.peek()

        # Don't reflect when reflect limit reached or method skipped.
        unless (@reflekt_counts[method] >= @@reflekt.config.reflect_limit) || self.class.reflekt_skipped?(method)

          # When stack empty or past execution done reflecting.
          if execution.nil? || execution.has_finished_reflecting?

            # Create execution.
            execution = Execution.new(self, method, @@reflekt.config.reflect_amount, @@reflekt.stack)

            @@reflekt.stack.push(execution)

          end

          ##
          # Reflect the execution.
          #
          # The first method call in the execution creates a reflection.
          # Then method calls are shadow executions which return to the reflection.
          ##
          if execution.has_empty_reflections? && !execution.is_reflecting?
            execution.is_reflecting = true

            # Create control.
            control = Control.new(execution, 0, @@reflekt.aggregator)
            execution.control = control

            # Execute control.
            control.reflect(*args)

            # Stop reflecting when control fails to execute.
            if control.status == :error
              @@reflekt.error = true
            # Continue reflecting when control executes succesfully.
            else

              # Save control as reflection.
              @@reflekt.db.get("reflections").push(control.serialize())

              # Multiple reflections per execution.
              execution.reflections.each_with_index do |value, index|

                # Create reflection.
                reflection = Reflection.new(execution, index + 1, @@reflekt.aggregator)
                execution.reflections[index] = reflection

                # Execute reflection.
                reflection.reflect(*args)
                @reflekt_counts[method] = @reflekt_counts[method] + 1

                # Save reflection.
                @@reflekt.db.get("reflections").push(reflection.serialize())

              end

              # Save control.
              @@reflekt.db.get("controls").push(control.serialize())

              # Save results.
              @@reflekt.db.write()

              # Render results.
              @@reflekt.renderer.render()

            end

            execution.is_reflecting = false
          end

        end

        # Don't execute skipped methods when reflecting.
        unless execution.is_reflecting? && self.class.reflekt_skipped?(method)

          # Continue execution / shadow execution.
          super *args

        end

      # When Reflekt disabled or control reflection failed.
      else

        # Continue execution.
        super *args

      end

    end

  end

  # Continue initialization.
  super

end