reflekt 0.9.5 → 0.9.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/Accessor.rb +8 -7
- data/lib/Reflection.rb +24 -19
- data/lib/Reflekt.rb +28 -32
- data/lib/Rule.rb +4 -4
- data/lib/RuleSet.rb +79 -0
- data/lib/Ruler.rb +142 -69
- data/lib/rules/FloatRule.rb +5 -1
- data/lib/rules/IntegerRule.rb +11 -7
- data/lib/rules/StringRule.rb +6 -1
- metadata +2 -2
- data/lib/RulePool.rb +0 -44
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 30e9de1f6073eb71a87871e5790dee7a5da6476b4c787a31c75d772986994295
|
4
|
+
data.tar.gz: 29b671194ac45fe98d596c4aefcd7cb17a5aba511d82109968403889d7d94aef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b3701e103b4ad81c7f1e0697b5f1dd07075f7fc8f55cc1715a0e9d2b8448218b8f8c6efa3ef6334943ba369172d967879b0e9e7516b1b0e2652981147ca4f107
|
7
|
+
data.tar.gz: 278a84e1cb673170c52779aea32de1e958b1ea2f2b24dcc6cb8c1356603b07d018c4d2ff22d16b9a86f2ea03beee8462f5024be87d94c362ff5163418259234a
|
data/lib/Accessor.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
################################################################################
|
2
2
|
# ACCESSOR
|
3
3
|
#
|
4
|
-
# Access variables via one object to avoid polluting the caller class.
|
4
|
+
# Access variables via one object to avoid polluting the caller class scope.
|
5
5
|
#
|
6
|
-
#
|
7
|
-
# - @
|
6
|
+
# Some variables are not accessed via Accessor:
|
7
|
+
# - @reflekt_counts on the instance
|
8
|
+
# - @reflekt_enabled on the instance
|
8
9
|
# - @@reflekt_skipped_methods on the instance's singleton class
|
9
10
|
################################################################################
|
10
11
|
|
@@ -13,24 +14,24 @@ class Accessor
|
|
13
14
|
attr_accessor :setup
|
14
15
|
attr_accessor :db
|
15
16
|
attr_accessor :stack
|
17
|
+
attr_accessor :ruler
|
16
18
|
attr_accessor :renderer
|
17
|
-
attr_accessor :rules
|
18
19
|
attr_accessor :path
|
19
20
|
attr_accessor :output_path
|
20
21
|
attr_accessor :reflect_amount
|
21
|
-
attr_accessor :
|
22
|
+
attr_accessor :reflect_limit
|
22
23
|
|
23
24
|
def initialize()
|
24
25
|
|
25
26
|
@setup = nil
|
26
27
|
@db = nil
|
27
28
|
@stack = nil
|
29
|
+
@ruler = nil
|
28
30
|
@renderer = nil
|
29
|
-
@rules = nil
|
30
31
|
@path = nil
|
31
32
|
@output_path = nil
|
32
33
|
@reflect_amount = nil
|
33
|
-
@
|
34
|
+
@reflect_limit = nil
|
34
35
|
|
35
36
|
end
|
36
37
|
|
data/lib/Reflection.rb
CHANGED
@@ -15,21 +15,32 @@ class Reflection
|
|
15
15
|
|
16
16
|
attr_accessor :clone
|
17
17
|
|
18
|
-
|
18
|
+
##
|
19
|
+
# Create a Reflection.
|
20
|
+
#
|
21
|
+
# @param Execution execution - The Execution that created this Reflection.
|
22
|
+
# @param Symbol klass - The class of the method being called.
|
23
|
+
# @param Symbol method - The method that is being called.
|
24
|
+
# @param Ruler ruler - The RuleSets for this class/method.
|
25
|
+
##
|
26
|
+
def initialize(execution, klass, method, ruler)
|
19
27
|
|
20
28
|
@execution = execution
|
29
|
+
@klass = klass
|
21
30
|
@method = method
|
22
31
|
@ruler = ruler
|
23
32
|
|
33
|
+
# Arguments.
|
34
|
+
@inputs = []
|
35
|
+
@output = nil
|
36
|
+
|
24
37
|
# Clone the execution's object.
|
25
38
|
@clone = execution.object.clone
|
26
39
|
@clone_id = nil
|
27
40
|
|
28
41
|
# Result.
|
29
|
-
@status =
|
42
|
+
@status = PASS
|
30
43
|
@time = Time.now.to_i
|
31
|
-
@inputs = []
|
32
|
-
@output = nil
|
33
44
|
|
34
45
|
end
|
35
46
|
|
@@ -38,13 +49,16 @@ class Reflection
|
|
38
49
|
#
|
39
50
|
# Creates a shadow execution stack.
|
40
51
|
#
|
41
|
-
# @param
|
42
|
-
# @param *args - The method arguments.
|
52
|
+
# @param *args - The method's arguments.
|
43
53
|
#
|
44
54
|
# @return - A reflection hash.
|
45
55
|
##
|
46
56
|
def reflect(*args)
|
47
57
|
|
58
|
+
# Get RuleSets.
|
59
|
+
input_rule_sets = @ruler.get_input_rule_sets(@klass, @method)
|
60
|
+
output_rule_set = @ruler.get_output_rule_set(@klass, @method)
|
61
|
+
|
48
62
|
# Create deviated arguments.
|
49
63
|
args.each do |arg|
|
50
64
|
case arg
|
@@ -59,10 +73,8 @@ class Reflection
|
|
59
73
|
begin
|
60
74
|
|
61
75
|
# Validate input with controls.
|
62
|
-
unless
|
63
|
-
|
64
|
-
@status = PASS
|
65
|
-
else
|
76
|
+
unless input_rule_sets.nil?
|
77
|
+
unless @ruler.validate_inputs(@inputs, input_rule_sets)
|
66
78
|
@status = FAIL
|
67
79
|
end
|
68
80
|
end
|
@@ -71,23 +83,16 @@ class Reflection
|
|
71
83
|
@output = @clone.send(@method, *@inputs)
|
72
84
|
|
73
85
|
# Validate output with controls.
|
74
|
-
unless
|
75
|
-
|
76
|
-
@status = PASS
|
77
|
-
else
|
86
|
+
unless output_rule_set.nil?
|
87
|
+
unless @ruler.validate_output(@output, output_rule_set)
|
78
88
|
@status = FAIL
|
79
89
|
end
|
80
|
-
return
|
81
90
|
end
|
82
91
|
|
83
92
|
# When fail.
|
84
93
|
rescue StandardError => message
|
85
94
|
@status = FAIL
|
86
95
|
@message = message
|
87
|
-
|
88
|
-
# When no validation and execution fails.
|
89
|
-
else
|
90
|
-
@status = PASS
|
91
96
|
end
|
92
97
|
|
93
98
|
end
|
data/lib/Reflekt.rb
CHANGED
@@ -26,7 +26,7 @@ module Reflekt
|
|
26
26
|
|
27
27
|
def initialize(*args)
|
28
28
|
|
29
|
-
@
|
29
|
+
@reflekt_counts = {}
|
30
30
|
|
31
31
|
# Get instance methods.
|
32
32
|
# TODO: Include parent methods like "Array.include?".
|
@@ -35,13 +35,13 @@ module Reflekt
|
|
35
35
|
# Don't process skipped methods.
|
36
36
|
next if self.class.reflekt_skipped?(method)
|
37
37
|
|
38
|
-
@
|
38
|
+
@reflekt_counts[method] = 0
|
39
39
|
|
40
40
|
# When method called in flow.
|
41
41
|
self.define_singleton_method(method) do |*args|
|
42
42
|
|
43
43
|
# Don't reflect when limit reached.
|
44
|
-
unless @
|
44
|
+
unless @reflekt_counts[method] >= @@reflekt.reflect_limit
|
45
45
|
|
46
46
|
# Get current execution.
|
47
47
|
execution = @@reflekt.stack.peek()
|
@@ -55,25 +55,20 @@ module Reflekt
|
|
55
55
|
|
56
56
|
end
|
57
57
|
|
58
|
-
# Get ruler.
|
59
|
-
# The method's ruler will not exist the first time the db generated.
|
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]
|
62
|
-
else
|
63
|
-
ruler = nil
|
64
|
-
end
|
65
|
-
|
66
58
|
# Reflect.
|
67
59
|
# The first method call in the Execution creates a Reflection.
|
68
60
|
# Subsequent method calls are shadow executions on cloned objects.
|
69
61
|
if execution.has_empty_reflections? && !execution.is_reflecting?
|
70
62
|
execution.is_reflecting = true
|
71
63
|
|
64
|
+
# Use symbols "klass" and "method" as keys in hashes.
|
65
|
+
# Use strings "class_name" and "method_name" to query the database.
|
72
66
|
class_name = execution.caller_class.to_s
|
73
67
|
method_name = method.to_s
|
68
|
+
klass = class_name.to_sym
|
74
69
|
|
75
70
|
# Create control.
|
76
|
-
control = Control.new(execution, method, ruler)
|
71
|
+
control = Control.new(execution, klass, method, @@reflekt.ruler)
|
77
72
|
execution.control = control
|
78
73
|
|
79
74
|
# Execute control.
|
@@ -86,12 +81,12 @@ module Reflekt
|
|
86
81
|
execution.reflections.each_with_index do |value, index|
|
87
82
|
|
88
83
|
# Create reflection.
|
89
|
-
reflection = Reflection.new(execution, method, ruler)
|
84
|
+
reflection = Reflection.new(execution, klass, method, @@reflekt.ruler)
|
90
85
|
execution.reflections[index] = reflection
|
91
86
|
|
92
87
|
# Execute reflection.
|
93
88
|
reflection.reflect(*args)
|
94
|
-
@
|
89
|
+
@reflekt_counts[method] = @reflekt_counts[method] + 1
|
95
90
|
|
96
91
|
# Save reflection.
|
97
92
|
@@reflekt.db.get("#{class_name}.#{method_name}.reflections").push(reflection.result())
|
@@ -163,24 +158,25 @@ module Reflekt
|
|
163
158
|
# Create shadow execution stack.
|
164
159
|
@@reflekt.stack = ShadowStack.new()
|
165
160
|
|
166
|
-
#
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
class_values.each do |method_name,
|
173
|
-
next if
|
174
|
-
next unless
|
175
|
-
if
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
ruler.
|
180
|
-
|
181
|
-
|
161
|
+
# Create rules.
|
162
|
+
@@reflekt.ruler = Ruler.new()
|
163
|
+
# TODO: Fix Rowdb.get(path) not returning values at path after Rowdb.push()?
|
164
|
+
values = @@reflekt.db.value()
|
165
|
+
values.each do |klass, class_values|
|
166
|
+
|
167
|
+
class_values.each do |method_name, method_items|
|
168
|
+
next if method_items.nil?
|
169
|
+
next unless method_items.class == Hash
|
170
|
+
if method_items.key? "controls"
|
171
|
+
|
172
|
+
method = method_name.to_sym
|
173
|
+
|
174
|
+
@@reflekt.ruler.load(klass, method, method_items['controls'])
|
175
|
+
@@reflekt.ruler.train(klass, method)
|
176
|
+
|
182
177
|
end
|
183
178
|
end
|
179
|
+
|
184
180
|
end
|
185
181
|
|
186
182
|
# The amount of reflections to create per method call.
|
@@ -188,7 +184,7 @@ module Reflekt
|
|
188
184
|
|
189
185
|
# Limit the amount of reflections that can be created per instance method.
|
190
186
|
# A method called thousands of times doesn't need that many reflections.
|
191
|
-
@@reflekt.
|
187
|
+
@@reflekt.reflect_limit = 10
|
192
188
|
|
193
189
|
# Create renderer.
|
194
190
|
@@reflekt.renderer = Renderer.new(@@reflekt.path, @@reflekt.output_path)
|
@@ -215,7 +211,7 @@ module Reflekt
|
|
215
211
|
end
|
216
212
|
|
217
213
|
def reflekt_limit(amount)
|
218
|
-
@@reflekt.
|
214
|
+
@@reflekt.reflect_limit = amount
|
219
215
|
end
|
220
216
|
|
221
217
|
end
|
data/lib/Rule.rb
CHANGED
@@ -11,7 +11,7 @@ class Rule
|
|
11
11
|
|
12
12
|
# Each rule intitalises itself.
|
13
13
|
def initialize()
|
14
|
-
@values =
|
14
|
+
@values = []
|
15
15
|
end
|
16
16
|
|
17
17
|
# Each rule loads up an array of values.
|
@@ -19,13 +19,13 @@ class Rule
|
|
19
19
|
@values << value
|
20
20
|
end
|
21
21
|
|
22
|
-
# Each rule trains on values to determine its
|
22
|
+
# Each rule trains on values to determine its boundaries.
|
23
23
|
def train()
|
24
24
|
|
25
25
|
end
|
26
26
|
|
27
|
-
# Each rule compares the
|
28
|
-
def validate()
|
27
|
+
# Each rule compares the values it's given with its boundaries.
|
28
|
+
def validate(value)
|
29
29
|
|
30
30
|
end
|
31
31
|
|
data/lib/RuleSet.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
################################################################################
|
2
|
+
# RULE POOL
|
3
|
+
#
|
4
|
+
# A collection of unique rules that validate an argument.
|
5
|
+
################################################################################
|
6
|
+
|
7
|
+
require 'set'
|
8
|
+
require 'rules/FloatRule'
|
9
|
+
require 'rules/IntegerRule'
|
10
|
+
require 'rules/StringRule'
|
11
|
+
|
12
|
+
class RuleSet
|
13
|
+
|
14
|
+
attr_accessor :types
|
15
|
+
attr_accessor :rules
|
16
|
+
|
17
|
+
def initialize()
|
18
|
+
|
19
|
+
@types = Set.new
|
20
|
+
@rules = {}
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
def load(type, value)
|
25
|
+
|
26
|
+
# Track data type.
|
27
|
+
@types << type
|
28
|
+
|
29
|
+
# Get rule for this data type.
|
30
|
+
rule = nil
|
31
|
+
|
32
|
+
case type
|
33
|
+
when "Integer"
|
34
|
+
unless @rules.key? IntegerRule
|
35
|
+
rule = IntegerRule.new()
|
36
|
+
@rules[IntegerRule] = rule
|
37
|
+
else
|
38
|
+
rule = @rules[IntegerRule]
|
39
|
+
end
|
40
|
+
when "String"
|
41
|
+
unless @rules.key? StringRule
|
42
|
+
rule = StringRule.new()
|
43
|
+
@rules[StringRule] = rule
|
44
|
+
else
|
45
|
+
rule = @rules[IntegerRule]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Add value to rule.
|
50
|
+
unless rule.nil?
|
51
|
+
rule.load(value)
|
52
|
+
end
|
53
|
+
|
54
|
+
return self
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
def train()
|
59
|
+
|
60
|
+
@rules.each do |klass, rule|
|
61
|
+
rule.train()
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
def validate_rule(value)
|
67
|
+
result = true
|
68
|
+
|
69
|
+
@rules.each do |klass, rule|
|
70
|
+
|
71
|
+
unless rule.validate(value)
|
72
|
+
result = false
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
return result
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
data/lib/Ruler.rb
CHANGED
@@ -1,124 +1,197 @@
|
|
1
1
|
################################################################################
|
2
2
|
# RULER
|
3
3
|
#
|
4
|
-
#
|
4
|
+
# Aggregates input/output from controls.
|
5
|
+
# Creates and trains RuleSets from aggregated input/output.
|
6
|
+
# Validates reflection input/output against RuleSets.
|
5
7
|
################################################################################
|
6
8
|
|
7
|
-
require '
|
9
|
+
require 'RuleSet'
|
8
10
|
|
9
11
|
class Ruler
|
10
12
|
|
11
|
-
|
13
|
+
INPUTS = "i"
|
12
14
|
OUTPUT = "o"
|
13
15
|
TYPE = "T"
|
14
16
|
VALUE = "V"
|
15
17
|
|
16
18
|
def initialize()
|
19
|
+
@rule_sets = {}
|
20
|
+
end
|
17
21
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
22
|
+
##
|
23
|
+
# Get input RuleSets.
|
24
|
+
#
|
25
|
+
# @param Symbol klass
|
26
|
+
# @param Symbol method
|
27
|
+
#
|
28
|
+
# @return Array
|
29
|
+
##
|
30
|
+
def get_input_rule_sets(klass, method)
|
31
|
+
return @rule_sets.dig(klass, method, :inputs)
|
32
|
+
end
|
23
33
|
|
34
|
+
##
|
35
|
+
# Get input RuleSet.
|
36
|
+
#
|
37
|
+
# @param Symbol klass
|
38
|
+
# @param Symbol method
|
39
|
+
#
|
40
|
+
# @return RuleSet
|
41
|
+
##
|
42
|
+
def get_input_rule_set(klass, method, arg_num)
|
43
|
+
@rule_sets.dig(klass, method, :inputs, arg_num)
|
24
44
|
end
|
25
45
|
|
26
|
-
|
46
|
+
##
|
47
|
+
# Get output RuleSet.
|
48
|
+
#
|
49
|
+
# @param Symbol klass
|
50
|
+
# @param Symbol method
|
51
|
+
#
|
52
|
+
# @return RuleSet.
|
53
|
+
##
|
54
|
+
def get_output_rule_set(klass, method)
|
55
|
+
@rule_sets.dig(klass, method, :output)
|
56
|
+
end
|
27
57
|
|
28
|
-
|
29
|
-
|
58
|
+
##
|
59
|
+
# Set input RuleSet.
|
60
|
+
#
|
61
|
+
# @param Symbol klass
|
62
|
+
# @param Symbol method
|
63
|
+
##
|
64
|
+
def set_input_rule_set(klass, method, arg_num, rule_set)
|
65
|
+
# Set defaults.
|
66
|
+
@rule_sets[klass] = {} unless @rule_sets.key? klass
|
67
|
+
@rule_sets[klass][method] = {} unless @rule_sets[klass].key? method
|
68
|
+
@rule_sets[klass][method][:inputs] = [] unless @rule_sets[klass][method].key? :inputs
|
69
|
+
# Set value.
|
70
|
+
@rule_sets[klass][method][:inputs][arg_num] = rule_set
|
71
|
+
end
|
30
72
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
73
|
+
##
|
74
|
+
# Set output RuleSet.
|
75
|
+
#
|
76
|
+
# @param Symbol klass
|
77
|
+
# @param Symbol method
|
78
|
+
# @param RuleSet rule_set
|
79
|
+
##
|
80
|
+
def set_output_rule_set(klass, method, rule_set)
|
81
|
+
# Set defaults.
|
82
|
+
@rule_sets[klass] = {} unless @rule_sets.key? klass
|
83
|
+
@rule_sets[klass][method] = {} unless @rule_sets[klass].key? method
|
84
|
+
# Set value.
|
85
|
+
@rule_sets[klass][method][:output] = rule_set
|
86
|
+
end
|
87
|
+
|
88
|
+
##
|
89
|
+
# Load RuleSets.
|
90
|
+
#
|
91
|
+
# @param Symbol klass
|
92
|
+
# @param Symbol method
|
93
|
+
# @param Array controls
|
94
|
+
##
|
95
|
+
def load(klass, method, controls)
|
96
|
+
|
97
|
+
# Create a RuleSet for each control's inputs/output.
|
98
|
+
controls.each_with_index do |control, index|
|
99
|
+
|
100
|
+
# Process inputs.
|
101
|
+
control[INPUTS].each_with_index do |input, arg_num|
|
102
|
+
rule_set = get_input_rule_set(klass, method, arg_num)
|
103
|
+
if rule_set.nil?
|
104
|
+
rule_set = RuleSet.new()
|
105
|
+
set_input_rule_set(klass, method, arg_num, rule_set)
|
35
106
|
end
|
107
|
+
rule_set.load(input[TYPE], input[VALUE])
|
36
108
|
end
|
37
109
|
|
38
|
-
#
|
39
|
-
|
40
|
-
|
41
|
-
|
110
|
+
# Process output.
|
111
|
+
output_rule_set = get_output_rule_set(klass, method)
|
112
|
+
if output_rule_set.nil?
|
113
|
+
output_rule_set = RuleSet.new()
|
114
|
+
set_output_rule_set(klass, method, output_rule_set)
|
42
115
|
end
|
116
|
+
output_rule_set.load(control[OUTPUT][TYPE], control[OUTPUT][VALUE])
|
43
117
|
|
44
118
|
end
|
45
119
|
|
46
120
|
end
|
47
121
|
|
48
|
-
|
122
|
+
##
|
123
|
+
# Train RuleSets from controls.
|
124
|
+
#
|
125
|
+
# @param Symbol klass
|
126
|
+
# @param Symbol method
|
127
|
+
##
|
128
|
+
def train(klass, method)
|
129
|
+
|
130
|
+
input_rule_sets = get_input_rule_sets(klass, method)
|
131
|
+
unless input_rule_sets.nil?
|
132
|
+
input_rule_sets.each do |input_rule_set|
|
133
|
+
input_rule_set.train()
|
134
|
+
end
|
135
|
+
end
|
49
136
|
|
50
|
-
|
51
|
-
|
137
|
+
output_rule_set = get_output_rule_set(klass, method)
|
138
|
+
unless output_rule_set.nil?
|
139
|
+
output_rule_set.train()
|
52
140
|
end
|
53
141
|
|
54
|
-
|
55
|
-
rule_pool.types << type
|
142
|
+
end
|
56
143
|
|
57
|
-
|
58
|
-
|
144
|
+
##
|
145
|
+
# Validate inputs.
|
146
|
+
#
|
147
|
+
# @param Array inputs - The method's arguments.
|
148
|
+
# @param Array input_rule_sets - The RuleSets to validate each input with.
|
149
|
+
##
|
150
|
+
def validate_inputs(inputs, input_rule_sets)
|
59
151
|
|
60
|
-
|
61
|
-
|
62
|
-
unless rule_pool.rules.key? IntegerRule
|
63
|
-
rule = IntegerRule.new()
|
64
|
-
rule_pool.rules << rule
|
65
|
-
else
|
66
|
-
rule = rule_pool.rules[IntegerRule]
|
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
|
152
|
+
# Default to a PASS result.
|
153
|
+
result = true
|
76
154
|
|
77
|
-
#
|
78
|
-
|
79
|
-
rule.load(value)
|
80
|
-
end
|
155
|
+
# Validate each argument against each rule set for that argument.
|
156
|
+
inputs.each_with_index do |input, arg_num|
|
81
157
|
|
82
|
-
|
158
|
+
unless input_rule_sets[arg_num].nil?
|
83
159
|
|
84
|
-
|
160
|
+
rule_set = input_rule_sets[arg_num]
|
85
161
|
|
86
|
-
|
162
|
+
unless rule_set.validate_rule(input)
|
163
|
+
result = false
|
164
|
+
end
|
87
165
|
|
88
|
-
@inputs.each do |rule_pool|
|
89
|
-
unless rule_pool.nil?
|
90
|
-
rule_pool.train()
|
91
166
|
end
|
92
167
|
end
|
93
168
|
|
94
|
-
|
95
|
-
@output.train()
|
96
|
-
end
|
169
|
+
return result
|
97
170
|
|
98
171
|
end
|
99
172
|
|
100
|
-
|
173
|
+
##
|
174
|
+
# Validate output.
|
175
|
+
#
|
176
|
+
# @param dynamic output - The method's return value.
|
177
|
+
# @param RuleSet output_rule_set - The RuleSet to validate the output with.
|
178
|
+
##
|
179
|
+
def validate_output(output, output_rule_set)
|
180
|
+
|
181
|
+
# Default to a PASS result.
|
101
182
|
result = true
|
102
183
|
|
103
|
-
|
104
|
-
|
105
|
-
|
184
|
+
unless output_rule_set.nil?
|
185
|
+
|
186
|
+
# Validate output RuleSet for that argument.
|
187
|
+
unless output_rule_set.validate_rule(output)
|
106
188
|
result = false
|
107
189
|
end
|
108
|
-
end
|
109
190
|
|
110
|
-
return result
|
111
|
-
end
|
112
|
-
|
113
|
-
def validate_output(output)
|
114
|
-
result = true
|
115
|
-
|
116
|
-
rule_pool = @output
|
117
|
-
unless rule_pool.validate(output)
|
118
|
-
result = false
|
119
191
|
end
|
120
192
|
|
121
193
|
return result
|
194
|
+
|
122
195
|
end
|
123
196
|
|
124
197
|
end
|
data/lib/rules/FloatRule.rb
CHANGED
@@ -1,9 +1,13 @@
|
|
1
|
+
require 'Rule'
|
2
|
+
|
1
3
|
class FloatRule < Rule
|
2
4
|
|
3
5
|
attr_accessor :min
|
4
6
|
attr_accessor :max
|
5
7
|
|
6
8
|
def initialize()
|
9
|
+
super
|
10
|
+
|
7
11
|
@min = nil
|
8
12
|
@max = nil
|
9
13
|
end
|
@@ -16,7 +20,7 @@ class FloatRule < Rule
|
|
16
20
|
# TODO.
|
17
21
|
end
|
18
22
|
|
19
|
-
def validate()
|
23
|
+
def validate(value)
|
20
24
|
# TODO.
|
21
25
|
true
|
22
26
|
end
|
data/lib/rules/IntegerRule.rb
CHANGED
@@ -1,15 +1,16 @@
|
|
1
|
-
|
1
|
+
require 'Rule'
|
2
2
|
|
3
|
-
|
4
|
-
attr_accessor :max
|
3
|
+
class IntegerRule < Rule
|
5
4
|
|
6
5
|
def initialize()
|
7
6
|
@min = nil
|
8
7
|
@max = nil
|
8
|
+
|
9
|
+
super
|
9
10
|
end
|
10
11
|
|
11
12
|
def load(value)
|
12
|
-
@values << value
|
13
|
+
@values << value.to_i
|
13
14
|
end
|
14
15
|
|
15
16
|
def train()
|
@@ -19,9 +20,12 @@ class IntegerRule < Rule
|
|
19
20
|
@max = numbers.last
|
20
21
|
end
|
21
22
|
|
22
|
-
def validate()
|
23
|
-
|
24
|
-
return false if value
|
23
|
+
def validate(value)
|
24
|
+
|
25
|
+
return false if value < @min
|
26
|
+
return false if value > @max
|
27
|
+
|
28
|
+
true
|
25
29
|
end
|
26
30
|
|
27
31
|
end
|
data/lib/rules/StringRule.rb
CHANGED
@@ -1,11 +1,16 @@
|
|
1
|
+
require 'Rule'
|
2
|
+
|
1
3
|
class StringRule < Rule
|
2
4
|
|
3
5
|
attr_accessor :min_length
|
4
6
|
attr_accessor :max_length
|
5
7
|
|
6
8
|
def initialize()
|
9
|
+
super
|
10
|
+
|
7
11
|
@min_length = nil
|
8
12
|
@max_length = nil
|
13
|
+
|
9
14
|
end
|
10
15
|
|
11
16
|
def load(value)
|
@@ -16,7 +21,7 @@ class StringRule < Rule
|
|
16
21
|
# TODO.
|
17
22
|
end
|
18
23
|
|
19
|
-
def validate()
|
24
|
+
def validate(value)
|
20
25
|
# TODO.
|
21
26
|
true
|
22
27
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maedi Prichard
|
@@ -37,7 +37,7 @@ files:
|
|
37
37
|
- lib/Reflekt.rb
|
38
38
|
- lib/Renderer.rb
|
39
39
|
- lib/Rule.rb
|
40
|
-
- lib/
|
40
|
+
- lib/RuleSet.rb
|
41
41
|
- lib/Ruler.rb
|
42
42
|
- lib/ShadowStack.rb
|
43
43
|
- lib/rules/FloatRule.rb
|
data/lib/RulePool.rb
DELETED
@@ -1,44 +0,0 @@
|
|
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
|