ruby-duration 2.1.4 → 3.0.0

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.
data/.rvmrc CHANGED
@@ -1 +1 @@
1
- rvm 1.9.3@ruby-duration
1
+ rvm 1.9.3@ruby-duration --create
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - jruby-19mode
5
+ - ruby-head
6
+ - jruby-head
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source :gemcutter
1
+ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in ruby-duration.gemspec
4
4
  gemspec
data/README.md CHANGED
@@ -16,7 +16,7 @@ Features
16
16
  * Construtor can receive the amount of time in seconds or a Hash with unit and amount of time.
17
17
  * Format method to display the time with i18n support.
18
18
  * Mongoid serialization support. Use `require 'duration/mongoid'`.
19
- * Tested on mri 1.8.7, ree 1.8.7, mri 1.9.2, jruby and rubinius. Kudos to rvm!
19
+ * Tested on mri 1.9.3 and jruby. Kudos to rvm!
20
20
 
21
21
 
22
22
  Show me the code
@@ -37,7 +37,10 @@ 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
+ The current version of this gem supports Mongoid >= [3.0.0](https://github.com/mongoid/mongoid).
41
+ For lower Mongoid versions try:
42
+ * < 3.0.0 tag [v2.1.4](https://github.com/peleteiro/ruby-duration/tree/v2.1.4)
43
+ * < 2.1.0 tag [v1.0.0](https://github.com/peleteiro/ruby-duration/tree/v1.0.0)
41
44
 
42
45
  require 'duration/mongoid'
43
46
 
@@ -46,6 +49,11 @@ The current version of this gem supports Mongoid >= [2.1.0](https://github.com/m
46
49
  field :duration, type => Duration
47
50
  end
48
51
 
52
+ ### Dependencies
53
+ The current version of this gem runs only on Ruby Versions >= 1.9.3.
54
+ If you are running a older version of Ruby try:
55
+ * tag [v2.1.4](https://github.com/peleteiro/ruby-duration/tree/v2.1.4)
56
+
49
57
 
50
58
  Note on Patches/Pull Requests
51
59
  -----------------------------
data/lib/duration.rb CHANGED
@@ -71,7 +71,7 @@ class Duration
71
71
  def %(other)
72
72
  Duration.new(@total % other.to_i)
73
73
  end
74
-
74
+
75
75
  %w(minutes hours days).each do |meth|
76
76
  define_method("total_#{meth}") { @total / MULTIPLES[meth.to_sym] }
77
77
  end
@@ -134,7 +134,7 @@ class Duration
134
134
  #
135
135
  # I18n.load_path << "path/to/your/locale"
136
136
  # I18n.locale = :your_locale
137
- #
137
+ #
138
138
  # And you must use the following structure (example) for your locale file:
139
139
  # pt:
140
140
  # ruby_duration:
@@ -182,7 +182,7 @@ class Duration
182
182
 
183
183
  alias_method :to_i, :total
184
184
  alias_method :strftime, :format
185
-
185
+
186
186
  private
187
187
 
188
188
  # Calculates the duration from seconds and figures out what the actual
@@ -219,4 +219,4 @@ private
219
219
 
220
220
  I18n.t(label, :scope => :ruby_duration, :default => label.to_s)
221
221
  end
222
- end
222
+ end
@@ -5,32 +5,52 @@ require "mongoid/fields"
5
5
  module Mongoid
6
6
  module Fields
7
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
8
+
9
+ # Instantiates a new Duration object
10
+ def initialize(seconds)
16
11
  ::Duration.new(seconds)
17
12
  end
18
13
 
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)}
14
+ # Converts the Duration object into a MongoDB friendly value.
15
+ def mongoize
16
+ self.to_i
17
+ end
18
+
19
+ class << self
20
+ # Deserialize a Duration given the amount of seconds stored by Mongodb
21
+ #
22
+ # @param [Integer, nil] duration in seconds
23
+ # @return [Duration] deserialized Duration
24
+ def demongoize(seconds)
25
+ return if !seconds
26
+ ::Duration.new(seconds)
27
+ end
28
+
29
+ # Serialize a Duration or a Hash (with duration units) or a amount of seconds to
30
+ # a BSON serializable type.
31
+ #
32
+ # @param [Duration, Hash, Integer] value
33
+ # @return [Integer] duration in seconds
34
+ def mongoize(value)
28
35
  return if value.blank?
29
- ::Duration.new(value).to_i
30
- elsif value.respond_to?(:to_i)
31
- value.to_i
36
+ if value.is_a?(Hash)
37
+ value.delete_if{|k, v| v.blank? || !::Duration::UNITS.include?(k.to_sym)}
38
+ return if value.blank?
39
+ ::Duration.new(value).to_i
40
+ elsif value.respond_to?(:to_i)
41
+ value.to_i
42
+ end
43
+ end
44
+
45
+ # Converts the object that was supplied to a criteria and converts it
46
+ # into a database friendly form.
47
+ def evolve(object)
48
+ case object
49
+ when ::Duration then object.mongoize
50
+ else object
51
+ end
32
52
  end
33
53
  end
34
54
  end
35
55
  end
36
- end
56
+ end
@@ -1,3 +1,3 @@
1
1
  class Duration
2
- VERSION = "2.1.4"
2
+ VERSION = "3.0.0"
3
3
  end
data/lib/ruby-duration.rb CHANGED
@@ -1,2 +1,2 @@
1
1
  # -*- encoding: utf-8 -*-
2
- require "#{File.dirname(__FILE__)}/duration"
2
+ require "#{File.dirname(__FILE__)}/duration"
@@ -22,8 +22,7 @@ Gem::Specification.new do |s|
22
22
  s.add_development_dependency "yard", ">= 0"
23
23
  s.add_development_dependency "rake", ">= 0"
24
24
  s.add_development_dependency "simplecov", ">= 0.3.5"
25
- s.add_development_dependency "bluecloth", ">= 0.3.5"
26
- s.add_development_dependency "mongoid", "~> 2.4.0"
25
+ s.add_development_dependency "mongoid", ">= 3.0.0"
27
26
 
28
27
  s.files = `git ls-files`.split("\n")
29
28
  s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
@@ -9,4 +9,4 @@ pt:
9
9
  day: dia
10
10
  days: dias
11
11
  week: semana
12
- weeks: semanas
12
+ weeks: semanas
@@ -27,29 +27,29 @@ describe "Duration" do
27
27
  assert_equal 219, d.total_hours
28
28
  assert_equal 9, d.total_days
29
29
  end
30
-
30
+
31
31
  describe "mathematical operations" do
32
-
32
+
33
33
  it "should +" do
34
34
  assert_equal Duration.new(15), Duration.new(10) + 5
35
35
  assert_equal Duration.new(15), Duration.new(10) + Duration.new(5)
36
36
  end
37
-
37
+
38
38
  it "should -" do
39
39
  assert_equal Duration.new(5), Duration.new(10) - 5
40
40
  assert_equal Duration.new(5), Duration.new(10) - Duration.new(5)
41
41
  end
42
-
42
+
43
43
  it "should *" do
44
44
  assert_equal Duration.new(20), Duration.new(10) * 2
45
45
  assert_equal Duration.new(20), Duration.new(10) * Duration.new(2)
46
46
  end
47
-
47
+
48
48
  it "should /" do
49
49
  assert_equal Duration.new(5), Duration.new(10) / 2
50
50
  assert_equal Duration.new(5), Duration.new(10) / Duration.new(2)
51
51
  end
52
-
52
+
53
53
  it "should %" do
54
54
  assert_equal Duration.new(1), Duration.new(10) % 3
55
55
  assert_equal Duration.new(1), Duration.new(10) % Duration.new(3)
@@ -66,79 +66,79 @@ describe "Duration" do
66
66
  d = Duration.new(:weeks => 1, :days => 1, :hours => 1, :minutes => 1, :seconds => 1)
67
67
  assert_equal "1 week 1 day 1 hour 1 minute 1 second", d.format("%w %~w %d %~d %h %~h %m %~m %s %~s")
68
68
  end
69
-
69
+
70
70
  it "should display total seconds" do
71
71
  d = Duration.new(:hours => 1, :minutes => 15)
72
72
  assert_equal "4500 seconds", d.format("%tsu")
73
73
  end
74
-
74
+
75
75
  it "should display total seconds in plural form when needed" do
76
76
  d = Duration.new(:minutes => 1, :seconds => 1)
77
77
  assert_equal "61 seconds", d.format("%tsu")
78
78
  end
79
-
79
+
80
80
  it "should display total minutes as number" do
81
81
  d = Duration.new(:hours => 1, :minutes => 15)
82
82
  assert_equal "75", d.format("%tm")
83
83
  end
84
-
84
+
85
85
  it 'should display total minutes with unit' do
86
86
  d = Duration.new(:hours => 1, :minutes => 15)
87
87
  assert_equal "75 minutes", d.format("%tmu")
88
88
  end
89
-
89
+
90
90
  it "should display total minutes in plural form when needed" do
91
91
  d = Duration.new(:hours => 1, :minutes => 1)
92
92
  assert_equal "61 minutes", d.format("%tmu")
93
93
  end
94
-
94
+
95
95
  it "should display total hours as number" do
96
96
  d = Duration.new(:days => 2, :hours => 1)
97
97
  assert_equal "49", d.format("%th")
98
98
  end
99
-
99
+
100
100
  it 'should display total hours with unit' do
101
101
  d = Duration.new(:days => 2, :hours => 2)
102
102
  assert_equal "50 hours", d.format("%thu")
103
103
  end
104
-
104
+
105
105
  it "should display total hours in plural form when needed" do
106
106
  d = Duration.new(:days => 1, :hours => 1)
107
107
  assert_equal "25 hours", d.format("%thu")
108
108
  end
109
-
109
+
110
110
  it "should display total days as number" do
111
111
  d = Duration.new(:weeks => 1, :days => 3)
112
112
  assert_equal "10", d.format("%td")
113
113
  end
114
-
114
+
115
115
  it 'should display total days with unit' do
116
116
  d = Duration.new(:weeks => 1, :days => 2)
117
117
  assert_equal "9 days", d.format("%tdu")
118
118
  end
119
-
119
+
120
120
  it "should display total days in plural form when needed" do
121
121
  d = Duration.new(:weeks => 1, :days => 1)
122
122
  assert_equal "8 days", d.format("%tdu")
123
123
  end
124
124
  end
125
-
125
+
126
126
  describe "#iso_6801" do
127
127
  it "should format seconds" do
128
128
  d = Duration.new(:seconds => 1)
129
129
  assert_equal "PT1S", d.iso8601
130
130
  end
131
-
131
+
132
132
  it "should format minutes" do
133
133
  d = Duration.new(:minutes => 1)
134
134
  assert_equal "PT1M", d.iso8601
135
135
  end
136
-
136
+
137
137
  it "should format hours" do
138
138
  d = Duration.new(:hours => 1)
139
139
  assert_equal "PT1H", d.iso8601
140
140
  end
141
-
141
+
142
142
  it "should format days" do
143
143
  d = Duration.new(:days => 1)
144
144
  assert_equal "P1D", d.iso8601
@@ -164,17 +164,17 @@ describe "Duration" do
164
164
  end
165
165
 
166
166
  end
167
-
167
+
168
168
  describe "utilities methods" do
169
169
  it "should respond to blank?" do
170
170
  assert Duration.new.blank?
171
171
  refute Duration.new(1).blank?
172
172
  end
173
-
173
+
174
174
  it "should respond to present?" do
175
175
  refute Duration.new.present?
176
176
  assert Duration.new(1).present?
177
177
  end
178
178
  end
179
179
 
180
- end
180
+ end
data/test/test_i18n.rb CHANGED
@@ -3,7 +3,7 @@ require 'helper'
3
3
 
4
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
@@ -30,7 +30,7 @@ describe "I18n" do
30
30
  end
31
31
 
32
32
  it "should translate to horas" do
33
- assert_equal "horas", Duration.new.format("%~h")
33
+ assert_equal "horas", Duration.new.format("%~h")
34
34
  end
35
35
 
36
36
  it "should translate to dia" do
data/test/test_mongoid.rb CHANGED
@@ -7,11 +7,11 @@ describe "mongoid support" do
7
7
  before do
8
8
  class MyModel
9
9
  include Mongoid::Document
10
- field :duration, type: Duration
10
+ field :duration, :type => Duration
11
11
  end
12
12
  @model = MyModel.new
13
13
  end
14
-
14
+
15
15
  describe "assigning an integer value" do
16
16
  it "should return the duration given the total in seconds" do
17
17
  @model.duration = 90
@@ -23,14 +23,14 @@ describe "mongoid support" do
23
23
  assert_nil @model.duration
24
24
  end
25
25
  end
26
-
26
+
27
27
  describe "assigning an array" do
28
28
  it "should return nil" do
29
29
  @model.duration = [1,2,3]
30
30
  assert_nil @model.duration
31
31
  end
32
32
  end
33
-
33
+
34
34
  describe "assigning a valid hash" do
35
35
  it "should return total seconds given a duration in hash" do
36
36
  @model.duration = { :minutes => 1, :seconds => 30 }
@@ -59,12 +59,12 @@ describe "mongoid support" do
59
59
  it "should return total seconds given a duration in string" do
60
60
  @model.duration = "10"
61
61
  assert_equal Duration.new(10), @model.duration
62
-
62
+
63
63
  @model.duration = "10string"
64
64
  assert_equal Duration.new(10), @model.duration
65
-
65
+
66
66
  @model.duration = "string"
67
67
  assert_equal Duration.new(0), @model.duration
68
68
  end
69
69
  end
70
- end
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
4
+ version: 3.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-04-28 00:00:00.000000000 Z
13
+ date: 2013-03-22 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -125,13 +125,13 @@ dependencies:
125
125
  - !ruby/object:Gem::Version
126
126
  version: 0.3.5
127
127
  - !ruby/object:Gem::Dependency
128
- name: bluecloth
128
+ name: mongoid
129
129
  requirement: !ruby/object:Gem::Requirement
130
130
  none: false
131
131
  requirements:
132
132
  - - ! '>='
133
133
  - !ruby/object:Gem::Version
134
- version: 0.3.5
134
+ version: 3.0.0
135
135
  type: :development
136
136
  prerelease: false
137
137
  version_requirements: !ruby/object:Gem::Requirement
@@ -139,23 +139,7 @@ dependencies:
139
139
  requirements:
140
140
  - - ! '>='
141
141
  - !ruby/object:Gem::Version
142
- version: 0.3.5
143
- - !ruby/object:Gem::Dependency
144
- name: mongoid
145
- requirement: !ruby/object:Gem::Requirement
146
- none: false
147
- requirements:
148
- - - ~>
149
- - !ruby/object:Gem::Version
150
- version: 2.4.0
151
- type: :development
152
- prerelease: false
153
- version_requirements: !ruby/object:Gem::Requirement
154
- none: false
155
- requirements:
156
- - - ~>
157
- - !ruby/object:Gem::Version
158
- version: 2.4.0
142
+ version: 3.0.0
159
143
  description: Duration type
160
144
  email:
161
145
  - jose@peleteiro.net
@@ -166,6 +150,7 @@ extra_rdoc_files: []
166
150
  files:
167
151
  - .gitignore
168
152
  - .rvmrc
153
+ - .travis.yml
169
154
  - Gemfile
170
155
  - LICENSE
171
156
  - README.md
@@ -193,6 +178,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
193
178
  - - ! '>='
194
179
  - !ruby/object:Gem::Version
195
180
  version: '0'
181
+ segments:
182
+ - 0
183
+ hash: 370806958351175358
196
184
  required_rubygems_version: !ruby/object:Gem::Requirement
197
185
  none: false
198
186
  requirements:
@@ -201,7 +189,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
201
189
  version: 1.3.6
202
190
  requirements: []
203
191
  rubyforge_project: ruby-duration
204
- rubygems_version: 1.8.18
192
+ rubygems_version: 1.8.25
205
193
  signing_key:
206
194
  specification_version: 3
207
195
  summary: Duration type