xduration 2.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/xduration.rb ADDED
@@ -0,0 +1,2 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require "#{File.dirname(__FILE__)}/duration"
@@ -0,0 +1,16 @@
1
+ da:
2
+ ruby_duration:
3
+ second: sekond
4
+ seconds: sekonder
5
+ minute: minut
6
+ minutes: minutter
7
+ hour: time
8
+ hours: timer
9
+ day: dag
10
+ days: dage
11
+ week: uge
12
+ weeks: uges
13
+ month: måned
14
+ months: måneder
15
+ year: år
16
+ years: år
@@ -0,0 +1,16 @@
1
+ pt:
2
+ ruby_duration:
3
+ second: segundo
4
+ seconds: segundos
5
+ minute: minuto
6
+ minutes: minutos
7
+ hour: hora
8
+ hours: horas
9
+ day: dia
10
+ days: dias
11
+ week: semana
12
+ weeks: semanas
13
+ month: mes
14
+ months: meses
15
+ year: ano
16
+ years: anos
data/test/helper.rb ADDED
@@ -0,0 +1,15 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'rubygems'
3
+ require 'minitest/spec'
4
+ begin
5
+ require 'simplecov'
6
+ SimpleCov.start do
7
+ end
8
+ rescue LoadError
9
+ end
10
+
11
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
12
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
13
+ require 'xduration'
14
+
15
+ MiniTest::Unit.autorun
@@ -0,0 +1,180 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ describe "Duration" do
5
+ it "should initialize given duration in seconds" do
6
+ d = Duration.new(90)
7
+ assert_equal 0, d.weeks
8
+ assert_equal 0, d.days
9
+ assert_equal 0, d.hours
10
+ assert_equal 1, d.minutes
11
+ assert_equal 30, d.seconds
12
+ assert_equal 90, d.total
13
+ assert_equal 1, d.total_minutes
14
+ assert_equal 0, d.total_hours
15
+ assert_equal 0, d.total_days
16
+ end
17
+
18
+ it "should initialize given duration in Hash" do
19
+ d = Duration.new(:weeks => 1, :days => 2, :hours => 3, :minutes => 4, :seconds => 5)
20
+ assert_equal 1, d.weeks
21
+ assert_equal 2, d.days
22
+ assert_equal 3, d.hours
23
+ assert_equal 4, d.minutes
24
+ assert_equal 5, d.seconds
25
+ assert_equal 788645, d.total
26
+ assert_equal 13144, d.total_minutes
27
+ assert_equal 219, d.total_hours
28
+ assert_equal 9, d.total_days
29
+ end
30
+
31
+ describe "mathematical operations" do
32
+
33
+ it "should +" do
34
+ assert_equal Duration.new(15), Duration.new(10) + 5
35
+ assert_equal Duration.new(15), Duration.new(10) + Duration.new(5)
36
+ end
37
+
38
+ it "should -" do
39
+ assert_equal Duration.new(5), Duration.new(10) - 5
40
+ assert_equal Duration.new(5), Duration.new(10) - Duration.new(5)
41
+ end
42
+
43
+ it "should *" do
44
+ assert_equal Duration.new(20), Duration.new(10) * 2
45
+ assert_equal Duration.new(20), Duration.new(10) * Duration.new(2)
46
+ end
47
+
48
+ it "should /" do
49
+ assert_equal Duration.new(5), Duration.new(10) / 2
50
+ assert_equal Duration.new(5), Duration.new(10) / Duration.new(2)
51
+ end
52
+
53
+ it "should %" do
54
+ assert_equal Duration.new(1), Duration.new(10) % 3
55
+ assert_equal Duration.new(1), Duration.new(10) % Duration.new(3)
56
+ end
57
+ end
58
+
59
+ describe "#format" do
60
+ it "should display units in plural form when needed" do
61
+ d = Duration.new(:weeks => 2, :days => 3, :hours => 4, :minutes => 5, :seconds => 6)
62
+ assert_equal "2 weeks 3 days 4 hours 5 minutes 6 seconds", d.format("%w %~w %d %~d %h %~h %m %~m %s %~s")
63
+ end
64
+
65
+ it "should display units in singular form when needed" do
66
+ d = Duration.new(:weeks => 1, :days => 1, :hours => 1, :minutes => 1, :seconds => 1)
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
+ end
69
+
70
+ it "should display total seconds" do
71
+ d = Duration.new(:hours => 1, :minutes => 15)
72
+ assert_equal "4500 seconds", d.format("%tsu")
73
+ end
74
+
75
+ it "should display total seconds in plural form when needed" do
76
+ d = Duration.new(:minutes => 1, :seconds => 1)
77
+ assert_equal "61 seconds", d.format("%tsu")
78
+ end
79
+
80
+ it "should display total minutes as number" do
81
+ d = Duration.new(:hours => 1, :minutes => 15)
82
+ assert_equal "75", d.format("%tm")
83
+ end
84
+
85
+ it 'should display total minutes with unit' do
86
+ d = Duration.new(:hours => 1, :minutes => 15)
87
+ assert_equal "75 minutes", d.format("%tmu")
88
+ end
89
+
90
+ it "should display total minutes in plural form when needed" do
91
+ d = Duration.new(:hours => 1, :minutes => 1)
92
+ assert_equal "61 minutes", d.format("%tmu")
93
+ end
94
+
95
+ it "should display total hours as number" do
96
+ d = Duration.new(:days => 2, :hours => 1)
97
+ assert_equal "49", d.format("%th")
98
+ end
99
+
100
+ it 'should display total hours with unit' do
101
+ d = Duration.new(:days => 2, :hours => 2)
102
+ assert_equal "50 hours", d.format("%thu")
103
+ end
104
+
105
+ it "should display total hours in plural form when needed" do
106
+ d = Duration.new(:days => 1, :hours => 1)
107
+ assert_equal "25 hours", d.format("%thu")
108
+ end
109
+
110
+ it "should display total days as number" do
111
+ d = Duration.new(:weeks => 1, :days => 3)
112
+ assert_equal "10", d.format("%td")
113
+ end
114
+
115
+ it 'should display total days with unit' do
116
+ d = Duration.new(:weeks => 1, :days => 2)
117
+ assert_equal "9 days", d.format("%tdu")
118
+ end
119
+
120
+ it "should display total days in plural form when needed" do
121
+ d = Duration.new(:weeks => 1, :days => 1)
122
+ assert_equal "8 days", d.format("%tdu")
123
+ end
124
+ end
125
+
126
+ describe "#iso_6801" do
127
+ it "should format seconds" do
128
+ d = Duration.new(:seconds => 1)
129
+ assert_equal "PT1S", d.iso8601
130
+ end
131
+
132
+ it "should format minutes" do
133
+ d = Duration.new(:minutes => 1)
134
+ assert_equal "PT1M", d.iso8601
135
+ end
136
+
137
+ it "should format hours" do
138
+ d = Duration.new(:hours => 1)
139
+ assert_equal "PT1H", d.iso8601
140
+ end
141
+
142
+ it "should format days" do
143
+ d = Duration.new(:days => 1)
144
+ assert_equal "P1D", d.iso8601
145
+ end
146
+
147
+ it "should format weeks" do
148
+ d = Duration.new(:weeks => 1)
149
+ assert_equal "P1W", d.iso8601
150
+ end
151
+
152
+ it "should format only with given values" do
153
+ d = Duration.new(:weeks => 1, :days => 2, :hours => 3, :minutes => 4, :seconds => 5)
154
+ assert_equal "P1W2DT3H4M5S", d.iso8601
155
+
156
+ d = Duration.new(:weeks => 1, :hours => 2, :seconds => 3)
157
+ assert_equal "P1WT2H3S", d.iso8601
158
+
159
+ d = Duration.new(:weeks => 1, :days => 2)
160
+ assert_equal "P1W2D", d.iso8601
161
+
162
+ d = Duration.new(:hours => 1, :seconds => 30)
163
+ assert_equal "PT1H30S", d.iso8601
164
+ end
165
+
166
+ end
167
+
168
+ describe "utilities methods" do
169
+ it "should respond to blank?" do
170
+ assert Duration.new.blank?
171
+ refute Duration.new(1).blank?
172
+ end
173
+
174
+ it "should respond to present?" do
175
+ refute Duration.new.present?
176
+ assert Duration.new(1).present?
177
+ end
178
+ end
179
+
180
+ end
data/test/test_i18n.rb ADDED
@@ -0,0 +1,82 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ I18n.load_path << Dir[File.join(File.dirname(__FILE__), 'fixtures', 'locales', '*.yml')]
5
+
6
+ describe "I18n" do
7
+ describe "when the locale is pt" do
8
+ before do
9
+ I18n.locale = :pt
10
+ end
11
+
12
+ it "should translate to segundo" do
13
+ assert_equal "segundo", Duration.new(:second => 1).format("%~s")
14
+ end
15
+
16
+ it "should translate to segundos" do
17
+ assert_equal "segundos", Duration.new.format("%~s")
18
+ end
19
+
20
+ it "should translate to minuto" do
21
+ assert_equal "minuto", Duration.new(:minute => 1).format("%~m")
22
+ end
23
+
24
+ it "should translate to minutos" do
25
+ assert_equal "minutos", Duration.new.format("%~m")
26
+ end
27
+
28
+ it "should translate to hora" do
29
+ assert_equal "hora", Duration.new(:hour => 1).format("%~h")
30
+ end
31
+
32
+ it "should translate to horas" do
33
+ assert_equal "horas", Duration.new.format("%~h")
34
+ end
35
+
36
+ it "should translate to dia" do
37
+ assert_equal "dia", Duration.new(:day => 1).format("%~d")
38
+ end
39
+
40
+ it "should translate to dias" do
41
+ assert_equal "dias", Duration.new.format("%~d")
42
+ end
43
+
44
+ it "should translate to semana" do
45
+ assert_equal "semana", Duration.new(:week => 1).format("%~w")
46
+ end
47
+
48
+ it "should translate to semanas" do
49
+ assert_equal "semanas", Duration.new.format("%~w")
50
+ end
51
+
52
+ it "should translate to mes" do
53
+ assert_equal "mes", Duration.new(:month => 1).format("%~o")
54
+ end
55
+
56
+ it "should translate to meses" do
57
+ assert_equal "meses", Duration.new.format("%~o")
58
+ end
59
+
60
+ it "should translate to ano" do
61
+ assert_equal "ano", Duration.new(:year => 1).format("%~y")
62
+ end
63
+
64
+ it "should translate to anos" do
65
+ assert_equal "anos", Duration.new.format("%~y")
66
+ end
67
+ end
68
+
69
+ describe "when the locale is da" do
70
+ before do
71
+ I18n.locale = :da
72
+ end
73
+
74
+ it "should translate to måned" do
75
+ assert_equal "måned", Duration.new(:month => 1).format("%~o")
76
+ end
77
+
78
+ it "should translate to måneder" do
79
+ assert_equal "måneder", Duration.new.format("%~o")
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,168 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'helper'
3
+ require 'duration/macros'
4
+
5
+ describe 'Numeric duration macros' do
6
+ it "duration" do
7
+ d = (60 + 30).duration
8
+ assert_equal 30, d.seconds
9
+ assert_equal 90, d.total
10
+ end
11
+
12
+ it "dseconds" do
13
+ d = 3.dseconds
14
+ assert_equal 3, d.seconds
15
+ assert_equal 3, d.total
16
+ end
17
+
18
+ it "dminutes" do
19
+ d = 3.dminutes
20
+ assert_equal 3, d.minutes
21
+ assert_equal 3 * 60, d.total
22
+ end
23
+
24
+ it "dhours" do
25
+ d = 3.dhours
26
+ assert_equal 3, d.hours
27
+ assert_equal 3 * 3600, d.total
28
+ end
29
+
30
+ it "ddays" do
31
+ d = 3.ddays
32
+ assert_equal 3, d.days
33
+ assert_equal 3 * 24 * 3600, d.total
34
+ end
35
+
36
+ it "dweeks" do
37
+ d = 3.dweeks
38
+ assert_equal 3, d.weeks
39
+ assert_equal 3 * 7 * 24 * 3600, d.total
40
+ end
41
+
42
+ it "dmonths" do
43
+ d = 3.dmonths
44
+ assert_equal 3, d.months
45
+ assert_equal 3 * 30 * 24 * 3600, d.total
46
+ end
47
+
48
+ it "dyears" do
49
+ d = 3.dyears
50
+ assert_equal 3, d.years
51
+ assert_equal 3 * 365.25 * 24 * 3600, d.total
52
+ end
53
+ end
54
+
55
+
56
+ describe Seconds do
57
+ it "should be a duration" do
58
+ d = Seconds.new(3)
59
+ assert_equal 3, d.seconds
60
+ assert_equal 3, d.total
61
+ end
62
+ end
63
+
64
+ describe Second do
65
+ it "should be a duration" do
66
+ d = Second.new
67
+ assert_equal 1, d.seconds
68
+ assert_equal 1, d.total
69
+ end
70
+ end
71
+
72
+ describe Minutes do
73
+ it "should be a duration" do
74
+ d = Minutes.new(3)
75
+ puts d.inspect
76
+ assert_equal 3, d.minutes
77
+ assert_equal (3 * 60), d.total
78
+ end
79
+ end
80
+
81
+ describe Minute do
82
+ it "should be a duration" do
83
+ d = Minute.new
84
+ assert_equal 1, d.minutes
85
+ assert_equal (1 * 60), d.total
86
+ end
87
+ end
88
+
89
+ describe Hours do
90
+ it "should be a duration" do
91
+ d = Hours.new(3)
92
+ assert_equal 3, d.hours
93
+ assert_equal (3 * 60 * 60), d.total
94
+ end
95
+ end
96
+
97
+ describe Hour do
98
+ it "should be a duration" do
99
+ d = Hour.new
100
+ assert_equal 1, d.hours
101
+ assert_equal (1 * 60 * 60), d.total
102
+ end
103
+ end
104
+
105
+ describe Days do
106
+ it "should be a duration" do
107
+ d = Days.new(3)
108
+ assert_equal 3, d.days
109
+ assert_equal (3 * 24 * 60 * 60), d.total
110
+ end
111
+ end
112
+
113
+ describe Day do
114
+ it "should be a duration" do
115
+ d = Day.new
116
+ assert_equal 1, d.days
117
+ assert_equal (1 * 24 * 60 * 60), d.total
118
+ end
119
+ end
120
+
121
+ describe Weeks do
122
+ it "should be a duration" do
123
+ d = Weeks.new(2)
124
+ assert_equal 2, d.weeks
125
+ assert_equal (14 * 24 * 60 * 60), d.total
126
+ end
127
+ end
128
+
129
+ describe Week do
130
+ it "should be a duration" do
131
+ d = Week.new
132
+ assert_equal 1, d.weeks
133
+ assert_equal (7 * 24 * 60 * 60), d.total
134
+ end
135
+ end
136
+
137
+ describe Months do
138
+ it "should be a duration" do
139
+ d = Months.new(2)
140
+ assert_equal 2, d.months
141
+ assert_equal (2 * 30 * 24 * 60 * 60), d.total
142
+ end
143
+ end
144
+
145
+ describe Month do
146
+ it "should be a duration" do
147
+ d = Month.new
148
+ assert_equal 1, d.months
149
+ assert_equal (30 * 24 * 60 * 60), d.total
150
+ end
151
+ end
152
+
153
+
154
+ describe Years do
155
+ it "should be a duration" do
156
+ d = Years.new(2)
157
+ assert_equal 2, d.years
158
+ assert_equal (2 * 365.25 * 24 * 60 * 60), d.total
159
+ end
160
+ end
161
+
162
+ describe Year do
163
+ it "should be a duration" do
164
+ d = Year.new
165
+ assert_equal 1, d.years
166
+ assert_equal (365.25 * 24 * 60 * 60), d.total
167
+ end
168
+ end
@@ -0,0 +1,70 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'helper'
3
+ require 'duration/mongoid'
4
+ require 'mongoid'
5
+
6
+ describe "mongoid support" do
7
+ before do
8
+ class MyModel
9
+ include Mongoid::Document
10
+ field :duration, :type => Duration
11
+ end
12
+ @model = MyModel.new
13
+ end
14
+
15
+ describe "assigning an integer value" do
16
+ it "should return the duration given the total in seconds" do
17
+ @model.duration = 90
18
+ assert_equal @model.duration, Duration.new(90)
19
+ end
20
+
21
+ it "assigning a nil value" do
22
+ @model.duration = nil
23
+ assert_nil @model.duration
24
+ end
25
+ end
26
+
27
+ describe "assigning an array" do
28
+ it "should return nil" do
29
+ @model.duration = [1,2,3]
30
+ assert_nil @model.duration
31
+ end
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
38
+ end
39
+ end
40
+
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
47
+ end
48
+ end
49
+
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
55
+ end
56
+ end
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
70
+ end
data/xduration.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/duration/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "xduration"
6
+ s.version = Duration::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Jose Peleteiro", "Bruno Azisaka Maciel", "Kristian Mandrup"]
9
+ s.email = ["jose@peleteiro.net", "bruno@azisaka.com.br", "kmandrup@gmail.com"]
10
+ s.homepage = "http://github.com/kristianmandrup/xduration"
11
+ s.summary = "Duration type"
12
+ s.description = "Duration type"
13
+
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+
16
+ s.add_dependency "activesupport", ">= 3.0.0"
17
+ s.add_dependency "i18n", ">= 0"
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"
26
+
27
+ s.files = `git ls-files`.split("\n")
28
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
29
+ s.require_path = 'lib'
30
+
31
+ s.rdoc_options = ["--charset=UTF-8"]
32
+ end