class FortyTime

Constants

VERSION

Attributes

minutes[RW]
value[RW]

Public Class Methods

new(minutes) click to toggle source
# File lib/forty_time.rb, line 30
def initialize(minutes)
  @minutes = minutes
end
parse(input) click to toggle source
# File lib/forty_time.rb, line 8
def self.parse(input)
  return NullFortyTime.new if input.nil? || input == ''
  return new(input) if input.is_a? Integer

  can_parse_string = input.is_a?(String) && input.match?(/:/)
  raise ParseError unless can_parse_string

  minutes = parse_string(input)
  new(minutes)
end
parse_string(string) click to toggle source
# File lib/forty_time.rb, line 19
def self.parse_string(string)
  hours, extra = string.split(':').map(&:to_i)
  extra *= -1 if string[0] == '-'

  hours * 60 + extra
end

Public Instance Methods

+(other) click to toggle source
# File lib/forty_time.rb, line 34
def +(other)
  result = @minutes + other.minutes
  FortyTime.new(result)
end
-(other) click to toggle source
# File lib/forty_time.rb, line 39
def -(other)
  result = @minutes - other.minutes
  FortyTime.new(result)
end
to_s() click to toggle source
# File lib/forty_time.rb, line 44
def to_s
  hours = (@minutes / 60.0).to_i
  extra = (@minutes - hours * 60.0).to_i

  if @minutes.negative?
    hours *= -1
    extra *= -1
    hours = "-#{hours}"
  end

  extra = "0#{extra}" if extra < 10
  [hours, extra].join(':')
end