Module: Reflekt

Defined in:
lib/reflekt.rb

Overview

REFLEKT

An Execution is created each time a method is called. Multiple Refections are created per Execution. These Reflections execute on a ShadowStack on cloned objects. Then flow is returned to the original method and normal execution continues.

Usage:

class ExampleClass
  prepend Reflekt

Defined Under Namespace

Modules: SingletonClassMethods

Constant Summary collapse

@@reflekt_reflect_amount =

The amount of reflections to create per method call.

2
@@reflection_limit =

Limit the amount of reflections that can be created per instance method. A method called thousands of times doesn’t need that many reflections.

10

Instance Method Summary collapse

Instance Method Details

#initialize(*args) ⇒ Object



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
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
# File 'lib/reflekt.rb', line 31

def initialize(*args)

  @reflection_counts = {}

  # Get instance methods.
  # TODO: Include parent methods like "Array.include?".
  self.class.instance_methods(false).each do |method|

    # Don't process skipped methods.
    next if self.class.reflekt_skipped?(method)

    @reflection_counts[method] = 0

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

      # Don't reflect when limit reached.
      unless @reflection_counts[method] >= @@reflection_limit

        # Get current execution.
        execution = @@reflekt_stack.peek()

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

          # Create execution.
          execution = Execution.new(self, @@reflekt_reflect_amount)
          @@reflekt_stack.push(execution)

        end

        # Reflect.
        # The first method call in the Execution creates a Reflection.
        # Subsequent method calls are shadow executions on cloned objects.
        if execution.has_empty_reflections? && !execution.is_reflecting?
          execution.is_reflecting = true

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

            # Flag first reflection is a control.
            is_control = false
            is_control = true if index == 0

            # Create reflection.
            reflection = Reflection.new(execution, method, is_control)
            execution.reflections[index] = reflection

            # Execute reflection.
            reflection.reflect(*args)

            # Add result.
            class_name = execution.caller_class.to_s
            method_name = method.to_s
            @@reflekt_db.get("#{class_name}.#{method_name}").push(reflection.result())

          end

          # Save results.
          @@reflekt_db.write()

          # Render results.
          reflekt_render()

          execution.is_reflecting = false
        end

      end

      @reflection_counts[method] = @reflection_counts[method] + 1

      # Continue execution / shadow execution.
      super *args

    end

  end

  # Continue initialization.
  super

end

#reflekt_renderObject

Render results.



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
# File 'lib/reflekt.rb', line 117

def reflekt_render()

  # Get JSON.
  @@reflekt_json = File.read("#{@@reflekt_output_path}/db.json")

  # Save HTML.
  template = File.read("#{@@reflekt_path}/web/template.html.erb")
  rendered = ERB.new(template).result(binding)
  File.open("#{@@reflekt_output_path}/index.html", 'w+') do |f|
    f.write rendered
  end

  # Add JS.
  javascript = File.read("#{@@reflekt_path}/web/script.js")
  File.open("#{@@reflekt_output_path}/script.js", 'w+') do |f|
    f.write javascript
  end

  # Add CSS.
  stylesheet = File.read("#{@@reflekt_path}/web/style.css")
  File.open("#{@@reflekt_output_path}/style.css", 'w+') do |f|
    f.write stylesheet
  end

end