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)


134
135
136
# File 'lib/Reflekt.rb', line 134

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

def initialize(*args)

  @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|

      # 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.
        # Subsequent method calls are shadow executions on cloned objects.
        ##
        if execution.has_empty_reflections? && !execution.is_reflecting?
          execution.is_reflecting = true

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

          # Execute control.
          control.reflect(*args)

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

          # 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.result())

          end

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

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

          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

    end

  end

  # Continue initialization.
  super

end