class PlayWhe::Result

Attributes

context[R]
date[R]
draw[R]
errors[R]
mark[R]
period[R]

Public Class Methods

new(draw:, date:, mark:, period:) click to toggle source
# File lib/playwhe/result.rb, line 8
def initialize(draw:, date:, mark:, period:)
  @draw = draw
  @date = date
  @mark = mark
  @period = period
  clean
end

Public Instance Methods

==(other) click to toggle source
# File lib/playwhe/result.rb, line 16
def ==(other)
  return true if other.equal?(self)
  return false unless other.instance_of?(self.class)
  draw == other.draw &&
    date == other.date &&
      mark == other.mark &&
        period == other.period
end
is_valid?(context = nil) click to toggle source
# File lib/playwhe/result.rb, line 29
def is_valid?(context = nil)
  validate(context)
  no_errors?
end
to_s() click to toggle source
# File lib/playwhe/result.rb, line 25
def to_s
  "#{draw},#{date ? date.strftime('%Y-%m-%d') : '-'},#{mark},#{period}"
end

Private Instance Methods

add_error(name, message) click to toggle source
# File lib/playwhe/result.rb, line 76
def add_error(name, message)
  @errors[name] ||= []
  @errors[name] << message
end
clean() click to toggle source
# File lib/playwhe/result.rb, line 38
def clean
  clean_draw
  clean_date
  clean_mark
  clean_period
end
clean_date() click to toggle source
# File lib/playwhe/result.rb, line 49
def clean_date
  @date = Date.parse(date.to_s) unless date.is_a?(Date)
rescue ArgumentError
  @date = nil
end
clean_draw() click to toggle source
# File lib/playwhe/result.rb, line 45
def clean_draw
  @draw = draw.to_i
end
clean_mark() click to toggle source
# File lib/playwhe/result.rb, line 55
def clean_mark
  @mark = mark.to_i
end
clean_period() click to toggle source
# File lib/playwhe/result.rb, line 59
def clean_period
  @period = period.to_s.upcase
end
init(context) click to toggle source
# File lib/playwhe/result.rb, line 71
def init(context)
  @context = context
  @errors = {}
end
no_errors?() click to toggle source
# File lib/playwhe/result.rb, line 81
def no_errors?
  errors.empty?
end
validate(context) click to toggle source
# File lib/playwhe/result.rb, line 63
def validate(context)
  init(context)
  validate_draw
  validate_date
  validate_mark
  validate_period
end
validate_date() click to toggle source
# File lib/playwhe/result.rb, line 89
def validate_date
  if date.nil?
    add_error(:date, "must be a date")
  else
    if date < BIRTHDAY
      add_error(:date, "must be greater than Play Whe's birthday")
    end

    if context && context.year && date.year != context.year
      add_error(:date, "must be for the correct year (#{context.year})")
    end

    if context && context.month && date.month != context.month
      add_error(:date, "must be for the correct month (#{context.month})")
    end
  end
end
validate_draw() click to toggle source
# File lib/playwhe/result.rb, line 85
def validate_draw
  add_error(:draw, "must be a positive integer") unless draw >= 1
end
validate_mark() click to toggle source
# File lib/playwhe/result.rb, line 107
def validate_mark
  if mark < LOWEST_MARK || mark > HIGHEST_MARK
    add_error(:mark, "must be between #{LOWEST_MARK} and #{HIGHEST_MARK} inclusive")
  end
end
validate_period() click to toggle source
# File lib/playwhe/result.rb, line 113
def validate_period
  unless PERIODS.include?(period)
    add_error(:period, "must be one of #{PERIODS.join(', ')}")
  end
end