ruby-duration 0.5.4 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.2@ruby-duration
data/Gemfile.lock CHANGED
@@ -1,15 +1,16 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-duration (0.5.0)
4
+ ruby-duration (2.0.1)
5
+ activesupport (>= 3.0.0)
5
6
  i18n
6
7
 
7
8
  GEM
8
9
  remote: http://rubygems.org/
9
10
  specs:
10
- activesupport (3.0.1)
11
+ activesupport (3.0.9)
11
12
  bluecloth (2.0.9)
12
- i18n (0.4.1)
13
+ i18n (0.5.0)
13
14
  minitest (1.7.2)
14
15
  rake (0.8.7)
15
16
  simplecov (0.3.6)
@@ -21,10 +22,8 @@ PLATFORMS
21
22
  ruby
22
23
 
23
24
  DEPENDENCIES
24
- activesupport (>= 3.0.0)
25
25
  bluecloth (>= 0.3.5)
26
26
  bundler (>= 1.0.0)
27
- i18n
28
27
  minitest
29
28
  rake
30
29
  ruby-duration!
data/README.md CHANGED
@@ -37,6 +37,7 @@ Show me the code
37
37
  Duration.new(:weeks => 1, :days => 20).iso8601 => "P3W6DT0H0M0S"
38
38
 
39
39
  ### Mongoid support
40
+ The current version of this gem supports Mongoid >= [2.1.0](https://github.com/mongoid/mongoid). For lower Mongoid versions try the tag [v1.0.0](https://github.com/peleteiro/ruby-duration/tree/v1.0.0)
40
41
 
41
42
  require 'duration/mongoid'
42
43
 
data/lib/duration.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  require 'i18n'
3
+ require 'active_support/core_ext'
3
4
 
4
5
  # Duration objects are simple mechanisms that allow you to operate on durations
5
6
  # of time. They allow you to know how much time has passed since a certain
@@ -11,7 +12,7 @@ class Duration
11
12
  include Comparable
12
13
 
13
14
  UNITS = [:seconds, :minutes, :hours, :days, :weeks]
14
-
15
+
15
16
  MULTIPLES = {:seconds => 1,
16
17
  :minutes => 60,
17
18
  :hours => 3600,
@@ -92,7 +93,7 @@ class Duration
92
93
  def blank?
93
94
  @total == 0
94
95
  end
95
-
96
+
96
97
  # @return true if total different than 0
97
98
  def present?
98
99
  !blank?
@@ -161,7 +162,7 @@ class Duration
161
162
 
162
163
  alias_method :to_i, :total
163
164
  alias_method :strftime, :format
164
-
165
+
165
166
  private
166
167
 
167
168
  # Calculates the duration from seconds and figures out what the actual
@@ -180,11 +181,11 @@ private
180
181
  # Gather the divided units
181
182
  @weeks, @days, @hours, @minutes, @seconds = units
182
183
  end
183
-
184
+
184
185
  def i18n_for(singular)
185
186
  plural = "#{singular}s"
186
187
  label = send(plural) == 1 ? singular : plural
187
-
188
+
188
189
  I18n.t(label, :scope => :ruby_duration, :default => label.to_s)
189
190
  end
190
- end
191
+ end
@@ -1,34 +1,37 @@
1
- # -*- encoding: utf-8 -*-
2
- require 'duration'
3
- require 'active_support/core_ext'
1
+ require "#{File.dirname(__FILE__)}/../duration"
4
2
 
5
3
  # Mongoid serialization support for Duration type.
6
- module Duration::Mongoid
7
-
8
- # Deserialize a Duration given the amount of seconds stored by Mongodb
9
- #
10
- # @param [Integer, nil] duration in seconds
11
- # @return [Duration] deserialized Duration
12
- def get(seconds)
13
- return if !seconds
14
- Duration.new(seconds)
15
- end
4
+ module Mongoid
5
+ module Fields
6
+ module Serializable
7
+ class Duration
8
+ include 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
16
18
 
17
- # Serialize a Duration or a Hash (with duration units) or a amount of seconds to
18
- # a BSON serializable type.
19
- #
20
- # @param [Duration, Hash, Integer] value
21
- # @return [Integer] duration in seconds
22
- def set(value)
23
- return if value.blank?
24
- if value.is_a?(Hash)
25
- value.delete_if{|k, v| v.blank? || !Duration::UNITS.include?(k.to_sym)}
26
- return if value.blank?
27
- Duration.new(value).to_i
28
- elsif value.respond_to?(:to_i)
29
- value.to_i
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)}
28
+ return if value.blank?
29
+ ::Duration.new(value).to_i
30
+ elsif value.respond_to?(:to_i)
31
+ value.to_i
32
+ end
33
+ end
34
+ end
30
35
  end
31
36
  end
32
- end
33
-
34
- Duration.send(:extend, Duration::Mongoid)
37
+ end
@@ -1,3 +1,3 @@
1
1
  class Duration
2
- VERSION = "0.5.4"
3
- end
2
+ VERSION = "2.0.2"
3
+ end
data/lib/ruby-duration.rb CHANGED
@@ -1,2 +1,2 @@
1
1
  # -*- encoding: utf-8 -*-
2
- require 'duration'
2
+ require "#{File.dirname(__FILE__)}/duration"
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
6
6
  s.version = Duration::VERSION
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Jose Peleteiro", "Bruno Azisaka Maciel"]
9
- s.email = ["jose@peleteiro.net", "bruno@bubble.com.br"]
9
+ s.email = ["jose@peleteiro.net", "bruno@azisaka.com.br"]
10
10
  s.homepage = "http://github.com/peleteiro/ruby-duration"
11
11
  s.summary = "Duration type"
12
12
  s.description = "Duration type"
@@ -14,13 +14,13 @@ Gem::Specification.new do |s|
14
14
  s.required_rubygems_version = ">= 1.3.6"
15
15
  s.rubyforge_project = "ruby-duration"
16
16
 
17
+ s.add_dependency "activesupport", ">= 3.0.0"
17
18
  s.add_dependency "i18n", ">= 0"
18
19
 
19
20
  s.add_development_dependency "bundler", ">= 1.0.0"
20
21
  s.add_development_dependency "minitest", ">= 0"
21
22
  s.add_development_dependency "yard", ">= 0"
22
23
  s.add_development_dependency "rake", ">= 0"
23
- s.add_development_dependency "activesupport", ">= 3.0.0"
24
24
  s.add_development_dependency "simplecov", ">= 0.3.5"
25
25
  s.add_development_dependency "bluecloth", ">= 0.3.5"
26
26
 
data/test/helper.rb CHANGED
@@ -10,7 +10,6 @@ end
10
10
 
11
11
  $LOAD_PATH.unshift(File.dirname(__FILE__))
12
12
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
13
- require 'duration'
14
- require 'duration/mongoid'
13
+ require 'ruby-duration'
15
14
 
16
15
  MiniTest::Unit.autorun
@@ -2,7 +2,6 @@
2
2
  require 'helper'
3
3
 
4
4
  describe "Duration" do
5
-
6
5
  it "should initialize given duration in seconds" do
7
6
  d = Duration.new(90)
8
7
  assert_equal 0, d.weeks
@@ -117,4 +116,4 @@ describe "Duration" do
117
116
  end
118
117
  end
119
118
 
120
- end
119
+ end
@@ -1,9 +1,9 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  require 'helper'
3
3
 
4
- I18n.load_path << File.join(File.dirname(__FILE__), '..', 'fixtures', 'locales', 'pt.yml')
4
+ I18n.load_path << File.join(File.dirname(__FILE__), 'fixtures', 'locales', 'pt.yml')
5
5
 
6
- describe "I18n" do
6
+ describe "I18n" do
7
7
  describe "when the locale is pt" do
8
8
  before do
9
9
  I18n.locale = :pt
@@ -0,0 +1,49 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'helper'
3
+ require 'duration/mongoid'
4
+
5
+ describe "mongoid support" do
6
+ before do
7
+ @mongoid_field = Mongoid::Fields::Serializable::Duration.new
8
+ end
9
+
10
+ describe "#get - Mongoid's deserialization" do
11
+ it "should return the duration given the total in seconds" do
12
+ assert_equal Duration.new(90), @mongoid_field.deserialize(90)
13
+ end
14
+
15
+ it "should return nil for nil serialized values" do
16
+ assert_nil @mongoid_field.deserialize(nil)
17
+ end
18
+ end
19
+
20
+ describe "#set - Mongoid's serialization" do
21
+ it "should serialize to nil given an invalid serialized value or nil" do
22
+ assert_nil @mongoid_field.serialize([1,2,3])
23
+ assert_nil @mongoid_field.serialize(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 => ""})
28
+ end
29
+
30
+ it "should return total seconds given a duration" do
31
+ assert_equal 90, @mongoid_field.serialize(Duration.new(:minutes => 1, :seconds => 30))
32
+ end
33
+
34
+ it "should return total seconds given a duration in seconds" do
35
+ assert_equal 10, @mongoid_field.serialize(10)
36
+ end
37
+
38
+ it "should return total seconds given a duration in string" do
39
+ assert_equal 10, @mongoid_field.serialize("10")
40
+ assert_equal 10, @mongoid_field.serialize("10string")
41
+ assert_equal 0, @mongoid_field.serialize("string") # not blank
42
+ end
43
+
44
+ it "should return total seconds given a duration in hash" do
45
+ assert_equal 90, @mongoid_field.serialize(:minutes => 1, :seconds => 30)
46
+ end
47
+ end
48
+
49
+ end
metadata CHANGED
@@ -1,147 +1,115 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: ruby-duration
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 5
8
- - 4
9
- version: 0.5.4
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.2
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Jose Peleteiro
13
9
  - Bruno Azisaka Maciel
14
10
  autorequire:
15
11
  bindir: bin
16
12
  cert_chain: []
17
-
18
- date: 2010-11-15 00:00:00 -02:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
22
- name: i18n
13
+ date: 2011-08-04 00:00:00.000000000Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ requirement: &2153695180 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 3.0.0
23
+ type: :runtime
23
24
  prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
25
+ version_requirements: *2153695180
26
+ - !ruby/object:Gem::Dependency
27
+ name: i18n
28
+ requirement: &2153694720 !ruby/object:Gem::Requirement
25
29
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- segments:
30
- - 0
31
- version: "0"
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
32
34
  type: :runtime
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: bundler
36
35
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
36
+ version_requirements: *2153694720
37
+ - !ruby/object:Gem::Dependency
38
+ name: bundler
39
+ requirement: &2153694260 !ruby/object:Gem::Requirement
38
40
  none: false
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- segments:
43
- - 1
44
- - 0
45
- - 0
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
46
44
  version: 1.0.0
47
45
  type: :development
48
- version_requirements: *id002
49
- - !ruby/object:Gem::Dependency
50
- name: minitest
51
46
  prerelease: false
52
- requirement: &id003 !ruby/object:Gem::Requirement
47
+ version_requirements: *2153694260
48
+ - !ruby/object:Gem::Dependency
49
+ name: minitest
50
+ requirement: &2153693800 !ruby/object:Gem::Requirement
53
51
  none: false
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- segments:
58
- - 0
59
- version: "0"
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
60
56
  type: :development
61
- version_requirements: *id003
62
- - !ruby/object:Gem::Dependency
63
- name: yard
64
57
  prerelease: false
65
- requirement: &id004 !ruby/object:Gem::Requirement
58
+ version_requirements: *2153693800
59
+ - !ruby/object:Gem::Dependency
60
+ name: yard
61
+ requirement: &2153693340 !ruby/object:Gem::Requirement
66
62
  none: false
67
- requirements:
68
- - - ">="
69
- - !ruby/object:Gem::Version
70
- segments:
71
- - 0
72
- version: "0"
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
73
67
  type: :development
74
- version_requirements: *id004
75
- - !ruby/object:Gem::Dependency
76
- name: rake
77
68
  prerelease: false
78
- requirement: &id005 !ruby/object:Gem::Requirement
69
+ version_requirements: *2153693340
70
+ - !ruby/object:Gem::Dependency
71
+ name: rake
72
+ requirement: &2153692880 !ruby/object:Gem::Requirement
79
73
  none: false
80
- requirements:
81
- - - ">="
82
- - !ruby/object:Gem::Version
83
- segments:
84
- - 0
85
- version: "0"
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
86
78
  type: :development
87
- version_requirements: *id005
88
- - !ruby/object:Gem::Dependency
89
- name: activesupport
90
79
  prerelease: false
91
- requirement: &id006 !ruby/object:Gem::Requirement
92
- none: false
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- segments:
97
- - 3
98
- - 0
99
- - 0
100
- version: 3.0.0
101
- type: :development
102
- version_requirements: *id006
103
- - !ruby/object:Gem::Dependency
80
+ version_requirements: *2153692880
81
+ - !ruby/object:Gem::Dependency
104
82
  name: simplecov
105
- prerelease: false
106
- requirement: &id007 !ruby/object:Gem::Requirement
83
+ requirement: &2153692420 !ruby/object:Gem::Requirement
107
84
  none: false
108
- requirements:
109
- - - ">="
110
- - !ruby/object:Gem::Version
111
- segments:
112
- - 0
113
- - 3
114
- - 5
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
115
88
  version: 0.3.5
116
89
  type: :development
117
- version_requirements: *id007
118
- - !ruby/object:Gem::Dependency
119
- name: bluecloth
120
90
  prerelease: false
121
- requirement: &id008 !ruby/object:Gem::Requirement
91
+ version_requirements: *2153692420
92
+ - !ruby/object:Gem::Dependency
93
+ name: bluecloth
94
+ requirement: &2153691960 !ruby/object:Gem::Requirement
122
95
  none: false
123
- requirements:
124
- - - ">="
125
- - !ruby/object:Gem::Version
126
- segments:
127
- - 0
128
- - 3
129
- - 5
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
130
99
  version: 0.3.5
131
100
  type: :development
132
- version_requirements: *id008
101
+ prerelease: false
102
+ version_requirements: *2153691960
133
103
  description: Duration type
134
- email:
104
+ email:
135
105
  - jose@peleteiro.net
136
- - bruno@bubble.com.br
106
+ - bruno@azisaka.com.br
137
107
  executables: []
138
-
139
108
  extensions: []
140
-
141
109
  extra_rdoc_files: []
142
-
143
- files:
110
+ files:
144
111
  - .gitignore
112
+ - .rvmrc
145
113
  - Gemfile
146
114
  - Gemfile.lock
147
115
  - LICENSE
@@ -152,44 +120,34 @@ files:
152
120
  - lib/duration/version.rb
153
121
  - lib/ruby-duration.rb
154
122
  - ruby-duration.gemspec
155
- - test/duration/test_i18n.rb
156
- - test/duration/test_mongoid.rb
157
123
  - test/fixtures/locales/pt.yml
158
124
  - test/helper.rb
159
125
  - test/test_duration.rb
160
- has_rdoc: true
126
+ - test/test_i18n.rb
127
+ - test/test_mongoid.rb
161
128
  homepage: http://github.com/peleteiro/ruby-duration
162
129
  licenses: []
163
-
164
130
  post_install_message:
165
- rdoc_options:
131
+ rdoc_options:
166
132
  - --charset=UTF-8
167
- require_paths:
133
+ require_paths:
168
134
  - lib
169
- required_ruby_version: !ruby/object:Gem::Requirement
135
+ required_ruby_version: !ruby/object:Gem::Requirement
170
136
  none: false
171
- requirements:
172
- - - ">="
173
- - !ruby/object:Gem::Version
174
- segments:
175
- - 0
176
- version: "0"
177
- required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ! '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
178
142
  none: false
179
- requirements:
180
- - - ">="
181
- - !ruby/object:Gem::Version
182
- segments:
183
- - 1
184
- - 3
185
- - 6
143
+ requirements:
144
+ - - ! '>='
145
+ - !ruby/object:Gem::Version
186
146
  version: 1.3.6
187
147
  requirements: []
188
-
189
148
  rubyforge_project: ruby-duration
190
- rubygems_version: 1.3.7
149
+ rubygems_version: 1.8.6
191
150
  signing_key:
192
151
  specification_version: 3
193
152
  summary: Duration type
194
153
  test_files: []
195
-
@@ -1,45 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- require 'helper'
3
-
4
- describe "mongoid support" do
5
-
6
- describe "#get - Mongoid's deserialization" do
7
- it "should return the duration given the total in seconds" do
8
- assert_equal Duration.new(90), Duration.get(90)
9
- end
10
-
11
- it "should return nil for nil serialized values" do
12
- assert_nil Duration.get(nil)
13
- end
14
- end
15
-
16
- describe "#set - Mongoid's serialization" do
17
- it "should serialize to nil given an invalid serialized value or nil" do
18
- assert_nil Duration.set([1,2,3])
19
- assert_nil Duration.set(nil)
20
- assert_nil Duration.set("")
21
- assert_nil Duration.set({})
22
- assert_nil Duration.set({:seconds => "", :hours => ""})
23
- assert_nil Duration.set({:x => 100, :seconds => ""})
24
- end
25
-
26
- it "should return total seconds given a duration" do
27
- assert_equal 90, Duration.set(Duration.new(:minutes => 1, :seconds => 30))
28
- end
29
-
30
- it "should return total seconds given a duration in seconds" do
31
- assert_equal 10, Duration.set(10)
32
- end
33
-
34
- it "should return total seconds given a duration in string" do
35
- assert_equal 10, Duration.set("10")
36
- assert_equal 10, Duration.set("10string")
37
- assert_equal 0, Duration.set("string") # not blank
38
- end
39
-
40
- it "should return total seconds given a duration in hash" do
41
- assert_equal 90, Duration.set(:minutes => 1, :seconds => 30)
42
- end
43
- end
44
-
45
- end