Class: Reflekt::IntegerRule

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

Instance Attribute Summary

Attributes inherited from Rule

#type

Instance Method Summary collapse

Constructor Details

#initializeIntegerRule

Returns a new instance of IntegerRule.



6
7
8
9
10
# File 'lib/rules/integer_rule.rb', line 6

def initialize()
  @type = :int
  @min = nil
  @max = nil
end

Instance Method Details

#randomObject



50
51
52
# File 'lib/rules/integer_rule.rb', line 50

def random()
  rand(@min..@max)
end

#resultObject



42
43
44
45
46
47
48
# File 'lib/rules/integer_rule.rb', line 42

def result()
  {
    :type => @type,
    :min => @min,
    :max => @max
  }
end

#test(value) ⇒ Object

Parameters:

  • value (Integer)


34
35
36
37
38
39
40
# File 'lib/rules/integer_rule.rb', line 34

def test(value)
  # Numbers only; if the value is a string then there will be no min/max.
  unless @min.nil? || @max.nil?
    return false if value < @min
    return false if value > @max
  end
end

#train(meta) ⇒ Object

Parameters:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rules/integer_rule.rb', line 15

def train(meta)
  value = meta[:value]

  if @min.nil?
    @min = value
  else
    @min = value if value < @min
  end

  if @max.nil?
    @max = value
  else
    @max = value if value > @max
  end
end