Class: ArrayRule

Inherits:
Rule
  • Object
show all
Defined in:
lib/rules/ArrayRule.rb

Instance Attribute Summary

Attributes inherited from Rule

#type

Instance Method Summary collapse

Constructor Details

#initializeArrayRule

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

#randomObject



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

#resultObject



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

Parameters:

  • value (Array)


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

Parameters:



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(meta)

  # Min value.
  if @min.nil?
    @min = meta[:min]
  else
    @min = meta[:min] if meta[:min] < @min
  end

  # Max value.
  if @max.nil?
    @max = meta[:max]
  else
    @max = meta[:max] if meta[:max] > @max
  end

  # Min length.
  if @min_length.nil?
    @min_length = meta[:length]
  else
    @min_length = meta[:length] if meta[:length] < @min_length
  end

  # Max length.
  if @max_length.nil?
    @max_length = meta[:length]
  else
    @max_length = meta[:length] if meta[:length] > @max_length
  end

end