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

Instance Method Summary collapse

Instance Method Details

#initialize(*args) ⇒ Object



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

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] >= @@reflekt.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

        # Get ruler.
        # The method's ruler will not exist the first time the db generated.
        if @@reflekt.rules.key? execution.caller_class.to_s.to_sym
          ruler = @@reflekt.rules[execution.caller_class.to_s.to_sym][method.to_s]
        else
          ruler = nil
        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

          class_name = execution.caller_class.to_s
          method_name = method.to_s

          # Create control.
          control = Control.new(execution, method, ruler)
          execution.control = control

          # Execute control.
          control.reflect(*args)

          # Save control.
          @@reflekt.db.get("#{class_name}.#{method_name}.controls").push(control.result())

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

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

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

            # Save reflection.
            @@reflekt.db.get("#{class_name}.#{method_name}.reflections").push(reflection.result())

          end

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

          # Render results.
          reflekt_render()

          execution.is_reflecting = false
        end

      end

      # Continue execution / shadow execution.
      super *args

    end

  end

  # Continue initialization.
  super

end

#reflekt_renderObject

Render results.



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

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