class Mongoid::TimeField::Value

Constants

MONTHS_FACTOR
YEARS_FACTOR

Attributes

seconds[RW]
to_i[RW]

Public Class Methods

new(seconds, options = {}) click to toggle source
# File lib/mongoid_time_field/value.rb, line 8
def initialize(seconds, options = {})
  if seconds.blank?
    @seconds = nil
  else
    @seconds = seconds
  end

  @options = options
end

Public Instance Methods

<(x) click to toggle source
# File lib/mongoid_time_field/value.rb, line 112
def <(x)
  raise ArgumentError, 'Argument is not Mongoid::TimeField::Value' unless x.is_a? Mongoid::TimeField::Value
  @seconds < x.seconds
end
==(something) click to toggle source
# File lib/mongoid_time_field/value.rb, line 121
def ==(something)
  if seconds === something
    true
  elsif to_s === something
    true
  else
    false
  end
end
>(x) click to toggle source
# File lib/mongoid_time_field/value.rb, line 107
def >(x)
  raise ArgumentError, 'Argument is not Mongoid::TimeField::Value' unless x.is_a? Mongoid::TimeField::Value
  @seconds > x.seconds
end
__bson_dump__(io, key) click to toggle source
# File lib/mongoid_time_field/value.rb, line 20
def __bson_dump__(io, key)
  seconds.__bson_dump__(io, key)
end
coerce(something) click to toggle source
# File lib/mongoid_time_field/value.rb, line 117
def coerce(something)
  [self, something]
end
format() click to toggle source
# File lib/mongoid_time_field/value.rb, line 24
def format
  @options[:format].dup
end
inspect() click to toggle source
# File lib/mongoid_time_field/value.rb, line 103
def inspect
  '"' + to_s + '"'
end
iso8601() click to toggle source

source: github.com/arnau/ISO8601/blob/master/lib/iso8601/duration.rb (MIT)

# File lib/mongoid_time_field/value.rb, line 72
def iso8601
  duration = @seconds
  sign = '-' if (duration < 0)
  duration = duration.abs
  years, y_mod = (duration / YEARS_FACTOR).to_i, (duration % YEARS_FACTOR)
  months, m_mod = (y_mod / MONTHS_FACTOR).to_i, (y_mod % MONTHS_FACTOR)
  days, d_mod = (m_mod / 86400).to_i, (m_mod % 86400)
  hours, h_mod = (d_mod / 3600).to_i, (d_mod % 3600)
  minutes, mi_mod = (h_mod / 60).to_i, (h_mod % 60)
  seconds = mi_mod.div(1) == mi_mod ? mi_mod.to_i : mi_mod.to_f # Coerce to Integer when needed (`PT1S` instead of `PT1.0S`)

  seconds = (seconds != 0 or (years == 0 and months == 0 and days == 0 and hours == 0 and minutes == 0)) ? "#{seconds}S" : ""
  minutes = (minutes != 0) ? "#{minutes}M" : ""
  hours = (hours != 0) ? "#{hours}H" : ""
  days = (days != 0) ? "#{days}D" : ""
  months = (months != 0) ? "#{months}M" : ""
  years = (years != 0) ? "#{years}Y" : ""

  date = %[#{sign}P#{years}#{months}#{days}]
  time = (hours != "" or minutes != "" or seconds != "") ? %[T#{hours}#{minutes}#{seconds}] : ""
  date + time
end
minutes() click to toggle source
# File lib/mongoid_time_field/value.rb, line 95
def minutes
  @seconds / 60
end
mongoize() click to toggle source
# File lib/mongoid_time_field/value.rb, line 99
def mongoize
  @seconds
end
to_s() click to toggle source
# File lib/mongoid_time_field/value.rb, line 28
def to_s
  if @seconds.nil?
    nil
  else
    format = @options[:format].dup

    fm, ss = @seconds.divmod(60)
    hh, mm = fm.divmod(60)

    if !format.match(/hh\?/).nil?
      if hh > 0
        format.gsub!('hh?', 'hh')
        format.gsub!('mm', 'MM')
      else
        format.gsub!(/hh\?[:\-_ ]?/, '')
      end
    end

    if format.match(/hh/i).nil?
      replaces  = {
        'mm' => fm,
        'MM' => fm.to_s.rjust(2, '0'),
        'SS' => ss.to_s.rjust(2, '0'),
      }
      format.gsub(/(mm|MM|SS)/) do |match|
        replaces[match]
      end
    else
      replaces  = {
        'hh' => hh,
        'HH' => hh.to_s.rjust(2, '0'),
        'mm' => mm,
        'MM' => mm.to_s.rjust(2, '0'),
        'SS' => ss.to_s.rjust(2, '0'),
      }
      format.gsub(/(hh|HH|mm|MM|SS)/) do |match|
        replaces[match]
      end
    end
  end
end
Also aliased as: to_str
to_str()
Alias for: to_s