Module: ComicInfo::Enums::Validators

Defined in:
lib/comicinfo/enums.rb

Overview

Validation methods

Class Method Summary collapse

Class Method Details

.validate_age_rating(value) ⇒ Object

Validates AgeRating enum values



74
75
76
77
78
79
80
# File 'lib/comicinfo/enums.rb', line 74

def self.validate_age_rating value
  return DEFAULT_ENUM_UNKNOWN if value.nil? || value.empty?

  raise Errors::InvalidEnumError.new('AgeRating', value, AGE_RATING_VALUES) unless AGE_RATING_VALUES.include?(value)

  value
end

.validate_comic_page_type(value) ⇒ Object

Validates ComicPageType enum values



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/comicinfo/enums.rb', line 83

def self.validate_comic_page_type value
  return DEFAULT_PAGE_TYPE if value.nil? || value.empty?

  # Handle space-separated list of values
  values = value.split(/\s+/)
  values.each do |v|
    unless COMIC_PAGE_TYPE_VALUES.include?(v)
      raise Errors::InvalidEnumError.new('ComicPageType', v, COMIC_PAGE_TYPE_VALUES)
    end
  end

  value
end

.validate_community_rating(value) ⇒ Object

Validates CommunityRating decimal range (0.0 to 5.0)



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/comicinfo/enums.rb', line 98

def self.validate_community_rating value
  return nil if value.nil? || value.empty?

  begin
    rating = Float(value)
    raise Errors::RangeError.new('CommunityRating', rating, 0.0, 5.0) if rating < 0.0 || rating > 5.0

    rating
  rescue ArgumentError
    raise Errors::TypeCoercionError.new('CommunityRating', value, 'Float')
  end
end

.validate_day(value) ⇒ Object

Raises:



141
142
143
144
145
146
147
148
# File 'lib/comicinfo/enums.rb', line 141

def self.validate_day value
  return DEFAULT_INTEGER if value.nil? || value.empty?

  day = validate_integer(value, 'Day')
  raise Errors::RangeError.new('Day', day, 1, 31) if day != DEFAULT_INTEGER && (day < 1 || day > 31)

  day
end

.validate_integer(value, field_name = 'integer', default = DEFAULT_INTEGER) ⇒ Object

Validates integer fields with range checking



112
113
114
115
116
117
118
119
120
# File 'lib/comicinfo/enums.rb', line 112

def self.validate_integer value, field_name = 'integer', default = DEFAULT_INTEGER
  return default if value.nil? || value.empty?

  begin
    Integer(value)
  rescue ArgumentError
    raise Errors::TypeCoercionError.new(field_name, value, 'Integer')
  end
end

.validate_manga(value) ⇒ Object

Validates Manga enum values



65
66
67
68
69
70
71
# File 'lib/comicinfo/enums.rb', line 65

def self.validate_manga value
  return DEFAULT_ENUM_UNKNOWN if value.nil? || value.empty?

  raise Errors::InvalidEnumError.new('Manga', value, MANGA_VALUES) unless MANGA_VALUES.include?(value)

  value
end

.validate_month(value) ⇒ Object

Raises:



132
133
134
135
136
137
138
139
# File 'lib/comicinfo/enums.rb', line 132

def self.validate_month value
  return DEFAULT_INTEGER if value.nil? || value.empty?

  month = validate_integer(value, 'Month')
  raise Errors::RangeError.new('Month', month, 1, 12) if month != DEFAULT_INTEGER && (month < 1 || month > 12)

  month
end

.validate_year(value) ⇒ Object

Validates date components (Year, Month, Day)

Raises:



123
124
125
126
127
128
129
130
# File 'lib/comicinfo/enums.rb', line 123

def self.validate_year value
  return DEFAULT_INTEGER if value.nil? || value.empty?

  year = validate_integer(value, 'Year')
  raise Errors::RangeError.new('Year', year, 1000, 9999) if year != DEFAULT_INTEGER && (year < 1000 || year > 9999)

  year
end

.validate_yes_no(value) ⇒ Object

Validates YesNo enum values



56
57
58
59
60
61
62
# File 'lib/comicinfo/enums.rb', line 56

def self.validate_yes_no value
  return DEFAULT_ENUM_UNKNOWN if value.nil? || value.empty?

  raise Errors::InvalidEnumError.new('BlackAndWhite', value, YES_NO_VALUES) unless YES_NO_VALUES.include?(value)

  value
end