Class: Mongoid::TimeField::Value
- Inherits:
-
Object
- Object
- Mongoid::TimeField::Value
- 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
-
#seconds ⇒ Object
(also: #to_i)
Returns the value of attribute seconds.
Instance Method Summary collapse
- #<(x) ⇒ Object
- #==(something) ⇒ Object
- #>(x) ⇒ Object
- #__bson_dump__(io, key) ⇒ Object
- #coerce(something) ⇒ Object
- #format ⇒ Object
-
#initialize(seconds, options = {}) ⇒ Value
constructor
A new instance of Value.
- #inspect ⇒ Object
-
#iso8601 ⇒ Object
source: github.com/arnau/ISO8601/blob/master/lib/iso8601/duration.rb (MIT).
- #minutes ⇒ Object
- #mongoize ⇒ Object
- #to_s ⇒ Object (also: #to_str)
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, = {}) if seconds.blank? @seconds = nil else @seconds = seconds end = end |
Instance Attribute Details
#seconds ⇒ Object 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
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
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 |
#format ⇒ Object
24 25 26 |
# File 'lib/mongoid_time_field/value.rb', line 24 def format [:format].dup end |
#inspect ⇒ Object
103 104 105 |
# File 'lib/mongoid_time_field/value.rb', line 103 def inspect '"' + to_s + '"' end |
#iso8601 ⇒ Object
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 |
#minutes ⇒ Object
95 96 97 |
# File 'lib/mongoid_time_field/value.rb', line 95 def minutes @seconds / 60 end |
#mongoize ⇒ Object
99 100 101 |
# File 'lib/mongoid_time_field/value.rb', line 99 def mongoize @seconds end |
#to_s ⇒ Object 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 = [: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 |