mongoid_time_field 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f4aa27455f08c761fe705a87ae8fc0e7a0c012f2
4
- data.tar.gz: 939108e24667ca9df64395eb85afac6a6a5425fe
3
+ metadata.gz: cf1d3359c89fdf7bb3fe0ee40e431e28d1875ab5
4
+ data.tar.gz: ceb9ba4afc6e5f4f5c72314a16840858d093a6a1
5
5
  SHA512:
6
- metadata.gz: dee99307d2610efff356221896ac621530561309e16d14a490ea6e4534bb851d788f8162ec9e0a5e3e2f41ceb253d4a8d805a5fff08efed5e298f76cf44321c1
7
- data.tar.gz: 3b30f2cfa92355c60ddc0f9662f1557ada278b7a9004bc62c21aeb76a5160ac4ad13051d4da989f32bd757cecf9eb22b0e8ca490bbf777cbe1359745562fe889
6
+ metadata.gz: 3db4fdc546994c28a1cca9db4fa40c4c5f1f4ac664f2c871fc55807caa5c2f55464f882d66caf0f2b069734fea897d895b905263a178cc2a5fdb58b68d848805
7
+ data.tar.gz: b8c09be48119c85f2039ad02bb3d29fd2d39808f0dfff8d42ce29c354cf225428cf8f4e96b68dcf5441fabe99ff2cf06edd408c67499a2272ed56f1b664760d7
@@ -65,7 +65,7 @@ class TimeField
65
65
  # Takes any possible object and converts it to how it would be
66
66
  # stored in the database.
67
67
  def mongoize(object)
68
- case object
68
+ case object
69
69
  when Mongoid::TimeField::Value then object.mongoize
70
70
  when String then parse(object).mongoize
71
71
  else object
@@ -1,5 +1,8 @@
1
1
  module Mongoid::TimeField
2
2
  class Value
3
+ YEARS_FACTOR = ((365 * 303 + 366 * 97) / 400) * 86400
4
+ MONTHS_FACTOR = (((365 * 303 + 366 * 97) / 400) * 86400) / 12
5
+
3
6
  attr_accessor :seconds
4
7
 
5
8
  def initialize(seconds, options = {})
@@ -65,6 +68,30 @@ module Mongoid::TimeField
65
68
  end
66
69
  alias_method :to_str, :to_s
67
70
 
71
+ # source: https://github.com/arnau/ISO8601/blob/master/lib/iso8601/duration.rb (MIT)
72
+ def iso8601
73
+ duration = @seconds
74
+ sign = '-' if (duration < 0)
75
+ duration = duration.abs
76
+ years, y_mod = (duration / YEARS_FACTOR).to_i, (duration % YEARS_FACTOR)
77
+ months, m_mod = (y_mod / MONTHS_FACTOR).to_i, (y_mod % MONTHS_FACTOR)
78
+ days, d_mod = (m_mod / 86400).to_i, (m_mod % 86400)
79
+ hours, h_mod = (d_mod / 3600).to_i, (d_mod % 3600)
80
+ minutes, mi_mod = (h_mod / 60).to_i, (h_mod % 60)
81
+ 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`)
82
+
83
+ seconds = (seconds != 0 or (years == 0 and months == 0 and days == 0 and hours == 0 and minutes == 0)) ? "#{seconds}S" : ""
84
+ minutes = (minutes != 0) ? "#{minutes}M" : ""
85
+ hours = (hours != 0) ? "#{hours}H" : ""
86
+ days = (days != 0) ? "#{days}D" : ""
87
+ months = (months != 0) ? "#{months}M" : ""
88
+ years = (years != 0) ? "#{years}Y" : ""
89
+
90
+ date = %[#{sign}P#{years}#{months}#{days}]
91
+ time = (hours != "" or minutes != "" or seconds != "") ? %[T#{hours}#{minutes}#{seconds}] : ""
92
+ date + time
93
+ end
94
+
68
95
  def minutes
69
96
  @seconds / 60
70
97
  end
@@ -1,3 +1,3 @@
1
1
  module MongoidTimeField
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
data/spec/v2_spec.rb CHANGED
@@ -13,9 +13,39 @@ describe Mongoid::TimeField do
13
13
  dummy.open_minutes.should eq 600
14
14
  end
15
15
  end
16
-
16
+ describe 'iso8601' do
17
+ it 'hh:mm' do
18
+ dummy = DummyV2.new
19
+ dummy.open = '10:00'
20
+ dummy.open.iso8601.should eq 'PT10H'
21
+ end
22
+ it 'seconds' do
23
+ dummy = DummyV2.new
24
+ dummy.open = '10:10'
25
+ dummy.open.iso8601.should eq 'PT10H10M'
26
+ end
27
+ it 'hh:mm' do
28
+ dummy = DummyV2.new
29
+ dummy.def = '10:00'
30
+ dummy.def.iso8601.should eq 'PT10M'
31
+ end
32
+ it 'seconds' do
33
+ dummy = DummyV2.new
34
+ dummy.def = '10:10'
35
+ dummy.def.iso8601.should eq 'PT10M10S'
36
+ end
37
+ it 'date' do
38
+ dummy = DummyV2.new
39
+ dummy.open = '12000:00'
40
+ dummy.open.iso8601.should eq 'P1Y4M13DT8H'
41
+ end
42
+ it 'date' do
43
+ dummy = DummyV2.new
44
+ dummy.def = '120:00:00'
45
+ dummy.def.iso8601.should eq 'P5D'
46
+ end
47
+ end
17
48
  describe 'datatype' do
18
-
19
49
  it 'is persisted normally' do
20
50
  dummy = DummyV2.new
21
51
  dummy.open = '10:00'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_time_field
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - glebtv
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-24 00:00:00.000000000 Z
11
+ date: 2013-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid