ruby-duration 2.1.0 → 2.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/lib/duration/mongoid.rb +24 -25
- data/lib/duration/version.rb +1 -1
- data/ruby-duration.gemspec +2 -0
- data/test/test_mongoid.rb +48 -27
- metadata +27 -57
- data/Gemfile.lock +0 -35
data/.gitignore
CHANGED
data/lib/duration/mongoid.rb
CHANGED
@@ -1,35 +1,34 @@
|
|
1
1
|
require "#{File.dirname(__FILE__)}/../duration"
|
2
|
+
require "mongoid/fields"
|
2
3
|
|
3
4
|
# Mongoid serialization support for Duration type.
|
4
5
|
module Mongoid
|
5
6
|
module Fields
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
7
|
+
class Duration
|
8
|
+
include Mongoid::Fields::Serializable
|
9
|
+
|
10
|
+
# Deserialize a Duration given the amount of seconds stored by Mongodb
|
11
|
+
#
|
12
|
+
# @param [Integer, nil] duration in seconds
|
13
|
+
# @return [Duration] deserialized Duration
|
14
|
+
def deserialize(seconds)
|
15
|
+
return if !seconds
|
16
|
+
::Duration.new(seconds)
|
17
|
+
end
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
19
|
+
# Serialize a Duration or a Hash (with duration units) or a amount of seconds to
|
20
|
+
# a BSON serializable type.
|
21
|
+
#
|
22
|
+
# @param [Duration, Hash, Integer] value
|
23
|
+
# @return [Integer] duration in seconds
|
24
|
+
def serialize(value)
|
25
|
+
return if value.blank?
|
26
|
+
if value.is_a?(Hash)
|
27
|
+
value.delete_if{|k, v| v.blank? || !::Duration::UNITS.include?(k.to_sym)}
|
25
28
|
return if value.blank?
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
::Duration.new(value).to_i
|
30
|
-
elsif value.respond_to?(:to_i)
|
31
|
-
value.to_i
|
32
|
-
end
|
29
|
+
::Duration.new(value).to_i
|
30
|
+
elsif value.respond_to?(:to_i)
|
31
|
+
value.to_i
|
33
32
|
end
|
34
33
|
end
|
35
34
|
end
|
data/lib/duration/version.rb
CHANGED
data/ruby-duration.gemspec
CHANGED
@@ -23,6 +23,8 @@ Gem::Specification.new do |s|
|
|
23
23
|
s.add_development_dependency "rake", ">= 0"
|
24
24
|
s.add_development_dependency "simplecov", ">= 0.3.5"
|
25
25
|
s.add_development_dependency "bluecloth", ">= 0.3.5"
|
26
|
+
|
27
|
+
s.add_runtime_dependency "mongoid", "~> 2.4.0"
|
26
28
|
|
27
29
|
s.files = `git ls-files`.split("\n")
|
28
30
|
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
data/test/test_mongoid.rb
CHANGED
@@ -1,49 +1,70 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
require 'helper'
|
3
3
|
require 'duration/mongoid'
|
4
|
+
require 'mongoid'
|
4
5
|
|
5
6
|
describe "mongoid support" do
|
6
7
|
before do
|
7
|
-
|
8
|
+
class MyModel
|
9
|
+
include Mongoid::Document
|
10
|
+
field :duration, type: Duration
|
11
|
+
end
|
12
|
+
@model = MyModel.new
|
8
13
|
end
|
9
14
|
|
10
|
-
describe "
|
15
|
+
describe "assigning an integer value" do
|
11
16
|
it "should return the duration given the total in seconds" do
|
12
|
-
|
17
|
+
@model.duration = 90
|
18
|
+
assert_equal @model.duration, Duration.new(90)
|
13
19
|
end
|
14
20
|
|
15
|
-
it "
|
16
|
-
|
21
|
+
it "assigning a nil value" do
|
22
|
+
@model.duration = nil
|
23
|
+
assert_nil @model.duration
|
17
24
|
end
|
18
25
|
end
|
19
|
-
|
20
|
-
describe "
|
21
|
-
it "should
|
22
|
-
|
23
|
-
assert_nil @
|
24
|
-
assert_nil @mongoid_field.serialize("")
|
25
|
-
assert_nil @mongoid_field.serialize({})
|
26
|
-
assert_nil @mongoid_field.serialize({:seconds => "", :hours => ""})
|
27
|
-
assert_nil @mongoid_field.serialize({:x => 100, :seconds => ""})
|
26
|
+
|
27
|
+
describe "assigning an array" do
|
28
|
+
it "should return nil" do
|
29
|
+
@model.duration = [1,2,3]
|
30
|
+
assert_nil @model.duration
|
28
31
|
end
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "assigning a valid hash" do
|
35
|
+
it "should return total seconds given a duration in hash" do
|
36
|
+
@model.duration = { :minutes => 1, :seconds => 30 }
|
37
|
+
assert_equal Duration.new({ :minutes => 1, :seconds => 30 }), @model.duration
|
32
38
|
end
|
39
|
+
end
|
33
40
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
assert_equal 10, @mongoid_field.serialize("10string")
|
41
|
-
assert_equal 0, @mongoid_field.serialize("string") # not blank
|
41
|
+
describe "assigning invalid hashes" do
|
42
|
+
it "should return nil" do
|
43
|
+
[{}, {:seconds => "", :hours => ""}, {:x => 100, :seconds => ""}].each do |value|
|
44
|
+
@model.duration = value
|
45
|
+
assert_nil @model.duration
|
46
|
+
end
|
42
47
|
end
|
48
|
+
end
|
43
49
|
|
44
|
-
|
45
|
-
|
50
|
+
describe "assigning a Duration object" do
|
51
|
+
it "should return the duration value" do
|
52
|
+
duration = Duration.new(:minutes => 1, :seconds => 30)
|
53
|
+
@model.duration = duration
|
54
|
+
assert_equal duration, @model.duration
|
46
55
|
end
|
47
56
|
end
|
48
57
|
|
58
|
+
describe "assigning a string" do
|
59
|
+
it "should return total seconds given a duration in string" do
|
60
|
+
@model.duration = "10"
|
61
|
+
assert_equal Duration.new(10), @model.duration
|
62
|
+
|
63
|
+
@model.duration = "10string"
|
64
|
+
assert_equal Duration.new(10), @model.duration
|
65
|
+
|
66
|
+
@model.duration = "string"
|
67
|
+
assert_equal Duration.new(0), @model.duration
|
68
|
+
end
|
69
|
+
end
|
49
70
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-duration
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-04-
|
13
|
+
date: 2012-04-17 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|
17
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirement: &2152565600 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,15 +22,10 @@ dependencies:
|
|
22
22
|
version: 3.0.0
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements:
|
26
|
-
none: false
|
27
|
-
requirements:
|
28
|
-
- - ! '>='
|
29
|
-
- !ruby/object:Gem::Version
|
30
|
-
version: 3.0.0
|
25
|
+
version_requirements: *2152565600
|
31
26
|
- !ruby/object:Gem::Dependency
|
32
27
|
name: i18n
|
33
|
-
requirement: !ruby/object:Gem::Requirement
|
28
|
+
requirement: &2152565040 !ruby/object:Gem::Requirement
|
34
29
|
none: false
|
35
30
|
requirements:
|
36
31
|
- - ! '>='
|
@@ -38,15 +33,10 @@ dependencies:
|
|
38
33
|
version: '0'
|
39
34
|
type: :runtime
|
40
35
|
prerelease: false
|
41
|
-
version_requirements:
|
42
|
-
none: false
|
43
|
-
requirements:
|
44
|
-
- - ! '>='
|
45
|
-
- !ruby/object:Gem::Version
|
46
|
-
version: '0'
|
36
|
+
version_requirements: *2152565040
|
47
37
|
- !ruby/object:Gem::Dependency
|
48
38
|
name: bundler
|
49
|
-
requirement: !ruby/object:Gem::Requirement
|
39
|
+
requirement: &2152564560 !ruby/object:Gem::Requirement
|
50
40
|
none: false
|
51
41
|
requirements:
|
52
42
|
- - ! '>='
|
@@ -54,15 +44,10 @@ dependencies:
|
|
54
44
|
version: 1.0.0
|
55
45
|
type: :development
|
56
46
|
prerelease: false
|
57
|
-
version_requirements:
|
58
|
-
none: false
|
59
|
-
requirements:
|
60
|
-
- - ! '>='
|
61
|
-
- !ruby/object:Gem::Version
|
62
|
-
version: 1.0.0
|
47
|
+
version_requirements: *2152564560
|
63
48
|
- !ruby/object:Gem::Dependency
|
64
49
|
name: minitest
|
65
|
-
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirement: &2152564080 !ruby/object:Gem::Requirement
|
66
51
|
none: false
|
67
52
|
requirements:
|
68
53
|
- - ! '>='
|
@@ -70,15 +55,10 @@ dependencies:
|
|
70
55
|
version: '0'
|
71
56
|
type: :development
|
72
57
|
prerelease: false
|
73
|
-
version_requirements:
|
74
|
-
none: false
|
75
|
-
requirements:
|
76
|
-
- - ! '>='
|
77
|
-
- !ruby/object:Gem::Version
|
78
|
-
version: '0'
|
58
|
+
version_requirements: *2152564080
|
79
59
|
- !ruby/object:Gem::Dependency
|
80
60
|
name: yard
|
81
|
-
requirement: !ruby/object:Gem::Requirement
|
61
|
+
requirement: &2152563560 !ruby/object:Gem::Requirement
|
82
62
|
none: false
|
83
63
|
requirements:
|
84
64
|
- - ! '>='
|
@@ -86,15 +66,10 @@ dependencies:
|
|
86
66
|
version: '0'
|
87
67
|
type: :development
|
88
68
|
prerelease: false
|
89
|
-
version_requirements:
|
90
|
-
none: false
|
91
|
-
requirements:
|
92
|
-
- - ! '>='
|
93
|
-
- !ruby/object:Gem::Version
|
94
|
-
version: '0'
|
69
|
+
version_requirements: *2152563560
|
95
70
|
- !ruby/object:Gem::Dependency
|
96
71
|
name: rake
|
97
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirement: &2152563040 !ruby/object:Gem::Requirement
|
98
73
|
none: false
|
99
74
|
requirements:
|
100
75
|
- - ! '>='
|
@@ -102,15 +77,10 @@ dependencies:
|
|
102
77
|
version: '0'
|
103
78
|
type: :development
|
104
79
|
prerelease: false
|
105
|
-
version_requirements:
|
106
|
-
none: false
|
107
|
-
requirements:
|
108
|
-
- - ! '>='
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: '0'
|
80
|
+
version_requirements: *2152563040
|
111
81
|
- !ruby/object:Gem::Dependency
|
112
82
|
name: simplecov
|
113
|
-
requirement: !ruby/object:Gem::Requirement
|
83
|
+
requirement: &2152562400 !ruby/object:Gem::Requirement
|
114
84
|
none: false
|
115
85
|
requirements:
|
116
86
|
- - ! '>='
|
@@ -118,15 +88,10 @@ dependencies:
|
|
118
88
|
version: 0.3.5
|
119
89
|
type: :development
|
120
90
|
prerelease: false
|
121
|
-
version_requirements:
|
122
|
-
none: false
|
123
|
-
requirements:
|
124
|
-
- - ! '>='
|
125
|
-
- !ruby/object:Gem::Version
|
126
|
-
version: 0.3.5
|
91
|
+
version_requirements: *2152562400
|
127
92
|
- !ruby/object:Gem::Dependency
|
128
93
|
name: bluecloth
|
129
|
-
requirement: !ruby/object:Gem::Requirement
|
94
|
+
requirement: &2152561640 !ruby/object:Gem::Requirement
|
130
95
|
none: false
|
131
96
|
requirements:
|
132
97
|
- - ! '>='
|
@@ -134,12 +99,18 @@ dependencies:
|
|
134
99
|
version: 0.3.5
|
135
100
|
type: :development
|
136
101
|
prerelease: false
|
137
|
-
version_requirements:
|
102
|
+
version_requirements: *2152561640
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: mongoid
|
105
|
+
requirement: &2152559660 !ruby/object:Gem::Requirement
|
138
106
|
none: false
|
139
107
|
requirements:
|
140
|
-
- -
|
108
|
+
- - ~>
|
141
109
|
- !ruby/object:Gem::Version
|
142
|
-
version:
|
110
|
+
version: 2.4.0
|
111
|
+
type: :runtime
|
112
|
+
prerelease: false
|
113
|
+
version_requirements: *2152559660
|
143
114
|
description: Duration type
|
144
115
|
email:
|
145
116
|
- jose@peleteiro.net
|
@@ -151,7 +122,6 @@ files:
|
|
151
122
|
- .gitignore
|
152
123
|
- .rvmrc
|
153
124
|
- Gemfile
|
154
|
-
- Gemfile.lock
|
155
125
|
- LICENSE
|
156
126
|
- README.md
|
157
127
|
- Rakefile
|
@@ -186,7 +156,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
186
156
|
version: 1.3.6
|
187
157
|
requirements: []
|
188
158
|
rubyforge_project: ruby-duration
|
189
|
-
rubygems_version: 1.8.
|
159
|
+
rubygems_version: 1.8.17
|
190
160
|
signing_key:
|
191
161
|
specification_version: 3
|
192
162
|
summary: Duration type
|
data/Gemfile.lock
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
ruby-duration (2.1.0)
|
5
|
-
activesupport (>= 3.0.0)
|
6
|
-
i18n
|
7
|
-
|
8
|
-
GEM
|
9
|
-
remote: http://rubygems.org/
|
10
|
-
specs:
|
11
|
-
activesupport (3.2.3)
|
12
|
-
i18n (~> 0.6)
|
13
|
-
multi_json (~> 1.0)
|
14
|
-
bluecloth (2.2.0)
|
15
|
-
i18n (0.6.0)
|
16
|
-
minitest (2.12.0)
|
17
|
-
multi_json (1.2.0)
|
18
|
-
rake (0.9.2.2)
|
19
|
-
simplecov (0.6.1)
|
20
|
-
multi_json (~> 1.0)
|
21
|
-
simplecov-html (~> 0.5.3)
|
22
|
-
simplecov-html (0.5.3)
|
23
|
-
yard (0.7.5)
|
24
|
-
|
25
|
-
PLATFORMS
|
26
|
-
ruby
|
27
|
-
|
28
|
-
DEPENDENCIES
|
29
|
-
bluecloth (>= 0.3.5)
|
30
|
-
bundler (>= 1.0.0)
|
31
|
-
minitest
|
32
|
-
rake
|
33
|
-
ruby-duration!
|
34
|
-
simplecov (>= 0.3.5)
|
35
|
-
yard
|