mongoid_time_field 0.2.1 → 0.2.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: 675c1a0fb1ddddd399518a3984b3575f5203186e
4
- data.tar.gz: 254095769b5f2c6cb8b67889d2e9e7aea3211c05
3
+ metadata.gz: 7180a86b7f2a54f680b0da5bb2388896749519b8
4
+ data.tar.gz: 7ade24dc09af27c8f37f5df045f13eef2fa14fb2
5
5
  SHA512:
6
- metadata.gz: 54b63521baaccff3b95eb6d7075d6ae58e0e342ea95c87d710a380a439c8adfa7af5983e1ce7fd15279bbb9692afc1a6b0fa0b2eef05f001984251541d0c4182
7
- data.tar.gz: 93944c906019a1a619b3cc77fd35f592112f7b9645d0b46da9c1402d563e84c5c72da52b84eb41e865db67335f5d6277a9cf43962c018c8d12149e1bbf6205c0
6
+ metadata.gz: 820f40c21a377b967fad550d1664562d3ceceb1ac5d8dc19dcdc4098929fd08f78c1fd45e26941343aad08db2c367c2188918ce0a8dd1c5dc73888f99ffaf9cc
7
+ data.tar.gz: 6e009f5bb2d6ac3fa56836cfdd21806585955094a045ef9cb2dc1946eaf214a5e51b055d6959aaf3035577f0019f241b79761bc71aad230637c336f8ca7a9d0a
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mongoid_time_field (0.2.1)
4
+ mongoid_time_field (0.2.2)
5
5
  mongoid (>= 3.0.0)
6
6
 
7
7
  GEM
data/README.rdoc CHANGED
@@ -18,10 +18,26 @@ Include the gem in your Gemfile
18
18
  field :duration, type: TimeField.new(format: 'mm:SS')
19
19
  end
20
20
 
21
+ == Format tokens:
22
+
23
+ SS - Seconds (always two digits)
24
+ MM - Minutes, zero-padded
25
+ mm - minutes, no padding
26
+ HH - hours, zero-padded to two
27
+ hh - hours, no padding
28
+ hh? - hours, displays hours only if they are present (non-zero)
29
+
30
+ Default format is 'hh?:mm:SS'.
31
+
32
+ Leftmost value in format is of arbitrary length (i.e. format 'HH:MM' is not limited to 24 hours, 'mm:SS' is not limited to 60 minutes and should not overflow)
33
+
34
+ == changes since v.0.1.0
35
+ Default format for deprecated time_field syntax is 'mm:SS' for compatibility (If you stored HH:MM with 0.1.0 it should still just keep working with 'mm:SS' format, with seconds stored in DB, but if you change to new syntax and use 'HH:MM' format, you need to multiply all stored values by 60)
36
+
21
37
  All time values are converted and stored in mongodb as seconds.
22
38
 
23
39
  == Contributing to mongoid_time_field
24
-
40
+
25
41
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
26
42
  * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
27
43
  * Fork the project.
@@ -2,12 +2,13 @@ class TimeField
2
2
  def initialize(options = {})
3
3
  @options = options
4
4
 
5
- @options[:format] = 'mm:SS' unless options[:format]
5
+ @options[:format] = 'hh?:mm:SS' unless options[:format]
6
6
  @options[:regexp] = build_regexp(options[:format]) unless options[:regexp]
7
7
  end
8
8
 
9
9
  def build_regexp(format)
10
10
  s = '^' + Regexp.escape(format) + '$'
11
+ s.gsub!('hh\?', '(?<h>\d*?)')
11
12
  s.gsub!(':', ':?')
12
13
  s.gsub!('-', '-?')
13
14
  s.gsub!('_', '_?')
@@ -18,13 +18,22 @@ module Mongoid::TimeField
18
18
  if @seconds.nil?
19
19
  nil
20
20
  else
21
- format = @options[:format]
21
+ format = @options[:format].dup
22
22
 
23
23
  fm, ss = @seconds.divmod(60)
24
24
  hh, mm = fm.divmod(60)
25
25
 
26
+ if !format.match(/hh\?/).nil?
27
+ if hh > 0
28
+ format.gsub!('hh?', 'hh')
29
+ format.gsub!('mm', 'MM')
30
+ else
31
+ format.gsub!(/hh\?[:\-_ ]?/, '')
32
+ end
33
+ end
34
+
26
35
  if format.match(/hh/i).nil?
27
- replaces = {
36
+ replaces = {
28
37
  'mm' => fm,
29
38
  'MM' => fm.to_s.rjust(2, '0'),
30
39
  'SS' => ss.to_s.rjust(2, '0'),
@@ -33,7 +42,7 @@ module Mongoid::TimeField
33
42
  replaces[match]
34
43
  end
35
44
  else
36
- replaces = {
45
+ replaces = {
37
46
  'hh' => hh,
38
47
  'HH' => hh.to_s.rjust(2, '0'),
39
48
  'mm' => mm,
@@ -1,3 +1,3 @@
1
1
  module MongoidTimeField
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -3,6 +3,8 @@ class DummyV2
3
3
 
4
4
  field :description
5
5
 
6
+ field :def, type: TimeField.new()
7
+
6
8
  field :open, type: TimeField.new(format: "hh:MM")
7
9
  field :close, type: TimeField.new(format: 'hh:MM')
8
10
 
data/spec/v2_spec.rb CHANGED
@@ -172,4 +172,18 @@ describe Mongoid::TimeField do
172
172
  dummy.time_of_day.minutes.should eq (130)
173
173
  end
174
174
  end
175
+
176
+ describe 'optional hours' do
177
+ it 'parses hours' do
178
+ dummy = DummyV2.new
179
+ dummy.def = '1:02:03'
180
+ dummy.def.should eq '1:02:03'
181
+ end
182
+
183
+ it 'formats without hours when no hours are present' do
184
+ dummy = DummyV2.new
185
+ dummy.def = '02:03'
186
+ dummy.def.should eq '2:03'
187
+ end
188
+ end
175
189
  end
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.2.1
4
+ version: 0.2.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-06-07 00:00:00.000000000 Z
11
+ date: 2013-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid