Class: Mongoid::TimeField::Value

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid_time_field/value.rb

Constant Summary collapse

YEARS_FACTOR =
((365 * 303 + 366 * 97) / 400) * 86400
MONTHS_FACTOR =
(((365 * 303 + 366 * 97) / 400) * 86400) / 12

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seconds, options = {}) ⇒ Value

Returns a new instance of Value.



8
9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#secondsObject Also known as: to_i

Returns the value of attribute seconds.



6
7
8
# File 'lib/mongoid_time_field/value.rb', line 6

def seconds
  @seconds
end

Instance Method Details

#<(x) ⇒ Object

Raises:

  • (ArgumentError)


112
113
114
115
# 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) ⇒ Object



121
122
123
124
125
126
127
128
129
# 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) ⇒ Object

Raises:

  • (ArgumentError)


107
108
109
110
# 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) ⇒ Object



20
21
22
# File 'lib/mongoid_time_field/value.rb', line 20

def __bson_dump__(io, key)
  seconds.__bson_dump__(io, key)
end

#coerce(something) ⇒ Object



117
118
119
# File 'lib/mongoid_time_field/value.rb', line 117

def coerce(something)
  [self, something]
end

#formatObject



24
25
26
# File 'lib/mongoid_time_field/value.rb', line 24

def format
  @options[:format].dup
end

#inspectObject



103
104
105
# File 'lib/mongoid_time_field/value.rb', line 103

def inspect
  '"' + to_s + '"'
end

#iso8601Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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

#minutesObject



95
96
97
# File 'lib/mongoid_time_field/value.rb', line 95

def minutes
  @seconds / 60
end

#mongoizeObject



99
100
101
# File 'lib/mongoid_time_field/value.rb', line 99

def mongoize
  @seconds
end

#to_sObject Also known as: to_str



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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