relativity 0.0.2 → 0.0.3

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/Changelog.rdoc CHANGED
@@ -1,3 +1,8 @@
1
+ 0.0.3 2012-01-12
2
+
3
+ * Change RelativeTime into DayTime
4
+ * Added an initial DayTimeRange class
5
+
1
6
  0.0.2 2012-01-12
2
7
 
3
8
  * Add the MIT license
data/Guardfile ADDED
@@ -0,0 +1,7 @@
1
+ guard 'rspec', :version => 2 do
2
+
3
+ watch(%r{^spec/.+_spec\.rb$}) { "spec" }
4
+ watch(%r{^spec/spec_support.rb$}) { "spec" }
5
+ watch(%r{^lib/(.+)\.rb$}) { "spec" }
6
+
7
+ end
data/README.rdoc CHANGED
@@ -3,19 +3,19 @@
3
3
  Time, Date and DateTime classes don't have a mode for working with relative
4
4
  time inside 1 day (or 1 week, 1 month, 1 quarter, etc.).
5
5
 
6
- A RelativeTime object, relative to a day or a week, is useful to describe
7
- e.g. opening hours of a business. In a next phase, a RelativeTimeRange
8
- will be built on top, so the ranges of opening hours can be represented.
6
+ A relative time object, relative to a day or a week, is useful to describe
7
+ e.g. opening hours of a business. In a next phase, time ranges will
8
+ be built on top, so the ranges of opening hours can be represented.
9
9
 
10
10
  == Example
11
11
 
12
12
  require 'relativity'
13
- opens_at = RelativeTime.new(9) #=> 09:00:00
14
- closes_at = RelativeTime.new(18,30) #=> 18:30:00
13
+ opens_at = DayTime.new(9) #=> 09:00:00
14
+ closes_at = DayTime.new(18,30) #=> 18:30:00
15
15
 
16
- rt = RelativeTime.new #=> 21:17:40
17
- rt.seconds_since_midnight #=> #<BigDecimal:9d35d48,'0.7666056825 7238E5',18(45)>
18
- rt.hours #=> 21
19
- rt.minutes #=> 17
20
- rt.seconds #=> 40
21
- rt.nano_seconds #=> 568257238
16
+ dt = DayTime.new #=> 21:17:40
17
+ dt.seconds_since_start #=> #<BigDecimal:9d35d48,'0.7666056825 7238E5',18(45)>
18
+ dt.hours #=> 21
19
+ dt.minutes #=> 17
20
+ dt.seconds #=> 40
21
+ dt.nano_seconds #=> 568257238
data/TODO ADDED
@@ -0,0 +1,16 @@
1
+ Refactoring:
2
+
3
+ * make an Exception class
4
+
5
+ Features to be implemented:
6
+
7
+ week_time = WeekTime.new
8
+ week_day = WeekDay.new
9
+
10
+ day_times (array of day_time entries)
11
+
12
+ day_time_range (range of days_time entries)
13
+ week_time_range
14
+ week_day_range
15
+
16
+ ActiveRecord coupling (Aggregation ?)
@@ -1,8 +1,8 @@
1
- class RelativeTime
1
+ class DayTime
2
2
 
3
3
  require 'bigdecimal'
4
4
 
5
- attr_reader :seconds_since_midnight
5
+ attr_reader :seconds_since_start
6
6
 
7
7
  def initialize(hours = nil, minutes = nil, seconds = nil, nano_seconds = nil)
8
8
  super()
@@ -18,25 +18,25 @@ class RelativeTime
18
18
  ss = t.sec
19
19
  nn = t.nsec
20
20
  end
21
- @seconds_since_midnight = ((hh * 3600) + (mm * 60) + ss + BigDecimal(nn)/BigDecimal(1000000000))%(24*3600)
21
+ @seconds_since_start = ((hh * 3600) + (mm * 60) + ss + BigDecimal(nn)/BigDecimal(1000000000))%(24*3600)
22
22
  end
23
23
 
24
24
  def hours
25
- hours = Integer(@seconds_since_midnight/(3600)).tap do |h|
26
- raise "Internal ERROR in RelativeTime; hours is #{h}" if (h < 0 || h > 23)
25
+ hours = Integer(@seconds_since_start/(3600)).tap do |h|
26
+ raise "Internal ERROR in DayTime; hours is #{h}" if (h < 0 || h > 23)
27
27
  end
28
28
  end
29
29
 
30
30
  def minutes
31
- Integer(@seconds_since_midnight / 60) % 60
31
+ Integer(@seconds_since_start / 60) % 60
32
32
  end
33
33
 
34
34
  def seconds
35
- Integer(@seconds_since_midnight) % 60
35
+ Integer(@seconds_since_start) % 60
36
36
  end
37
37
 
38
38
  def nano_seconds
39
- Integer((@seconds_since_midnight % 1) * 1000000000)
39
+ Integer((@seconds_since_start % 1) * 1000000000)
40
40
  end
41
41
 
42
42
  def to_s
@@ -0,0 +1,6 @@
1
+ class DayTimeRange
2
+
3
+ def initialize(first, second = nil)
4
+ super()
5
+ end
6
+ end
@@ -1,3 +1,3 @@
1
1
  module Relativity
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/relativity.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require 'relativity/version'
2
- require 'relativity/relative_time'
2
+ require 'relativity/day_time'
3
+ require 'relativity/day_time_range'
data/relativity.gemspec CHANGED
@@ -18,5 +18,9 @@ Gem::Specification.new do |s|
18
18
 
19
19
  # specify any dependencies here; for example:
20
20
  s.add_development_dependency 'rspec', '>= 2.7'
21
- s.add_dependency 'bundler', '>= 1.0'
21
+ s.add_development_dependency 'guard-rspec', '>= 0.5'
22
+ if RUBY_PLATFORM.match(/linux/)
23
+ s.add_development_dependency 'rb-inotify'
24
+ s.add_development_dependency 'libnotify'
25
+ end
22
26
  end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ describe DayTime do
4
+
5
+ it "new build a DayTime" do
6
+ lambda { subject }.should_not raise_error
7
+ end
8
+
9
+ it "seconds_since_start" do
10
+ lambda { subject.seconds_since_start }.should_not raise_error
11
+ end
12
+
13
+ it "new creates a DayTime close to now" do
14
+ rt_s = subject.seconds_since_start
15
+ t = Time.new
16
+ t_s = (t.hour*60 + t.min)*60 + t.sec + t.nsec/1E9 # seconds_since_start from Time
17
+ (((t_s-rt_s).abs+2)%(24*60*60)).should be <= 4
18
+ end
19
+
20
+ it "hours" do
21
+ (((subject.hours - Time.new.hour).abs+1)%24).should be <= 2
22
+ end
23
+
24
+ it "minutes" do
25
+ (((subject.minutes - Time.new.min).abs+1)%60).should be <= 2
26
+ end
27
+
28
+ it "seconds" do
29
+ (((subject.seconds - Time.new.sec).abs+2)%60).should be <= 4
30
+ end
31
+
32
+ it "seconds_since_start should be BigDecimal" do
33
+ subject.seconds_since_start.should be_kind_of(BigDecimal)
34
+ end
35
+
36
+ it "hours should be Integer" do
37
+ subject.hours.should be_kind_of(Integer)
38
+ end
39
+
40
+ it "minutes should be Integer" do
41
+ subject.minutes.should be_kind_of(Integer)
42
+ end
43
+
44
+ it "seconds should be Integer" do
45
+ subject.seconds.should be_kind_of(Integer)
46
+ end
47
+
48
+ it "nano_seconds should be Integer" do
49
+ subject.nano_seconds.should be_kind_of(Integer)
50
+ end
51
+
52
+ it "new with argument hours" do
53
+ DayTime.new(10).hours.should == 10
54
+ rt = DayTime.new(20)
55
+ rt.hours.should == 20
56
+ rt.minutes.should == 0
57
+ rt.seconds.should == 0
58
+ rt.nano_seconds.should == 0
59
+ end
60
+
61
+ it "new with argument minutes" do
62
+ rt = DayTime.new(10,25)
63
+ rt.minutes.should == 25
64
+ rt.seconds.should == 0
65
+ rt.nano_seconds.should == 0
66
+ end
67
+
68
+ it "new with argument seconds" do
69
+ rt = DayTime.new(10,37,45)
70
+ rt.seconds.should == 45
71
+ rt.nano_seconds.should == 0
72
+ end
73
+
74
+ it "new with argument nano_seconds" do
75
+ rt = DayTime.new(0,23,45,457834889)
76
+ rt.nano_seconds.should == 457834889
77
+ end
78
+
79
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe DayTime do
4
+
5
+ context "output" do
6
+
7
+ it "does HH:MM:SS for to_s" do
8
+ subject.to_s.should match(/\d{2}:\d{2}:\d{2}/)
9
+ end
10
+
11
+ it "does correct hours, minutes, seconds for to_s" do
12
+ rt = DayTime.new(0,23,45,457834889)
13
+ rt.to_s.should == "00:23:45"
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe DayTime do
4
+
5
+ context "overflow / underflow " do
6
+
7
+ it "normalizes on overflow of hours" do
8
+ rt = DayTime.new(24,1,2,3)
9
+ rt.hours.should == 0
10
+ rt.minutes.should == 1
11
+ rt.seconds.should == 2
12
+ rt.nano_seconds.should == 3
13
+ end
14
+
15
+ it "normalizes on underflow of hours" do
16
+ rt = DayTime.new(-3,1,2,3)
17
+ rt.hours.should == 21
18
+ rt.minutes.should == 1
19
+ rt.seconds.should == 2
20
+ rt.nano_seconds.should == 3
21
+ end
22
+
23
+ it "normalizes on overflow of minutes" do
24
+ rt = DayTime.new(23,65,2,3)
25
+ rt.hours.should == 0
26
+ rt.minutes.should == 5
27
+ rt.seconds.should == 2
28
+ rt.nano_seconds.should == 3
29
+ end
30
+
31
+ it "normalizes on underflow of minutes" do
32
+ rt = DayTime.new(1,-100,2,3)
33
+ rt.hours.should == 23
34
+ rt.minutes.should == 20
35
+ rt.seconds.should == 2
36
+ rt.nano_seconds.should == 3
37
+ end
38
+
39
+ it "normalizes on overflow of nano_seconds" do
40
+ rt = DayTime.new(22,55,2,3600000000)
41
+ rt.hours.should == 22
42
+ rt.minutes.should == 55
43
+ rt.seconds.should == 5
44
+ rt.nano_seconds.should == 600000000
45
+ end
46
+
47
+ it "normalizes on underflow of nano_seconds" do
48
+ rt = DayTime.new(22,55,2,-3600000000)
49
+ rt.hours.should == 22
50
+ rt.minutes.should == 54
51
+ rt.seconds.should == 58
52
+ rt.nano_seconds.should == 400000000
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe DayTimeRange do
4
+
5
+ it "builds new with 1 argument" do
6
+ lambda { DayTimeRange.new("8 until 11") }.should_not raise_error
7
+ end
8
+
9
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relativity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-11 00:00:00.000000000 Z
12
+ date: 2012-01-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &76604630 !ruby/object:Gem::Requirement
16
+ requirement: &68449760 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,18 +21,40 @@ dependencies:
21
21
  version: '2.7'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *76604630
24
+ version_requirements: *68449760
25
25
  - !ruby/object:Gem::Dependency
26
- name: bundler
27
- requirement: &76604360 !ruby/object:Gem::Requirement
26
+ name: guard-rspec
27
+ requirement: &68449150 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
31
31
  - !ruby/object:Gem::Version
32
- version: '1.0'
33
- type: :runtime
32
+ version: '0.5'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *68449150
36
+ - !ruby/object:Gem::Dependency
37
+ name: rb-inotify
38
+ requirement: &68448230 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *68448230
47
+ - !ruby/object:Gem::Dependency
48
+ name: libnotify
49
+ requirement: &68447520 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
34
56
  prerelease: false
35
- version_requirements: *76604360
57
+ version_requirements: *68447520
36
58
  description: time and time_ranges relative to day, week, month, quarter etc.
37
59
  email:
38
60
  - peter@vandenabeele.com
@@ -44,14 +66,20 @@ files:
44
66
  - .rvmrc
45
67
  - Changelog.rdoc
46
68
  - Gemfile
69
+ - Guardfile
47
70
  - MIT-LICENSE
48
71
  - README.rdoc
49
72
  - Rakefile
73
+ - TODO
50
74
  - lib/relativity.rb
51
- - lib/relativity/relative_time.rb
75
+ - lib/relativity/day_time.rb
76
+ - lib/relativity/day_time_range.rb
52
77
  - lib/relativity/version.rb
53
78
  - relativity.gemspec
54
- - spec/relative_time_spec.rb
79
+ - spec/day_time/day_time_spec.rb
80
+ - spec/day_time/output_spec.rb
81
+ - spec/day_time/overflow_spec.rb
82
+ - spec/day_time_range/day_time_range_spec.rb
55
83
  - spec/spec_helper.rb
56
84
  homepage: https://github.com/petervandenabeele/relativity
57
85
  licenses: []
@@ -1,147 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe RelativeTime do
4
-
5
- it "new creates a RelativeTime" do
6
- lambda { subject }.should_not raise_error
7
- end
8
-
9
- context "time in a day" do
10
-
11
- it "seconds_since_midnight" do
12
- lambda { subject.seconds_since_midnight }.should_not raise_error
13
- end
14
-
15
- it "new creates a RelativeTime close to now" do
16
- rt_s = subject.seconds_since_midnight
17
- t = Time.new
18
- t_s = (t.hour*60 + t.min)*60 + t.sec # seconds_since_midnight from Time
19
- (((t_s-rt_s).abs+2)%(24*60*60)).should be <= 4
20
- end
21
-
22
- it "hours" do
23
- (((subject.hours - Time.new.hour).abs+1)%24).should be <= 2
24
- end
25
-
26
- it "minutes" do
27
- (((subject.minutes - Time.new.min).abs+1)%60).should be <= 2
28
- end
29
-
30
- it "seconds" do
31
- (((subject.seconds - Time.new.sec).abs+2)%60).should be <= 4
32
- end
33
-
34
- it "seconds_since_midnight should be BigDecimal" do
35
- subject.seconds_since_midnight.should be_kind_of(BigDecimal)
36
- end
37
-
38
- it "hours should be Integer" do
39
- subject.hours.should be_kind_of(Integer)
40
- end
41
-
42
- it "minutes should be Integer" do
43
- subject.minutes.should be_kind_of(Integer)
44
- end
45
-
46
- it "seconds should be Integer" do
47
- subject.seconds.should be_kind_of(Integer)
48
- end
49
-
50
- it "nano_seconds should be Integer" do
51
- subject.nano_seconds.should be_kind_of(Integer)
52
- end
53
-
54
- it "new with argument hours" do
55
- RelativeTime.new(10).hours.should == 10
56
- rt = RelativeTime.new(20)
57
- rt.hours.should == 20
58
- rt.minutes.should == 0
59
- rt.seconds.should == 0
60
- rt.nano_seconds.should == 0
61
- end
62
-
63
- it "new with argument minutes" do
64
- rt = RelativeTime.new(10,25)
65
- rt.minutes.should == 25
66
- rt.seconds.should == 0
67
- rt.nano_seconds.should == 0
68
- end
69
-
70
- it "new with argument seconds" do
71
- rt = RelativeTime.new(10,37,45)
72
- rt.seconds.should == 45
73
- rt.nano_seconds.should == 0
74
- end
75
-
76
- it "new with argument nano_seconds" do
77
- rt = RelativeTime.new(0,23,45,457834889)
78
- rt.nano_seconds.should == 457834889
79
- end
80
-
81
- end
82
-
83
- context "overflow / underflow " do
84
-
85
- it "normalizes on overflow of hours" do
86
- rt = RelativeTime.new(24,1,2,3)
87
- rt.hours.should == 0
88
- rt.minutes.should == 1
89
- rt.seconds.should == 2
90
- rt.nano_seconds.should == 3
91
- end
92
-
93
- it "normalizes on underflow of hours" do
94
- rt = RelativeTime.new(-3,1,2,3)
95
- rt.hours.should == 21
96
- rt.minutes.should == 1
97
- rt.seconds.should == 2
98
- rt.nano_seconds.should == 3
99
- end
100
-
101
- it "normalizes on overflow of minutes" do
102
- rt = RelativeTime.new(23,65,2,3)
103
- rt.hours.should == 0
104
- rt.minutes.should == 5
105
- rt.seconds.should == 2
106
- rt.nano_seconds.should == 3
107
- end
108
-
109
- it "normalizes on underflow of minutes" do
110
- rt = RelativeTime.new(1,-100,2,3)
111
- rt.hours.should == 23
112
- rt.minutes.should == 20
113
- rt.seconds.should == 2
114
- rt.nano_seconds.should == 3
115
- end
116
-
117
- it "normalizes on overflow of nano_seconds" do
118
- rt = RelativeTime.new(22,55,2,3600000000)
119
- rt.hours.should == 22
120
- rt.minutes.should == 55
121
- rt.seconds.should == 5
122
- rt.nano_seconds.should == 600000000
123
- end
124
-
125
- it "normalizes on underflow of nano_seconds" do
126
- rt = RelativeTime.new(22,55,2,-3600000000)
127
- rt.hours.should == 22
128
- rt.minutes.should == 54
129
- rt.seconds.should == 58
130
- rt.nano_seconds.should == 400000000
131
- end
132
-
133
- end
134
-
135
- context "output" do
136
-
137
- it "does HH:MM:SS for to_s" do
138
- subject.to_s.should match(/\d{2}:\d{2}:\d{2}/)
139
- end
140
-
141
- it "does correct hours, minutes, seconds for to_s" do
142
- rt = RelativeTime.new(0,23,45,457834889)
143
- rt.to_s.should == "00:23:45"
144
- end
145
-
146
- end
147
- end