reflekt 0.9.3 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d76793031a1fb34931c9c71500a45249951cc50dcd820be7ac2faf6df2b091d3
4
- data.tar.gz: 5263bb832fff7b00d73496cee4f708b1355907f38e496dd96486ca0bbbd0efab
3
+ metadata.gz: f2048b61fdb7acc0deede9ee5bcae644e6d51fa6e2dab2842f748433ce5ba949
4
+ data.tar.gz: dce2b1bd92e2eaf90c16e1cad6c27d9b9fc686001c958124f184a5d11a2ebe8d
5
5
  SHA512:
6
- metadata.gz: 6d0a232b0fbc4cb944a8c19827da8bbe2962e4fc41b178e6b9ae99853c7f96d611d6b08b307313d8f8430af2e118933149e1772f3c3c125bd4f14eb70bf61b82
7
- data.tar.gz: c5e439d83d26c7b9938b08ecfa466817e39cdf938a584012a86dd7ec0d2187f8ead6559c085874f8df5e1788567d9b57a847249ae1f20e76c03c67be58fea56d
6
+ metadata.gz: b4bdebf8083aef0a50806c1970b25f90736dfe68b8373f5a742ef6c7f2e4139e4ca0d273d683d826230069de2039c5fda7428c4ed8147471da00f1e93704f59a
7
+ data.tar.gz: afbe22e05eb10efe8ead84240c4296546e10570c19e73efea912494e55a85538c8fbed2872c42485ea21ba696a101347e73ec5feee37a568636a154dcae8bcf6
@@ -12,8 +12,8 @@ class Accessor
12
12
 
13
13
  attr_accessor :setup
14
14
  attr_accessor :db
15
- attr_accessor :json
16
15
  attr_accessor :stack
16
+ attr_accessor :renderer
17
17
  attr_accessor :rules
18
18
  attr_accessor :path
19
19
  attr_accessor :output_path
@@ -24,8 +24,8 @@ class Accessor
24
24
 
25
25
  @setup = nil
26
26
  @db = nil
27
- @json = nil
28
27
  @stack = nil
28
+ @renderer = nil
29
29
  @rules = nil
30
30
  @path = nil
31
31
  @output_path = nil
@@ -14,11 +14,11 @@ class Control < Reflection
14
14
  ##
15
15
  def reflect(*args)
16
16
 
17
- @input = *args
17
+ @inputs = *args
18
18
 
19
19
  # Action method with new arguments.
20
20
  begin
21
- @output = @clone.send(@method, *@input)
21
+ @output = @clone.send(@method, *@inputs)
22
22
  # When fail.
23
23
  rescue StandardError => message
24
24
  @status = FAIL
@@ -57,31 +57,36 @@ class Reflection
57
57
 
58
58
  # Action method with new arguments.
59
59
  begin
60
+
60
61
  # Validate input with controls.
61
62
  unless @ruler.nil?
62
- if @ruler.validate_inputs(@execution.caller_class, @method, @inputs)
63
+ if @ruler.validate_inputs(@inputs)
63
64
  @status = PASS
64
65
  else
65
66
  @status = FAIL
66
67
  end
67
68
  end
69
+
68
70
  # Run reflection.
69
71
  @output = @clone.send(@method, *@inputs)
70
- # When fail.
71
- rescue StandardError => message
72
- @status = FAIL
73
- @message = message
74
- # When pass.
75
- else
72
+
76
73
  # Validate output with controls.
77
74
  unless @ruler.nil?
78
- if @ruler.validate_output(@execution.caller_class, @method, @output)
75
+ if @ruler.validate_output(@output)
79
76
  @status = PASS
80
77
  else
81
78
  @status = FAIL
82
79
  end
83
80
  return
84
81
  end
82
+
83
+ # When fail.
84
+ rescue StandardError => message
85
+ @status = FAIL
86
+ @message = message
87
+
88
+ # When no validation and execution fails.
89
+ else
85
90
  @status = PASS
86
91
  end
87
92
 
@@ -1,27 +1,27 @@
1
- require 'set'
2
- require 'erb'
3
- require 'rowdb'
4
- require 'Accessor'
5
- require 'Control'
6
- require 'Execution'
7
- require 'Reflection'
8
- require 'Ruler'
9
- require 'ShadowStack'
10
-
11
1
  ################################################################################
12
- # REFLEKT
2
+ # REFLEKT - By Maedi Prichard.
13
3
  #
14
4
  # An Execution is created each time a method is called.
15
- # Multiple Refections are created per Execution.
16
- # These Reflections execute on a ShadowStack on cloned objects.
5
+ # Many Refections are created per Execution.
6
+ # Each Reflection executes on a ShadowStack on cloned data.
17
7
  # Then flow is returned to the original method and normal execution continues.
18
8
  #
19
9
  # Usage:
20
- #
21
10
  # class ExampleClass
22
11
  # prepend Reflekt
23
12
  ################################################################################
24
13
 
14
+ require 'set'
15
+ require 'erb'
16
+ require 'rowdb'
17
+ require 'Accessor'
18
+ require 'Control'
19
+ require 'Execution'
20
+ require 'Reflection'
21
+ require 'Renderer'
22
+ require 'Ruler'
23
+ require 'ShadowStack'
24
+
25
25
  module Reflekt
26
26
 
27
27
  def initialize(*args)
@@ -102,7 +102,7 @@ module Reflekt
102
102
  @@reflekt.db.write()
103
103
 
104
104
  # Render results.
105
- reflekt_render()
105
+ @@reflekt.renderer.render()
106
106
 
107
107
  execution.is_reflecting = false
108
108
  end
@@ -121,35 +121,6 @@ module Reflekt
121
121
 
122
122
  end
123
123
 
124
- ##
125
- # Render results.
126
- ##
127
- def reflekt_render()
128
-
129
- # Get JSON.
130
- @@reflekt.json = File.read("#{@@reflekt.output_path}/db.json")
131
-
132
- # Save HTML.
133
- template = File.read("#{@@reflekt.path}/web/template.html.erb")
134
- rendered = ERB.new(template).result(binding)
135
- File.open("#{@@reflekt.output_path}/index.html", 'w+') do |f|
136
- f.write rendered
137
- end
138
-
139
- # Add JS.
140
- javascript = File.read("#{@@reflekt.path}/web/script.js")
141
- File.open("#{@@reflekt.output_path}/script.js", 'w+') do |f|
142
- f.write javascript
143
- end
144
-
145
- # Add CSS.
146
- stylesheet = File.read("#{@@reflekt.path}/web/style.css")
147
- File.open("#{@@reflekt.output_path}/style.css", 'w+') do |f|
148
- f.write stylesheet
149
- end
150
-
151
- end
152
-
153
124
  private
154
125
 
155
126
  def self.prepended(base)
@@ -219,6 +190,9 @@ module Reflekt
219
190
  # A method called thousands of times doesn't need that many reflections.
220
191
  @@reflekt.reflection_limit = 10
221
192
 
193
+ # Create renderer.
194
+ @@reflekt.renderer = Renderer.new(@@reflekt.path, @@reflekt.output_path)
195
+
222
196
  return true
223
197
  end
224
198
 
@@ -0,0 +1,38 @@
1
+ class Renderer
2
+
3
+ def initialize(path, output_path)
4
+ @path = path
5
+ @output_path = output_path
6
+ end
7
+
8
+ ##
9
+ # Render results.
10
+ ##
11
+ def render()
12
+
13
+ # Get JSON.
14
+ json = File.read("#{@output_path}/db.json")
15
+
16
+ # Save HTML.
17
+ template = File.read("#{@path}/web/template.html.erb")
18
+ rendered = ERB.new(template).result(binding)
19
+ File.open("#{@output_path}/index.html", 'w+') do |f|
20
+ f.write rendered
21
+ end
22
+
23
+ # Add JS.
24
+ javascript = File.read("#{@path}/web/script.js")
25
+ File.open("#{@output_path}/script.js", 'w+') do |f|
26
+ f.write javascript
27
+ end
28
+
29
+ # Add CSS.
30
+ stylesheet = File.read("#{@path}/web/style.css")
31
+ File.open("#{@output_path}/style.css", 'w+') do |f|
32
+ f.write stylesheet
33
+ end
34
+
35
+ end
36
+
37
+
38
+ end
@@ -1,38 +1,32 @@
1
+ ################################################################################
2
+ # RULES
3
+ #
4
+ # Abstract class.
5
+ # See lib/rules for rules.
6
+ ################################################################################
7
+
1
8
  require 'set'
2
9
 
3
10
  class Rule
4
11
 
5
- attr_accessor :type
6
- attr_accessor :min
7
- attr_accessor :max
8
- attr_accessor :length
9
- attr_accessor :types
10
- attr_accessor :values
11
-
12
+ # Each rule intitalises itself.
12
13
  def initialize()
13
-
14
- @types = Set.new
15
- @values = Set.new
16
- @min = nil
17
- @max = nil
18
- @length = nil
19
-
14
+ @values = nil
20
15
  end
21
16
 
22
- ##
23
- # A parameter can accept multiple input types.
24
- # Duplicates will not be added to the set.
25
- ##
26
- def add_type(type)
27
- @types.add(type)
17
+ # Each rule loads up an array of values.
18
+ def load(value)
19
+ @values << value
28
20
  end
29
21
 
30
- def add_value(value)
31
- @values.add(value)
22
+ # Each rule trains on values to determine its patterns.
23
+ def train()
24
+
32
25
  end
33
26
 
34
- def is_number?
35
- @types.include? Integer
27
+ # Each rule compares the data it has with the data it's given.
28
+ def validate()
29
+
36
30
  end
37
31
 
38
32
  end
@@ -0,0 +1,44 @@
1
+ ################################################################################
2
+ # RULE POOL
3
+ #
4
+ # A collection of rules generated for an argument.
5
+ # Duplicates will not be added to the sets.
6
+ ################################################################################
7
+
8
+ require 'set'
9
+ require 'Rule'
10
+
11
+ class RulePool
12
+
13
+ attr_accessor :types
14
+ attr_accessor :rules
15
+
16
+ def initialize()
17
+
18
+ @types = Set.new
19
+ @rules = {}
20
+
21
+ end
22
+
23
+ def train()
24
+
25
+ rules.each do |rule|
26
+ rule.train()
27
+ end
28
+
29
+ end
30
+
31
+ def validate(value)
32
+ result = true
33
+
34
+ rules.each do |rule|
35
+ result = rule.validate(value)
36
+ if result == false
37
+ result = false
38
+ end
39
+ end
40
+
41
+ return result
42
+ end
43
+
44
+ end
@@ -1,4 +1,10 @@
1
- require 'Rule'
1
+ ################################################################################
2
+ # RULER
3
+ #
4
+ # Aggregate a method's inputs and output into a series of generic rules.
5
+ ################################################################################
6
+
7
+ require 'RulePool'
2
8
 
3
9
  class Ruler
4
10
 
@@ -9,9 +15,9 @@ class Ruler
9
15
 
10
16
  def initialize()
11
17
 
18
+ # Reflections.
12
19
  @controls = nil
13
-
14
- # Rules.
20
+ # Arguments.
15
21
  @inputs = []
16
22
  @output = nil
17
23
 
@@ -22,90 +28,96 @@ class Ruler
22
28
  @controls = controls
23
29
  @controls.each_with_index do |control, index|
24
30
 
25
- # Multiple inputs.
31
+ # Create rules for each input.
26
32
  control[INPUT].each_with_index do |input, index|
27
33
  unless input.nil?
28
-
29
- # Create rule.
30
- if @inputs[index].nil?
31
- rule = Rule.new()
32
- @inputs[index] = rule
33
- else
34
- rule = @inputs[index]
35
- end
36
-
37
- # Add rules to rule.
38
- unless input[TYPE].nil? || input[TYPE].empty?
39
- rule.add_type(input[TYPE].class)
40
- end
41
- unless input[VALUE].nil? || input[VALUE].empty?
42
- rule.add_value(input[VALUE])
43
- end
44
-
45
- index = index + 1
34
+ @inputs[index] = load_rule_pool(@inputs[index], input[TYPE], input[VALUE])
46
35
  end
47
36
  end
48
37
 
49
- # Singular output.
38
+ # Create rules for the output.
50
39
  output = control[OUTPUT]
51
40
  unless control[OUTPUT].nil?
41
+ @output = load_rule_pool(@output, output[TYPE], output[VALUE])
42
+ end
52
43
 
53
- # Create rule.
54
- if @output.nil?
55
- rule = Rule.new()
56
- @output = rule
57
- else
58
- rule = @output
59
- end
44
+ end
60
45
 
61
- ## Add rules to rule.
62
- unless output[TYPE].nil? || output[TYPE].empty?
63
- rule.add_type(output[TYPE])
64
- end
65
- unless output[VALUE].nil? || output[VALUE].empty?
66
- rule.add_value(output[VALUE])
67
- end
46
+ end
47
+
48
+ def load_rule_pool(rule_pool, type, value)
49
+
50
+ if rule_pool.nil?
51
+ rule_pool = RulePool.new()
52
+ end
53
+
54
+ # Track data type.
55
+ rule_pool.types << type
56
+
57
+ # Get rule for this data type.
58
+ rule = nil
68
59
 
60
+ case type
61
+ when "Integer"
62
+ unless rule_pool.rules.key? IntegerRule
63
+ rule = IntegerRule.new()
64
+ rule_pool.rules << rule
65
+ else
66
+ rule = rule_pool.rules[IntegerRule]
69
67
  end
68
+ when "String"
69
+ unless rule_pool.rules.key? StringRule
70
+ rule = StringRule.new()
71
+ rule_pool.rules << rule
72
+ else
73
+ rule = rule_pool.rules[IntegerRule]
74
+ end
75
+ end
70
76
 
77
+ # Add value to rule.
78
+ unless rule.nil?
79
+ rule.load(value)
71
80
  end
72
81
 
82
+ return rule_pool
83
+
73
84
  end
74
85
 
75
86
  def train()
76
87
 
77
- @inputs.each do |rule|
78
- # Get min/max.
79
- if rule.is_number?
80
- numbers = rule.values.select {|num| num.class == Integer }
81
- numbers.sort!
82
- rule.min = numbers.first
83
- rule.max = numbers.last
88
+ @inputs.each do |rule_pool|
89
+ unless rule_pool.nil?
90
+ rule_pool.train()
84
91
  end
92
+ end
85
93
 
94
+ unless @output.nil?
95
+ @output.train()
86
96
  end
87
97
 
88
98
  end
89
99
 
90
- def validate_inputs(klass, method, inputs)
100
+ def validate_inputs(inputs)
91
101
  result = true
102
+
92
103
  inputs.each_with_index do |value, index|
93
- rule = @inputs[index]
94
- if rule.is_number? && value.class == Integer
95
- result = false if value < rule.min
96
- result = false if value > rule.max
104
+ rule_pool = @inputs[index]
105
+ unless rule_pool.validate(input)
106
+ result = false
97
107
  end
98
108
  end
109
+
99
110
  return result
100
111
  end
101
112
 
102
- def validate_output(klass, method, outputs)
113
+ def validate_output(output)
103
114
  result = true
104
- rule = @output
105
- if rule.is_number? && value.class == Integer
106
- result = false if value < rule.min
107
- result = false if value > rule.max
115
+
116
+ rule_pool = @output
117
+ unless rule_pool.validate(output)
118
+ result = false
108
119
  end
120
+
109
121
  return result
110
122
  end
111
123
 
@@ -0,0 +1,24 @@
1
+ class FloatRule < Rule
2
+
3
+ attr_accessor :min
4
+ attr_accessor :max
5
+
6
+ def initialize()
7
+ @min = nil
8
+ @max = nil
9
+ end
10
+
11
+ def load(value)
12
+ @values << value
13
+ end
14
+
15
+ def train()
16
+ # TODO.
17
+ end
18
+
19
+ def validate()
20
+ # TODO.
21
+ true
22
+ end
23
+
24
+ end
@@ -0,0 +1,27 @@
1
+ class IntegerRule < Rule
2
+
3
+ attr_accessor :min
4
+ attr_accessor :max
5
+
6
+ def initialize()
7
+ @min = nil
8
+ @max = nil
9
+ end
10
+
11
+ def load(value)
12
+ @values << value
13
+ end
14
+
15
+ def train()
16
+ numbers = @values.select {|num| num.class == Integer }
17
+ numbers.sort!
18
+ @min = numbers.first
19
+ @max = numbers.last
20
+ end
21
+
22
+ def validate()
23
+ return false if value < rule.min
24
+ return false if value > rule.max
25
+ end
26
+
27
+ end
@@ -0,0 +1,24 @@
1
+ class StringRule < Rule
2
+
3
+ attr_accessor :min_length
4
+ attr_accessor :max_length
5
+
6
+ def initialize()
7
+ @min_length = nil
8
+ @max_length = nil
9
+ end
10
+
11
+ def load(value)
12
+ @values << value
13
+ end
14
+
15
+ def train()
16
+ # TODO.
17
+ end
18
+
19
+ def validate()
20
+ # TODO.
21
+ true
22
+ end
23
+
24
+ end
@@ -32,7 +32,7 @@
32
32
 
33
33
  function getData() {
34
34
 
35
- var data = JSON.parse(<%= @@reflekt.json %>);
35
+ var data = JSON.parse(<%= json %>);
36
36
  var results = {};
37
37
 
38
38
  console.log("DATA:");
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.3
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-10-04 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
@@ -34,10 +34,15 @@ files:
34
34
  - lib/Control.rb
35
35
  - lib/Execution.rb
36
36
  - lib/Reflection.rb
37
+ - lib/Reflekt.rb
38
+ - lib/Renderer.rb
37
39
  - lib/Rule.rb
40
+ - lib/RulePool.rb
38
41
  - lib/Ruler.rb
39
42
  - lib/ShadowStack.rb
40
- - lib/reflekt.rb
43
+ - lib/rules/FloatRule.rb
44
+ - lib/rules/IntegerRule.rb
45
+ - lib/rules/StringRule.rb
41
46
  - lib/web/script.js
42
47
  - lib/web/style.css
43
48
  - lib/web/template.html.erb