Module: Reflekt
- Defined in:
- lib/reflekt.rb
Overview
REFLEKT
Usage. Prepend to the class like so:
class ExampleClass
prepend Reflekt
Defined Under Namespace
Modules: Klass
Constant Summary collapse
- @@clone_count =
5
Instance Method Summary collapse
Instance Method Details
#initialize(*args) ⇒ Object
24 25 26 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 |
# File 'lib/reflekt.rb', line 24 def initialize(*args) @reflekt_forked = false @reflekt_clones = [] # Override methods. self.class.instance_methods(false).each do |method| self.define_singleton_method(method) do |*args| # When method called in flow. if @reflekt_forked unless self.class.deflekted?(method) # Reflekt on method. @reflekt_clones.each do |clone| reflekt_action(clone, method, *args) end # Save results. @@db.write() end end # Continue method flow. super *args end end # Continue contructor flow. super # Create forks. reflekt_fork() end |
#reflekt_action(clone, method, *args) ⇒ Object
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 |
# File 'lib/reflekt.rb', line 69 def reflekt_action(clone, method, *args) class_name = clone.class.to_s method_name = method.to_s # Create new arguments. new_args = [] args.each do |arg| case arg when Integer new_args << rand(9999) else new_args << arg end end # Action method with new arguments. begin clone.send(method, *new_args) # Build reflection. reflection = { "time" => Time.now.to_i, } # When error. rescue StandardError => error reflection["status"] = "error" reflection["error"] = error # When success. else reflection["status"] = "success" end # Save reflection. @@db.get("#{class_name}.#{method_name}") .push(reflection) end |
#reflekt_fork ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/reflekt.rb', line 59 def reflekt_fork() @@clone_count.times do |clone| @reflekt_clones << self.clone end @reflekt_forked = true end |