Class: Stattleship::Validators::BaseValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/stattleship/validators/base_validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(key:, params:, class_name:) ⇒ BaseValidator

Returns a new instance of BaseValidator.


4
5
6
7
8
# File 'lib/stattleship/validators/base_validator.rb', line 4

def initialize(key:, params:, class_name:)
  @class_name = class_name
  @key = key
  @params = params
end

Instance Method Details

#validateObject


10
11
12
# File 'lib/stattleship/validators/base_validator.rb', line 10

def validate
  raise 'Validator subclass must implement validate method'
end

#validates_dateObject


14
15
16
17
18
19
20
# File 'lib/stattleship/validators/base_validator.rb', line 14

def validates_date
  if value.is_a?(Date)
    return
  else
    fail ArgumentError.new("expected #{key} to be a Date")
  end
end

#validates_league_prefixObject


38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/stattleship/validators/base_validator.rb', line 38

def validates_league_prefix
  if blank?(value)
    return
  elsif blank?(league_prefix)
    return
  else
    if value.start_with?("#{league_prefix}-")
      return
    end
  end

  fail ArgumentError.
    new("expected #{key} to start with '#{league_prefix}'")
end

#validates_positive_integerObject


30
31
32
33
34
35
36
# File 'lib/stattleship/validators/base_validator.rb', line 30

def validates_positive_integer
  if value.is_a?(Integer) && value > 0
    return
  else
    fail ArgumentError.new("expected #{key} to be an integer > 0")
  end
end

#validates_positive_or_zero_integerObject


22
23
24
25
26
27
28
# File 'lib/stattleship/validators/base_validator.rb', line 22

def validates_positive_or_zero_integer
  if value.is_a?(Integer) && value >= 0
    return
  else
    fail ArgumentError.new("expected #{key} to be an integer >= 0")
  end
end

#validates_sport_prefixObject


53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/stattleship/validators/base_validator.rb', line 53

def validates_sport_prefix
  if blank?(value)
    return
  elsif blank?(sport_prefix)
    return
  else
    if value.start_with?("#{sport_prefix}") || value.start_with?("#{sport_prefix}_")
      return
    end
  end

  fail ArgumentError.
    new("expected #{key} to start with '#{sport_prefix}'")
end