class Mongoid::DayTime::Type

Attributes

hour[R]
min[R]
time[R]

Public Class Methods

new(time) click to toggle source
# File lib/mongoid/day_time/type.rb, line 28
def initialize(time)
  @time = time
  @hour, @min = time.split(':').map(&:to_i)
end

Private Class Methods

demongoize(object) click to toggle source
# File lib/mongoid/day_time/type.rb, line 44
def demongoize(object)
  case object
  when Integer
    self.from_minutes(object).to_s
  else
    raise TypeError
  end
end
evolve(object) click to toggle source
# File lib/mongoid/day_time/type.rb, line 66
def evolve(object)
  case object
  when Integer
    object
  when self
    object.mongoize
  else
    raise TypeError
  end
end
from_minutes(object) click to toggle source
# File lib/mongoid/day_time/type.rb, line 34
def from_minutes(object) # "10:15" = 10 * 60 + 15 = 615
  hour, min = object.divmod(60)
  str = [hour.to_s.rjust(2, '0'), min.to_s.rjust(2, '0')].join(":")
  self.new(str)
end
from_string(object) click to toggle source
# File lib/mongoid/day_time/type.rb, line 40
def from_string(object) # "10:15"
  self.new(object)
end
mongoize(object) click to toggle source
# File lib/mongoid/day_time/type.rb, line 53
def mongoize(object)
  case object
  when Integer
    self.from_minutes(object).mongoize
  when String
    self.from_string(object).mongoize
  when self
    object
  else
    raise TypeError
  end
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/mongoid/day_time/type.rb, line 23
def <=>(other)
  self.to_i <=> other.to_i
end
mongoize() click to toggle source
# File lib/mongoid/day_time/type.rb, line 11
def mongoize
  to_minutes
end
to_i() click to toggle source
# File lib/mongoid/day_time/type.rb, line 15
def to_i
  to_minutes
end
to_minutes() click to toggle source
# File lib/mongoid/day_time/type.rb, line 7
def to_minutes
  hour * 60 + min
end
to_s() click to toggle source
# File lib/mongoid/day_time/type.rb, line 19
def to_s
  [hour.to_s.rjust(2, '0'), min.to_s.rjust(2, '0')].join(":")
end