reflekt 1.0.9 → 1.0.10

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.
@@ -1,90 +1,94 @@
1
1
  require_relative '../rule'
2
2
 
3
3
  module Reflekt
4
- class ArrayRule < Rule
5
-
6
- def initialize()
7
-
8
- @type = :array
9
- @min = nil
10
- @max = nil
11
- @min_length = nil
12
- @max_length = nil
13
-
14
- end
15
-
16
- ##
17
- # @param meta [ArrayMeta]
18
- ##
19
- def train(meta)
20
-
21
- # Min value.
22
- if @min.nil?
23
- @min = meta[:min]
24
- else
25
- @min = meta[:min] if meta[:min] < @min
4
+ class ArrayRule < Rule
5
+
6
+ def initialize()
7
+ @type = :array
8
+ @min = nil
9
+ @max = nil
10
+ @min_length = nil
11
+ @max_length = nil
26
12
  end
27
13
 
28
- # Max value.
29
- if @max.nil?
30
- @max = meta[:max]
31
- else
32
- @max = meta[:max] if meta[:max] > @max
14
+ ##
15
+ # @param meta [ArrayMeta]
16
+ ##
17
+ def train(meta)
18
+ if Meta.numeric? meta[:min]
19
+ # Min value.
20
+ meta_min = meta[:min].to_i
21
+ if @min.nil?
22
+ @min = meta_min
23
+ else
24
+ @min = meta_min if meta_min < @min
25
+ end
26
+
27
+ # Max value.
28
+ meta_max = meta[:max].to_i
29
+ if @max.nil?
30
+ @max = meta_max
31
+ else
32
+ @max = meta_max if meta_max > @max
33
+ end
34
+ end
35
+
36
+ # Min length.
37
+ if @min_length.nil?
38
+ @min_length = meta[:length]
39
+ else
40
+ @min_length = meta[:length] if meta[:length] < @min_length
41
+ end
42
+
43
+ # Max length.
44
+ if @max_length.nil?
45
+ @max_length = meta[:length]
46
+ else
47
+ @max_length = meta[:length] if meta[:length] > @max_length
48
+ end
33
49
  end
34
50
 
35
- # Min length.
36
- if @min_length.nil?
37
- @min_length = meta[:length]
38
- else
39
- @min_length = meta[:length] if meta[:length] < @min_length
51
+ ##
52
+ # @param value [Array]
53
+ ##
54
+ def test(value)
55
+ # Handle empty value.
56
+ return true if value.empty? && @min_length == 0 && @max_length == 0
57
+
58
+ unless value.empty?
59
+ # Numbers only; if the value is a string then there will be no min/max.
60
+ unless @min.nil? || @max.nil?
61
+ return false if value.min() < @min
62
+ return false if value.max() > @max
63
+ end
64
+
65
+ # Min/max length.
66
+ return false if value.length < @min_length
67
+ return false if value.length > @max_length
68
+ end
69
+
70
+ true
40
71
  end
41
72
 
42
- # Max length.
43
- if @max_length.nil?
44
- @max_length = meta[:length]
45
- else
46
- @max_length = meta[:length] if meta[:length] > @max_length
73
+ def result()
74
+ {
75
+ :type => @type,
76
+ :min => @min,
77
+ :max => @max,
78
+ :min_length => @min_length,
79
+ :max_length => @max_length
80
+ }
47
81
  end
48
82
 
49
- end
50
-
51
- ##
52
- # @param value [Array]
53
- ##
54
- def test(value)
55
-
56
- # Min/max value.
57
- return false if value.min() < @min
58
- return false if value.max() > @max
83
+ def random()
84
+ array = Array.new(rand(@min_length..@max_length))
59
85
 
60
- # Min/max length.
61
- return false if value.length < @min_length
62
- return false if value.length > @max_length
86
+ array.each_with_index do |item, index|
87
+ array[index] = rand(@min..@max)
88
+ end
63
89
 
64
- true
65
- end
66
-
67
- def result()
68
- {
69
- :type => @type,
70
- :min => @min,
71
- :max => @max,
72
- :min_length => @min_length,
73
- :max_length => @max_length
74
- }
75
- end
76
-
77
- def random()
78
-
79
- array = Array.new(rand(@min_length..@max_length))
80
-
81
- array.each_with_index do |item, index|
82
- array[index] = rand(@min..@max)
90
+ return array
83
91
  end
84
92
 
85
- return array
86
-
87
93
  end
88
-
89
- end
90
94
  end
@@ -2,48 +2,42 @@ require 'set'
2
2
  require_relative '../rule'
3
3
 
4
4
  module Reflekt
5
- class BooleanRule < Rule
5
+ class BooleanRule < Rule
6
6
 
7
- def initialize()
8
-
9
- @type = :bool
10
- @booleans = Set.new()
11
-
12
- end
13
-
14
- ##
15
- # @param meta [BooleanMeta]
16
- ##
17
- def train(meta)
18
-
19
- value = meta[:value]
20
-
21
- unless value.nil?
22
- @booleans << value
7
+ def initialize()
8
+ @type = :bool
9
+ @booleans = Set.new()
23
10
  end
24
11
 
25
- end
12
+ ##
13
+ # @param meta [BooleanMeta]
14
+ ##
15
+ def train(meta)
16
+ value = meta[:value]
26
17
 
27
- ##
28
- # @param value [Boolean]
29
- ##
30
- def test(value)
18
+ unless value.nil?
19
+ @booleans << value
20
+ end
21
+ end
31
22
 
32
- # Booleans are stored as strings.
33
- @booleans.include? value.to_s
23
+ ##
24
+ # @param value [Boolean]
25
+ ##
26
+ def test(value)
27
+ # Booleans are stored as strings.
28
+ @booleans.include? value.to_s
29
+ end
34
30
 
35
- end
31
+ def result()
32
+ {
33
+ :type => @type,
34
+ :booleans => @booleans
35
+ }
36
+ end
36
37
 
37
- def result()
38
- {
39
- :type => @type,
40
- :booleans => @booleans
41
- }
42
- end
38
+ def random()
39
+ @booleans.to_a.sample
40
+ end
43
41
 
44
- def random()
45
- @booleans.to_a.sample
46
42
  end
47
-
48
- end
49
43
  end
@@ -1,59 +1,55 @@
1
1
  require_relative '../rule'
2
2
 
3
3
  module Reflekt
4
- class FloatRule < Rule
4
+ class FloatRule < Rule
5
5
 
6
- def initialize()
7
-
8
- @type = :float
9
- @min = nil
10
- @max = nil
11
-
12
- end
13
-
14
- ##
15
- # @param meta [FloatMeta]
16
- ##
17
- def train(meta)
18
-
19
- value = meta[:value]
20
-
21
- if @min.nil?
22
- @min = value
23
- else
24
- @min = value if value < @min
6
+ def initialize()
7
+ @type = :float
8
+ @min = nil
9
+ @max = nil
25
10
  end
26
11
 
27
- if @max.nil?
28
- @max = value
29
- else
30
- @max = value if value > @max
12
+ ##
13
+ # @param meta [FloatMeta]
14
+ ##
15
+ def train(meta)
16
+ value = meta[:value]
17
+
18
+ if @min.nil?
19
+ @min = value
20
+ else
21
+ @min = value if value < @min
22
+ end
23
+
24
+ if @max.nil?
25
+ @max = value
26
+ else
27
+ @max = value if value > @max
28
+ end
31
29
  end
32
30
 
33
- end
34
-
35
- ##
36
- # @param value [Float]
37
- ##
38
- def test(value)
39
-
40
- return false if value < @min
41
- return false if value > @max
31
+ ##
32
+ # @param value [Float]
33
+ ##
34
+ def test(value)
35
+ # Numbers only; if the value is a string then there will be no min/max.
36
+ unless @min.nil? || @max.nil?
37
+ return false if value < @min
38
+ return false if value > @max
39
+ end
40
+ end
42
41
 
43
- true
44
- end
42
+ def result()
43
+ {
44
+ :type => @type,
45
+ :min => @min,
46
+ :max => @max
47
+ }
48
+ end
45
49
 
46
- def result()
47
- {
48
- :type => @type,
49
- :min => @min,
50
- :max => @max
51
- }
52
- end
50
+ def random()
51
+ rand(@min..@max)
52
+ end
53
53
 
54
- def random()
55
- rand(@min..@max)
56
54
  end
57
-
58
- end
59
55
  end
@@ -1,59 +1,55 @@
1
1
  require_relative '../rule'
2
2
 
3
3
  module Reflekt
4
- class IntegerRule < Rule
4
+ class IntegerRule < Rule
5
5
 
6
- def initialize()
7
-
8
- @type = :int
9
- @min = nil
10
- @max = nil
11
-
12
- end
13
-
14
- ##
15
- # @param meta [IntegerMeta]
16
- ##
17
- def train(meta)
18
-
19
- value = meta[:value]
20
-
21
- if @min.nil?
22
- @min = value
23
- else
24
- @min = value if value < @min
6
+ def initialize()
7
+ @type = :int
8
+ @min = nil
9
+ @max = nil
25
10
  end
26
11
 
27
- if @max.nil?
28
- @max = value
29
- else
30
- @max = value if value > @max
12
+ ##
13
+ # @param meta [IntegerMeta]
14
+ ##
15
+ def train(meta)
16
+ value = meta[:value]
17
+
18
+ if @min.nil?
19
+ @min = value
20
+ else
21
+ @min = value if value < @min
22
+ end
23
+
24
+ if @max.nil?
25
+ @max = value
26
+ else
27
+ @max = value if value > @max
28
+ end
31
29
  end
32
30
 
33
- end
34
-
35
- ##
36
- # @param value [Integer]
37
- ##
38
- def test(value)
39
-
40
- return false if value < @min
41
- return false if value > @max
31
+ ##
32
+ # @param value [Integer]
33
+ ##
34
+ def test(value)
35
+ # Numbers only; if the value is a string then there will be no min/max.
36
+ unless @min.nil? || @max.nil?
37
+ return false if value < @min
38
+ return false if value > @max
39
+ end
40
+ end
42
41
 
43
- true
44
- end
42
+ def result()
43
+ {
44
+ :type => @type,
45
+ :min => @min,
46
+ :max => @max
47
+ }
48
+ end
45
49
 
46
- def result()
47
- {
48
- :type => @type,
49
- :min => @min,
50
- :max => @max
51
- }
52
- end
50
+ def random()
51
+ rand(@min..@max)
52
+ end
53
53
 
54
- def random()
55
- rand(@min..@max)
56
54
  end
57
-
58
- end
59
55
  end