reflekt 0.7.2 → 0.9.5

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.
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.7.2
4
+ version: 0.9.5
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-07-22 00:00:00.000000000 Z
11
+ date: 2020-10-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rowdb
@@ -30,11 +30,23 @@ executables: []
30
30
  extensions: []
31
31
  extra_rdoc_files: []
32
32
  files:
33
- - lib/reflekt.rb
33
+ - lib/Accessor.rb
34
+ - lib/Control.rb
35
+ - lib/Execution.rb
36
+ - lib/Reflection.rb
37
+ - lib/Reflekt.rb
38
+ - lib/Renderer.rb
39
+ - lib/Rule.rb
40
+ - lib/RulePool.rb
41
+ - lib/Ruler.rb
42
+ - lib/ShadowStack.rb
43
+ - lib/rules/FloatRule.rb
44
+ - lib/rules/IntegerRule.rb
45
+ - lib/rules/StringRule.rb
34
46
  - lib/web/script.js
35
47
  - lib/web/style.css
36
48
  - lib/web/template.html.erb
37
- homepage: https://github.com/maedi/reflekt
49
+ homepage: https://github.com/refIekt/reflekt
38
50
  licenses:
39
51
  - MPL-2.0
40
52
  metadata: {}
@@ -1,277 +0,0 @@
1
- require 'set'
2
- require 'erb'
3
- require 'rowdb'
4
-
5
- ################################################################################
6
- # REFLEKT
7
- #
8
- # Usage. Prepend to the class like so:
9
- #
10
- # class ExampleClass
11
- # prepend Reflekt
12
- ################################################################################
13
-
14
- module Reflekt
15
-
16
- # Reflection keys.
17
- REFLEKT_TIME = "t"
18
- REFLEKT_INPUT = "i"
19
- REFLEKT_OUTPUT = "o"
20
- REFLEKT_TYPE = "T"
21
- REFLEKT_COUNT = "C"
22
- REFLEKT_VALUE = "V"
23
- REFLEKT_STATUS = "s"
24
- REFLEKT_MESSAGE = "m"
25
- # Reflection values.
26
- REFLEKT_PASS = "p"
27
- REFLEKT_FAIL = "f"
28
-
29
- @@reflekt_clone_count = 5
30
-
31
- def initialize(*args)
32
-
33
- @reflekt_forked = false
34
- @reflekt_clones = []
35
-
36
- # Limit the amount of clones that can be created per instance.
37
- # A method called thousands of times doesn't need that many reflections.
38
- @reflekt_limit = 5
39
- @reflekt_count = 0
40
-
41
- # Override methods.
42
- self.class.instance_methods(false).each do |method|
43
- self.define_singleton_method(method) do |*args|
44
-
45
- # When method called in flow.
46
- if @reflekt_forked
47
-
48
- if @reflekt_count < @reflekt_limit
49
- unless self.class.deflekted?(method)
50
-
51
- # Reflekt on method.
52
- @reflekt_clones.each do |clone|
53
- reflekt_action(clone, method, *args)
54
- end
55
-
56
- # Save results.
57
- @@reflekt_db.write()
58
-
59
- reflekt_render()
60
-
61
- end
62
- @reflekt_count = @reflekt_count + 1
63
- end
64
-
65
- end
66
-
67
- # Continue method flow.
68
- super *args
69
- end
70
-
71
- end
72
-
73
- # Continue contructor flow.
74
- super
75
-
76
- # Create forks.
77
- reflekt_fork()
78
-
79
- end
80
-
81
- def reflekt_fork()
82
-
83
- @@reflekt_clone_count.times do |clone|
84
- @reflekt_clones << self.clone
85
- end
86
-
87
- @reflekt_forked = true
88
-
89
- end
90
-
91
- def reflekt_action(clone, method, *args)
92
-
93
- class_name = clone.class.to_s
94
- method_name = method.to_s
95
-
96
- # TODO: Create control fork. Get good value. Check against it.
97
-
98
- # Create new arguments that are deviations on inputted type.
99
- input = []
100
-
101
- args.each do |arg|
102
- case arg
103
- when Integer
104
- input << rand(9999)
105
- else
106
- input << arg
107
- end
108
- end
109
-
110
- # Action method with new arguments.
111
- begin
112
- output = clone.send(method, *input)
113
-
114
- # Build reflection.
115
- reflection = {
116
- REFLEKT_TIME => Time.now.to_i,
117
- REFLEKT_INPUT => reflekt_normalize_input(input),
118
- REFLEKT_OUTPUT => reflekt_normalize_output(output)
119
- }
120
-
121
- # When fail.
122
- rescue StandardError => message
123
- reflection[REFLEKT_STATUS] = REFLEKT_MESSAGE
124
- reflection[REFLEKT_MESSAGE] = message
125
- # When pass.
126
- else
127
- reflection[REFLEKT_STATUS] = REFLEKT_PASS
128
- end
129
-
130
- # Save reflection.
131
- @@reflekt_db.get("#{class_name}.#{method_name}").push(reflection)
132
-
133
- end
134
-
135
- ##
136
- # Normalize inputs.
137
- #
138
- # @param args - The actual inputs.
139
- # @return - A generic inputs representation.
140
- ##
141
- def reflekt_normalize_input(args)
142
- inputs = []
143
- args.each do |arg|
144
- input = {
145
- REFLEKT_TYPE => arg.class.to_s,
146
- REFLEKT_VALUE => reflekt_normalize_value(arg)
147
- }
148
- if (arg.class == Array)
149
- input[REFLEKT_COUNT] = arg.count
150
- end
151
- inputs << input
152
- end
153
- inputs
154
- end
155
-
156
- ##
157
- # Normalize output.
158
- #
159
- # @param output - The actual output.
160
- # @return - A generic output representation.
161
- ##
162
- def reflekt_normalize_output(output)
163
-
164
- o = {
165
- REFLEKT_TYPE => output.class.to_s,
166
- REFLEKT_VALUE => reflekt_normalize_value(output)
167
- }
168
-
169
- if (output.class == Array || output.class == Hash)
170
- o[REFLEKT_COUNT] = output.count
171
- elsif (output.class == TrueClass || output.class == FalseClass)
172
- o[REFLEKT_TYPE] = :Boolean
173
- end
174
-
175
- return o
176
-
177
- end
178
-
179
- def reflekt_normalize_value(value)
180
-
181
- unless value.nil?
182
- value = value.to_s.gsub(/\r?\n/, " ").to_s
183
- if value.length >= 30
184
- value = value[0, value.rindex(/\s/,30)].rstrip() + '...'
185
- end
186
- end
187
-
188
- return value
189
-
190
- end
191
-
192
- def reflekt_render()
193
-
194
- # Render results.
195
- @@reflekt_json = File.read("#{@@reflekt_output_path}/db.json")
196
- template = File.read("#{@@reflekt_path}/web/template.html.erb")
197
- rendered = ERB.new(template).result(binding)
198
- File.open("#{@@reflekt_output_path}/index.html", 'w+') do |f|
199
- f.write rendered
200
- end
201
-
202
- # Add JS.
203
- javascript = File.read("#{@@reflekt_path}/web/script.js")
204
- File.open("#{@@reflekt_output_path}/script.js", 'w+') do |f|
205
- f.write javascript
206
- end
207
-
208
- # Add CSS.
209
- stylesheet = File.read("#{@@reflekt_path}/web/style.css")
210
- File.open("#{@@reflekt_output_path}/style.css", 'w+') do |f|
211
- f.write stylesheet
212
- end
213
-
214
- end
215
-
216
- private
217
-
218
- def self.prepended(base)
219
- # Prepend class methods to the instance's singleton class.
220
- base.singleton_class.prepend(SingletonClassMethods)
221
-
222
- @@reflekt_setup ||= reflekt_setup_class
223
- end
224
-
225
- # Setup class.
226
- def self.reflekt_setup_class()
227
-
228
- # Receive configuration from host application.
229
- $ENV ||= {}
230
- $ENV[:reflekt] ||= $ENV[:reflekt] = {}
231
-
232
- @@reflekt_path = File.dirname(File.realpath(__FILE__))
233
-
234
- # Create "reflections" directory in configured path.
235
- if $ENV[:reflekt][:output_path]
236
- @@reflekt_output_path = File.join($ENV[:reflekt][:output_path], 'reflections')
237
- # Create "reflections" directory in current execution path.
238
- else
239
- @@reflekt_output_path = File.join(Dir.pwd, 'reflections')
240
- end
241
-
242
- unless Dir.exist? @@reflekt_output_path
243
- Dir.mkdir(@@reflekt_output_path)
244
- end
245
-
246
- # Create database.
247
- @@reflekt_db = Rowdb.new(@@reflekt_output_path + '/db.json')
248
- @@reflekt_db.defaults({ :reflekt => { :api_version => 1 }}).write()
249
-
250
- return true
251
- end
252
-
253
- module SingletonClassMethods
254
-
255
- @@deflekted_methods = Set.new
256
-
257
- ##
258
- # Skip a method.
259
- #
260
- # @param method - A symbol representing the method name.
261
- ##
262
- def reflekt_skip(method)
263
- @@deflekted_methods.add(method)
264
- end
265
-
266
- def deflekted?(method)
267
- return true if @@deflekted_methods.include?(method)
268
- false
269
- end
270
-
271
- def reflekt_limit(amount)
272
- @reflekt_limit = amount
273
- end
274
-
275
- end
276
-
277
- end