Module: Reflekt

Defined in:
lib/reflekt.rb

Overview

REFLEKT

Usage. Prepend to the class like so:

class ExampleClass
  prepend Reflekt

Defined Under Namespace

Modules: SingletonClassMethods

Constant Summary collapse

REFLEKT_STATUS =
"s"
REFLEKT_PASS =
"p"
REFLEKT_FAIL =
"f"
REFLEKT_MESSAGE =
"m"
@@reflekt_clone_count =
5

Instance Method Summary collapse

Instance Method Details

#initialize(*args) ⇒ Object



23
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/reflekt.rb', line 23

def initialize(*args)

  @reflekt_forked = false
  @reflekt_clones = []

  # Limit the amount of clones that can be created per instance.
  # A method called thousands of times doesn't need that many reflections.
  @reflekt_limit = 5
  @reflekt_count = 0

  # 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

        if @reflekt_count < @reflekt_limit
          unless self.class.deflekted?(method)

            # Reflekt on method.
            @reflekt_clones.each do |clone|
              reflekt_action(clone, method, *args)
            end

            # Save results.
            @@reflekt_db.write()

            reflekt_render()

          end
          @reflekt_count = @reflekt_count + 1
        end

      end

      # Continue method flow.
      super *args
    end

  end

  # Continue contructor flow.
  super

  # Create forks.
  reflekt_fork()

end

#reflekt_action(clone, method, *args) ⇒ Object



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

def reflekt_action(clone, method, *args)

  class_name = clone.class.to_s
  method_name = method.to_s

  # Create new arguments that are deviations on inputted type.
  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 fail.
rescue StandardError => message
    reflection[REFLEKT_STATUS] = REFLEKT_MESSAGE
    reflection[REFLEKT_MESSAGE] = message
  # When pass.
  else
    reflection[REFLEKT_STATUS] = REFLEKT_PASS
  end

  # Save reflection.
  @@reflekt_db.get("#{class_name}.#{method_name}").push(reflection)

end

#reflekt_forkObject



73
74
75
76
77
78
79
80
81
# File 'lib/reflekt.rb', line 73

def reflekt_fork()

  @@reflekt_clone_count.times do |clone|
    @reflekt_clones << self.clone
  end

  @reflekt_forked = true

end

#reflekt_renderObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/reflekt.rb', line 120

def reflekt_render()

  # Render results.
  @@reflekt_json = File.read("#{@@reflekt_output_path}/db.json")
  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/alpine.js")
  File.open("#{@@reflekt_output_path}/alpine.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