xduration 2.2.3 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f3f0711581bf23394090cd5d2527d1ed88d5514e
4
+ data.tar.gz: 5cc07346c97931605e0b61d91e452ec0850ffe4c
5
+ SHA512:
6
+ metadata.gz: a9eff3904f88e24b96bb0f21d360628b971ec55415303f0f9e5b081e1fb78f38bec737be7ee6a1635c2275f650f1857280429100dedfdace4d92917d4a598736
7
+ data.tar.gz: d1b82e8ed44416f61f1713a28c5c513e74f7ea70a65a0f535486aeb51c52ad126f37a8738cb0f1c9936bd625704d846fa835e5e3c49a6cd39349055ebae57889
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ ## 3.0.0
2
+
3
+ Updated for Duration 3.0.0 functionality to support Mongoid 3.0+
4
+
1
5
  ## 0.2.2
2
6
 
3
7
  * Renamed to 'xduration'
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 xduration.gemspec
4
4
  gemspec
data/README.md CHANGED
@@ -1,6 +1,61 @@
1
1
  xduration
2
2
  =============
3
3
 
4
+ xduration is an extension of the [ruby-duration](https://github.com/peleteiro/ruby-duration) gem. It additionally adds support for longer time units such as weeks, months and years.
5
+
6
+ Specific time unit classes that subclass and specialize `Duration` are made available:
7
+
8
+ * Decades, Decade
9
+ * Years, Year
10
+ * Months, Month
11
+ * Weeks, Week
12
+ * Days, Day
13
+ * Hours, Hour
14
+ * Minutes, Minute
15
+ * Seconds, Second
16
+
17
+ The class `Fixnum` is patched with additional `am` and `pm` methods to return the time, fx `1.am` is today at `01:00 am`.
18
+
19
+ The class `Numeric` is patched with methods such as:
20
+
21
+ * duration
22
+ * dseconds, dsecond, ..., dhours, ddays, dweeks, dyears, ddecades
23
+
24
+ Each method returns an appropriate instance of Hours, Decades and so on..
25
+
26
+ `5.dhours # => Hours.new(5)`
27
+
28
+ `5.duration # => Duration.new(5) # 5 seconds`
29
+
30
+ ## Rails
31
+
32
+ The locale file used for text output, fx for `duration.i18n_for(:weeks)`.
33
+
34
+ /config/locales/duration/[i18n code].yml'
35
+
36
+ Here an example for a danish (da) locale file:
37
+
38
+ ```yaml
39
+ da:
40
+ ruby_duration:
41
+ second: sekond
42
+ seconds: sekonder
43
+ minute: minut
44
+ minutes: minutter
45
+ hour: time
46
+ hours: timer
47
+ day: dag
48
+ days: dage
49
+ week: uge
50
+ weeks: uges
51
+ month: måned
52
+ months: måneder
53
+ year: år
54
+ years: år
55
+ ```
56
+
57
+ For more advanced Duration functionality, the [Timespan](https://github.com/kristianmandrup/timespan) gem is recommended ;)
58
+
4
59
  Duration is an immutable type that represents some amount of time with accuracy in seconds.
5
60
 
6
61
  A lot of the code and inspirations is borrowed from [duration](http://rubyforge.org/projects/duration)
@@ -10,7 +65,7 @@ lib, which is a **mutable** Duration type with lot more features but lack of tes
10
65
  Features
11
66
  --------
12
67
 
13
- * Representation of time in weeks, days, hours, minutes and seconds.
68
+ * Representation of time in years, months, weeks, days, hours, minutes and seconds.
14
69
  * Construtor can receive the amount of time in seconds or a Hash with unit and amount of time.
15
70
  * Format method to display the time with i18n support.
16
71
  * Mongoid serialization support. Use `require 'duration/mongoid'`.
data/lib/duration.rb CHANGED
@@ -80,7 +80,7 @@ class Duration
80
80
  Duration.new(@total % other.to_i)
81
81
  end
82
82
 
83
- %w(minutes hours days).each do |meth|
83
+ %w(minutes hours days weeks months years).each do |meth|
84
84
  define_method("total_#{meth}") { @total / MULTIPLES[meth.to_sym] }
85
85
  end
86
86
 
@@ -89,6 +89,8 @@ class Duration
89
89
  def iso8601
90
90
  output = 'P'
91
91
 
92
+ output << "#{years}Y" if years > 0
93
+ output << "#{months}M" if months > 0
92
94
  output << "#{weeks}W" if weeks > 0
93
95
  output << "#{days}D" if days > 0
94
96
  if seconds > 0 || minutes > 0 || hours > 0
@@ -5,30 +5,50 @@ 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
@@ -1,3 +1,3 @@
1
1
  class Duration
2
- VERSION = "2.2.3"
2
+ VERSION = "3.0.0"
3
3
  end
data/xduration.gemspec CHANGED
@@ -8,21 +8,21 @@ Gem::Specification.new do |s|
8
8
  s.authors = ["Jose Peleteiro", "Bruno Azisaka Maciel", "Kristian Mandrup"]
9
9
  s.email = ["jose@peleteiro.net", "bruno@azisaka.com.br", "kmandrup@gmail.com"]
10
10
  s.homepage = "http://github.com/kristianmandrup/xduration"
11
- s.summary = "Duration type"
12
- s.description = "Duration type"
11
+ s.summary = "Duration type with Mongoid 2.4+ support"
12
+ s.description = "Duration with support for longer time periods"
13
13
 
14
14
  s.required_rubygems_version = ">= 1.3.6"
15
15
 
16
16
  s.add_dependency "activesupport", ">= 3.0.0"
17
- s.add_dependency "i18n", ">= 0"
17
+ s.add_dependency "i18n", ">= 0.6"
18
18
 
19
- s.add_development_dependency "bundler", ">= 1.0.0"
20
- s.add_development_dependency "minitest", ">= 0"
21
- s.add_development_dependency "yard", ">= 0"
22
- s.add_development_dependency "rake", ">= 0"
23
- s.add_development_dependency "simplecov", ">= 0.3.5"
24
- s.add_development_dependency "mongoid", "~> 2.4.0"
25
- s.add_development_dependency "rails", ">= 3.1"
19
+ s.add_development_dependency "bundler", ">= 1.0.0"
20
+ s.add_development_dependency "minitest", ">= 0"
21
+ s.add_development_dependency "yard", ">= 0"
22
+ s.add_development_dependency "rake", ">= 0.9.2"
23
+ s.add_development_dependency "simplecov", ">= 0.5"
24
+ s.add_development_dependency "mongoid", ">= 2.4.0"
25
+ s.add_development_dependency "rails", ">= 3.1"
26
26
 
27
27
  s.files = `git ls-files`.split("\n")
28
28
  s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xduration
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.3
5
- prerelease:
4
+ version: 3.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Jose Peleteiro
@@ -11,153 +10,135 @@ authors:
11
10
  autorequire:
12
11
  bindir: bin
13
12
  cert_chain: []
14
- date: 2012-05-07 00:00:00.000000000 Z
13
+ date: 2013-03-13 00:00:00.000000000 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: activesupport
18
17
  requirement: !ruby/object:Gem::Requirement
19
- none: false
20
18
  requirements:
21
- - - ! '>='
19
+ - - '>='
22
20
  - !ruby/object:Gem::Version
23
21
  version: 3.0.0
24
22
  type: :runtime
25
23
  prerelease: false
26
24
  version_requirements: !ruby/object:Gem::Requirement
27
- none: false
28
25
  requirements:
29
- - - ! '>='
26
+ - - '>='
30
27
  - !ruby/object:Gem::Version
31
28
  version: 3.0.0
32
29
  - !ruby/object:Gem::Dependency
33
30
  name: i18n
34
31
  requirement: !ruby/object:Gem::Requirement
35
- none: false
36
32
  requirements:
37
- - - ! '>='
33
+ - - '>='
38
34
  - !ruby/object:Gem::Version
39
- version: '0'
35
+ version: '0.6'
40
36
  type: :runtime
41
37
  prerelease: false
42
38
  version_requirements: !ruby/object:Gem::Requirement
43
- none: false
44
39
  requirements:
45
- - - ! '>='
40
+ - - '>='
46
41
  - !ruby/object:Gem::Version
47
- version: '0'
42
+ version: '0.6'
48
43
  - !ruby/object:Gem::Dependency
49
44
  name: bundler
50
45
  requirement: !ruby/object:Gem::Requirement
51
- none: false
52
46
  requirements:
53
- - - ! '>='
47
+ - - '>='
54
48
  - !ruby/object:Gem::Version
55
49
  version: 1.0.0
56
50
  type: :development
57
51
  prerelease: false
58
52
  version_requirements: !ruby/object:Gem::Requirement
59
- none: false
60
53
  requirements:
61
- - - ! '>='
54
+ - - '>='
62
55
  - !ruby/object:Gem::Version
63
56
  version: 1.0.0
64
57
  - !ruby/object:Gem::Dependency
65
58
  name: minitest
66
59
  requirement: !ruby/object:Gem::Requirement
67
- none: false
68
60
  requirements:
69
- - - ! '>='
61
+ - - '>='
70
62
  - !ruby/object:Gem::Version
71
63
  version: '0'
72
64
  type: :development
73
65
  prerelease: false
74
66
  version_requirements: !ruby/object:Gem::Requirement
75
- none: false
76
67
  requirements:
77
- - - ! '>='
68
+ - - '>='
78
69
  - !ruby/object:Gem::Version
79
70
  version: '0'
80
71
  - !ruby/object:Gem::Dependency
81
72
  name: yard
82
73
  requirement: !ruby/object:Gem::Requirement
83
- none: false
84
74
  requirements:
85
- - - ! '>='
75
+ - - '>='
86
76
  - !ruby/object:Gem::Version
87
77
  version: '0'
88
78
  type: :development
89
79
  prerelease: false
90
80
  version_requirements: !ruby/object:Gem::Requirement
91
- none: false
92
81
  requirements:
93
- - - ! '>='
82
+ - - '>='
94
83
  - !ruby/object:Gem::Version
95
84
  version: '0'
96
85
  - !ruby/object:Gem::Dependency
97
86
  name: rake
98
87
  requirement: !ruby/object:Gem::Requirement
99
- none: false
100
88
  requirements:
101
- - - ! '>='
89
+ - - '>='
102
90
  - !ruby/object:Gem::Version
103
- version: '0'
91
+ version: 0.9.2
104
92
  type: :development
105
93
  prerelease: false
106
94
  version_requirements: !ruby/object:Gem::Requirement
107
- none: false
108
95
  requirements:
109
- - - ! '>='
96
+ - - '>='
110
97
  - !ruby/object:Gem::Version
111
- version: '0'
98
+ version: 0.9.2
112
99
  - !ruby/object:Gem::Dependency
113
100
  name: simplecov
114
101
  requirement: !ruby/object:Gem::Requirement
115
- none: false
116
102
  requirements:
117
- - - ! '>='
103
+ - - '>='
118
104
  - !ruby/object:Gem::Version
119
- version: 0.3.5
105
+ version: '0.5'
120
106
  type: :development
121
107
  prerelease: false
122
108
  version_requirements: !ruby/object:Gem::Requirement
123
- none: false
124
109
  requirements:
125
- - - ! '>='
110
+ - - '>='
126
111
  - !ruby/object:Gem::Version
127
- version: 0.3.5
112
+ version: '0.5'
128
113
  - !ruby/object:Gem::Dependency
129
114
  name: mongoid
130
115
  requirement: !ruby/object:Gem::Requirement
131
- none: false
132
116
  requirements:
133
- - - ~>
117
+ - - '>='
134
118
  - !ruby/object:Gem::Version
135
119
  version: 2.4.0
136
120
  type: :development
137
121
  prerelease: false
138
122
  version_requirements: !ruby/object:Gem::Requirement
139
- none: false
140
123
  requirements:
141
- - - ~>
124
+ - - '>='
142
125
  - !ruby/object:Gem::Version
143
126
  version: 2.4.0
144
127
  - !ruby/object:Gem::Dependency
145
128
  name: rails
146
129
  requirement: !ruby/object:Gem::Requirement
147
- none: false
148
130
  requirements:
149
- - - ! '>='
131
+ - - '>='
150
132
  - !ruby/object:Gem::Version
151
133
  version: '3.1'
152
134
  type: :development
153
135
  prerelease: false
154
136
  version_requirements: !ruby/object:Gem::Requirement
155
- none: false
156
137
  requirements:
157
- - - ! '>='
138
+ - - '>='
158
139
  - !ruby/object:Gem::Version
159
140
  version: '3.1'
160
- description: Duration type
141
+ description: Duration with support for longer time periods
161
142
  email:
162
143
  - jose@peleteiro.net
163
144
  - bruno@azisaka.com.br
@@ -202,28 +183,27 @@ files:
202
183
  - xduration.gemspec
203
184
  homepage: http://github.com/kristianmandrup/xduration
204
185
  licenses: []
186
+ metadata: {}
205
187
  post_install_message:
206
188
  rdoc_options:
207
189
  - --charset=UTF-8
208
190
  require_paths:
209
191
  - lib
210
192
  required_ruby_version: !ruby/object:Gem::Requirement
211
- none: false
212
193
  requirements:
213
- - - ! '>='
194
+ - - '>='
214
195
  - !ruby/object:Gem::Version
215
196
  version: '0'
216
197
  required_rubygems_version: !ruby/object:Gem::Requirement
217
- none: false
218
198
  requirements:
219
- - - ! '>='
199
+ - - '>='
220
200
  - !ruby/object:Gem::Version
221
201
  version: 1.3.6
222
202
  requirements: []
223
203
  rubyforge_project:
224
- rubygems_version: 1.8.24
204
+ rubygems_version: 2.0.0
225
205
  signing_key:
226
- specification_version: 3
227
- summary: Duration type
206
+ specification_version: 4
207
+ summary: Duration type with Mongoid 2.4+ support
228
208
  test_files: []
229
209
  has_rdoc: