Class: ArrayRule
Instance Attribute Summary
Attributes inherited from Rule
Instance Method Summary collapse
-
#initialize ⇒ ArrayRule
constructor
A new instance of ArrayRule.
- #random ⇒ Object
- #result ⇒ Object
- #test(value) ⇒ Object
- #train(meta) ⇒ Object
Constructor Details
#initialize ⇒ ArrayRule
Returns a new instance of ArrayRule.
5 6 7 8 9 10 11 12 13 |
# File 'lib/rules/ArrayRule.rb', line 5 def initialize() @type = :array @min = nil @max = nil @min_length = nil @max_length = nil end |
Instance Method Details
#random ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rules/ArrayRule.rb', line 76 def random() array = Array.new(rand(@min_length..@max_length)) array.each_with_index do |item, index| array[index] = rand(@min..@max) end return array end |
#result ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/rules/ArrayRule.rb', line 66 def result() { :type => @type, :min => @min, :max => @max, :min_length => @min_length, :max_length => @max_length } end |
#test(value) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rules/ArrayRule.rb', line 53 def test(value) # Min/max value. return false if value.min() < @min return false if value.max() > @max # Min/max length. return false if value.length < @min_length return false if value.length > @max_length true end |
#train(meta) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rules/ArrayRule.rb', line 18 def train() # Min value. if @min.nil? @min = [:min] else @min = [:min] if [:min] < @min end # Max value. if @max.nil? @max = [:max] else @max = [:max] if [:max] > @max end # Min length. if @min_length.nil? @min_length = [:length] else @min_length = [:length] if [:length] < @min_length end # Max length. if @max_length.nil? @max_length = [:length] else @max_length = [:length] if [:length] > @max_length end end |