Module: Reflekt
- Defined in:
- lib/Reflekt.rb
Defined Under Namespace
Modules: SingletonClassMethods
Class Method Summary collapse
- .prepended(base) ⇒ Object
-
.reflekt_setup_class ⇒ Object
Setup class.
Instance Method Summary collapse
Class Method Details
.prepended(base) ⇒ Object
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/Reflekt.rb', line 126 def self.prepended(base) # Prepend class methods to the instance's singleton class. base.singleton_class.prepend(SingletonClassMethods) # Setup class. @@reflekt = Accessor.new() @@reflekt.setup ||= reflekt_setup_class end |
.reflekt_setup_class ⇒ Object
Setup class.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/Reflekt.rb', line 138 def self.reflekt_setup_class() # Receive configuration. $ENV ||= {} $ENV[:reflekt] ||= $ENV[:reflekt] = {} # Set configuration. @@reflekt.path = File.dirname(File.realpath(__FILE__)) # Build reflections directory. if $ENV[:reflekt][:output_path] @@reflekt.output_path = File.join($ENV[:reflekt][:output_path], 'reflections') # Build reflections directory in current execution path. else @@reflekt.output_path = File.join(Dir.pwd, 'reflections') end # Create reflections directory. unless Dir.exist? @@reflekt.output_path Dir.mkdir(@@reflekt.output_path) end # Create database. @@reflekt.db = Rowdb.new(@@reflekt.output_path + '/db.json') @@reflekt.db.defaults({ :reflekt => { :api_version => 1 }}) # Create shadow execution stack. @@reflekt.stack = ShadowStack.new() # Define rules. # TODO: Fix Rowdb.get(path) not returning data at path after Rowdb.push()? @@reflekt.rules = {} db = @@reflekt.db.value() db.each do |class_name, class_values| @@reflekt.rules[class_name] = {} class_values.each do |method_name, method_values| next if method_values.nil? next unless method_values.class == Hash if method_values.key? "controls" ruler = Ruler.new() ruler.load(method_values['controls']) ruler.train() @@reflekt.rules[class_name][method_name] = ruler end end end # The amount of reflections to create per method call. @@reflekt.reflect_amount = 2 # Limit the amount of reflections that can be created per instance method. # A method called thousands of times doesn't need that many reflections. @@reflekt.reflection_limit = 10 # Create renderer. @@reflekt.renderer = Renderer.new(@@reflekt.path, @@reflekt.output_path) return true end |
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.renderer.render() execution.is_reflecting = false end end # Continue execution / shadow execution. super *args end end # Continue initialization. super end |