Class: StringRule

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStringRule

Returns a new instance of StringRule.



8
9
10
11
12
13
# File 'lib/rules/StringRule.rb', line 8

def initialize()

  @min_length = nil
  @max_length = nil

end

Instance Attribute Details

#max_lengthObject

Returns the value of attribute max_length.



6
7
8
# File 'lib/rules/StringRule.rb', line 6

def max_length
  @max_length
end

#min_lengthObject

Returns the value of attribute min_length.



5
6
7
# File 'lib/rules/StringRule.rb', line 5

def min_length
  @min_length
end

Instance Method Details

#resultObject



49
50
51
52
53
54
55
# File 'lib/rules/StringRule.rb', line 49

def result()
  {
    :type => :string,
    :min_length => @min_length,
    :max_length => @max_length
  }
end

#test(value) ⇒ Object

Parameters:

  • value (String)


39
40
41
42
43
44
45
46
47
# File 'lib/rules/StringRule.rb', line 39

def test(value)

  length = value.length

  return false if length < @min_length
  return false if 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
# File 'lib/rules/StringRule.rb', line 18

def train(meta)

  length = meta[:length]

  if @min_length.nil?
    @min_length = length
  else
    @min_length = length if length < @min_length
  end

  if @max_length.nil?
    @max_length = length
  else
    @max_length = length if length > @max_length
  end

end