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 action.
        action = @@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 action done reflecting.
          if action.nil? || action.has_finished_reflecting?

            # Create action.
            action = Action.new(self, method, @@reflekt.config.reflect_amount, @@reflekt.stack)

            @@reflekt.stack.push(action)

          end

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

            # Create control.
            control = Control.new(action, 0, @@reflekt.aggregator)
            action.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 a reflection.
              @@reflekt.db.get("reflections").push(control.serialize())

              # Multiple experiments per action.
              action.experiments.each_with_index do |value, index|

                # Create experiment.
                experiment = Experiment.new(action, index + 1, @@reflekt.aggregator)
                action.experiments[index] = experiment

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

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

              end

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

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

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

            end

            action.is_reflecting = false
          end

        end

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

          # Continue action / shadow action.
          super *args

        end

      # When Reflekt disabled or control reflection failed.
      else

        # Continue action.
        super *args

      end

    end

  end

  # Continue initialization.
  super

end