reflekt 0.9.2 → 0.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/Accessor.rb +37 -0
- data/lib/reflekt.rb +42 -37
- data/lib/web/template.html.erb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d76793031a1fb34931c9c71500a45249951cc50dcd820be7ac2faf6df2b091d3
|
4
|
+
data.tar.gz: 5263bb832fff7b00d73496cee4f708b1355907f38e496dd96486ca0bbbd0efab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d0a232b0fbc4cb944a8c19827da8bbe2962e4fc41b178e6b9ae99853c7f96d611d6b08b307313d8f8430af2e118933149e1772f3c3c125bd4f14eb70bf61b82
|
7
|
+
data.tar.gz: c5e439d83d26c7b9938b08ecfa466817e39cdf938a584012a86dd7ec0d2187f8ead6559c085874f8df5e1788567d9b57a847249ae1f20e76c03c67be58fea56d
|
data/lib/Accessor.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
################################################################################
|
2
|
+
# ACCESSOR
|
3
|
+
#
|
4
|
+
# Access variables via one object to avoid polluting the caller class.
|
5
|
+
#
|
6
|
+
# Only 2 variables are not accessed via Accessor:
|
7
|
+
# - @reflection_counts on the instance
|
8
|
+
# - @@reflekt_skipped_methods on the instance's singleton class
|
9
|
+
################################################################################
|
10
|
+
|
11
|
+
class Accessor
|
12
|
+
|
13
|
+
attr_accessor :setup
|
14
|
+
attr_accessor :db
|
15
|
+
attr_accessor :json
|
16
|
+
attr_accessor :stack
|
17
|
+
attr_accessor :rules
|
18
|
+
attr_accessor :path
|
19
|
+
attr_accessor :output_path
|
20
|
+
attr_accessor :reflect_amount
|
21
|
+
attr_accessor :reflection_limit
|
22
|
+
|
23
|
+
def initialize()
|
24
|
+
|
25
|
+
@setup = nil
|
26
|
+
@db = nil
|
27
|
+
@json = nil
|
28
|
+
@stack = nil
|
29
|
+
@rules = nil
|
30
|
+
@path = nil
|
31
|
+
@output_path = nil
|
32
|
+
@reflect_amount = nil
|
33
|
+
@reflection_limit = nil
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
data/lib/reflekt.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'set'
|
2
2
|
require 'erb'
|
3
3
|
require 'rowdb'
|
4
|
+
require 'Accessor'
|
4
5
|
require 'Control'
|
5
6
|
require 'Execution'
|
6
7
|
require 'Reflection'
|
@@ -23,13 +24,6 @@ require 'ShadowStack'
|
|
23
24
|
|
24
25
|
module Reflekt
|
25
26
|
|
26
|
-
# The amount of reflections to create per method call.
|
27
|
-
@@reflekt_reflect_amount = 2
|
28
|
-
|
29
|
-
# Limit the amount of reflections that can be created per instance method.
|
30
|
-
# A method called thousands of times doesn't need that many reflections.
|
31
|
-
@@reflection_limit = 10
|
32
|
-
|
33
27
|
def initialize(*args)
|
34
28
|
|
35
29
|
@reflection_counts = {}
|
@@ -47,24 +41,24 @@ module Reflekt
|
|
47
41
|
self.define_singleton_method(method) do |*args|
|
48
42
|
|
49
43
|
# Don't reflect when limit reached.
|
50
|
-
unless @reflection_counts[method] >= @@reflection_limit
|
44
|
+
unless @reflection_counts[method] >= @@reflekt.reflection_limit
|
51
45
|
|
52
46
|
# Get current execution.
|
53
|
-
execution = @@
|
47
|
+
execution = @@reflekt.stack.peek()
|
54
48
|
|
55
49
|
# When stack empty or past execution done reflecting.
|
56
50
|
if execution.nil? || execution.has_finished_reflecting?
|
57
51
|
|
58
52
|
# Create execution.
|
59
|
-
execution = Execution.new(self, @@
|
60
|
-
@@
|
53
|
+
execution = Execution.new(self, @@reflekt.reflect_amount)
|
54
|
+
@@reflekt.stack.push(execution)
|
61
55
|
|
62
56
|
end
|
63
57
|
|
64
58
|
# Get ruler.
|
65
59
|
# The method's ruler will not exist the first time the db generated.
|
66
|
-
if @@
|
67
|
-
ruler = @@
|
60
|
+
if @@reflekt.rules.key? execution.caller_class.to_s.to_sym
|
61
|
+
ruler = @@reflekt.rules[execution.caller_class.to_s.to_sym][method.to_s]
|
68
62
|
else
|
69
63
|
ruler = nil
|
70
64
|
end
|
@@ -86,7 +80,7 @@ module Reflekt
|
|
86
80
|
control.reflect(*args)
|
87
81
|
|
88
82
|
# Save control.
|
89
|
-
@@
|
83
|
+
@@reflekt.db.get("#{class_name}.#{method_name}.controls").push(control.result())
|
90
84
|
|
91
85
|
# Multiple reflections per execution.
|
92
86
|
execution.reflections.each_with_index do |value, index|
|
@@ -100,12 +94,12 @@ module Reflekt
|
|
100
94
|
@reflection_counts[method] = @reflection_counts[method] + 1
|
101
95
|
|
102
96
|
# Save reflection.
|
103
|
-
@@
|
97
|
+
@@reflekt.db.get("#{class_name}.#{method_name}.reflections").push(reflection.result())
|
104
98
|
|
105
99
|
end
|
106
100
|
|
107
101
|
# Save results.
|
108
|
-
@@
|
102
|
+
@@reflekt.db.write()
|
109
103
|
|
110
104
|
# Render results.
|
111
105
|
reflekt_render()
|
@@ -133,24 +127,24 @@ module Reflekt
|
|
133
127
|
def reflekt_render()
|
134
128
|
|
135
129
|
# Get JSON.
|
136
|
-
@@
|
130
|
+
@@reflekt.json = File.read("#{@@reflekt.output_path}/db.json")
|
137
131
|
|
138
132
|
# Save HTML.
|
139
|
-
template = File.read("#{@@
|
133
|
+
template = File.read("#{@@reflekt.path}/web/template.html.erb")
|
140
134
|
rendered = ERB.new(template).result(binding)
|
141
|
-
File.open("#{@@
|
135
|
+
File.open("#{@@reflekt.output_path}/index.html", 'w+') do |f|
|
142
136
|
f.write rendered
|
143
137
|
end
|
144
138
|
|
145
139
|
# Add JS.
|
146
|
-
javascript = File.read("#{@@
|
147
|
-
File.open("#{@@
|
140
|
+
javascript = File.read("#{@@reflekt.path}/web/script.js")
|
141
|
+
File.open("#{@@reflekt.output_path}/script.js", 'w+') do |f|
|
148
142
|
f.write javascript
|
149
143
|
end
|
150
144
|
|
151
145
|
# Add CSS.
|
152
|
-
stylesheet = File.read("#{@@
|
153
|
-
File.open("#{@@
|
146
|
+
stylesheet = File.read("#{@@reflekt.path}/web/style.css")
|
147
|
+
File.open("#{@@reflekt.output_path}/style.css", 'w+') do |f|
|
154
148
|
f.write stylesheet
|
155
149
|
end
|
156
150
|
|
@@ -159,10 +153,14 @@ module Reflekt
|
|
159
153
|
private
|
160
154
|
|
161
155
|
def self.prepended(base)
|
156
|
+
|
162
157
|
# Prepend class methods to the instance's singleton class.
|
163
158
|
base.singleton_class.prepend(SingletonClassMethods)
|
164
159
|
|
165
|
-
|
160
|
+
# Setup class.
|
161
|
+
@@reflekt = Accessor.new()
|
162
|
+
@@reflekt.setup ||= reflekt_setup_class
|
163
|
+
|
166
164
|
end
|
167
165
|
|
168
166
|
# Setup class.
|
@@ -173,33 +171,33 @@ module Reflekt
|
|
173
171
|
$ENV[:reflekt] ||= $ENV[:reflekt] = {}
|
174
172
|
|
175
173
|
# Set configuration.
|
176
|
-
@@
|
174
|
+
@@reflekt.path = File.dirname(File.realpath(__FILE__))
|
177
175
|
|
178
176
|
# Build reflections directory.
|
179
177
|
if $ENV[:reflekt][:output_path]
|
180
|
-
@@
|
178
|
+
@@reflekt.output_path = File.join($ENV[:reflekt][:output_path], 'reflections')
|
181
179
|
# Build reflections directory in current execution path.
|
182
180
|
else
|
183
|
-
@@
|
181
|
+
@@reflekt.output_path = File.join(Dir.pwd, 'reflections')
|
184
182
|
end
|
185
183
|
# Create reflections directory.
|
186
|
-
unless Dir.exist? @@
|
187
|
-
Dir.mkdir(@@
|
184
|
+
unless Dir.exist? @@reflekt.output_path
|
185
|
+
Dir.mkdir(@@reflekt.output_path)
|
188
186
|
end
|
189
187
|
|
190
188
|
# Create database.
|
191
|
-
@@
|
192
|
-
@@
|
189
|
+
@@reflekt.db = Rowdb.new(@@reflekt.output_path + '/db.json')
|
190
|
+
@@reflekt.db.defaults({ :reflekt => { :api_version => 1 }})
|
193
191
|
|
194
192
|
# Create shadow execution stack.
|
195
|
-
@@
|
193
|
+
@@reflekt.stack = ShadowStack.new()
|
196
194
|
|
197
195
|
# Define rules.
|
198
196
|
# TODO: Fix Rowdb.get(path) not returning data at path after Rowdb.push()?
|
199
|
-
@@
|
200
|
-
db = @@
|
197
|
+
@@reflekt.rules = {}
|
198
|
+
db = @@reflekt.db.value()
|
201
199
|
db.each do |class_name, class_values|
|
202
|
-
@@
|
200
|
+
@@reflekt.rules[class_name] = {}
|
203
201
|
class_values.each do |method_name, method_values|
|
204
202
|
next if method_values.nil?
|
205
203
|
next unless method_values.class == Hash
|
@@ -209,11 +207,18 @@ module Reflekt
|
|
209
207
|
ruler.load(method_values['controls'])
|
210
208
|
ruler.train()
|
211
209
|
|
212
|
-
@@
|
210
|
+
@@reflekt.rules[class_name][method_name] = ruler
|
213
211
|
end
|
214
212
|
end
|
215
213
|
end
|
216
214
|
|
215
|
+
# The amount of reflections to create per method call.
|
216
|
+
@@reflekt.reflect_amount = 2
|
217
|
+
|
218
|
+
# Limit the amount of reflections that can be created per instance method.
|
219
|
+
# A method called thousands of times doesn't need that many reflections.
|
220
|
+
@@reflekt.reflection_limit = 10
|
221
|
+
|
217
222
|
return true
|
218
223
|
end
|
219
224
|
|
@@ -236,7 +241,7 @@ module Reflekt
|
|
236
241
|
end
|
237
242
|
|
238
243
|
def reflekt_limit(amount)
|
239
|
-
@@reflection_limit = amount
|
244
|
+
@@reflekt.reflection_limit = amount
|
240
245
|
end
|
241
246
|
|
242
247
|
end
|
data/lib/web/template.html.erb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reflekt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maedi Prichard
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-10-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rowdb
|
@@ -30,6 +30,7 @@ executables: []
|
|
30
30
|
extensions: []
|
31
31
|
extra_rdoc_files: []
|
32
32
|
files:
|
33
|
+
- lib/Accessor.rb
|
33
34
|
- lib/Control.rb
|
34
35
|
- lib/Execution.rb
|
35
36
|
- lib/Reflection.rb
|