relativity 0.0.5 → 0.0.6
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 +5 -8
- data/README.rdoc +3 -0
- data/lib/relativity/day_time/conversions.rb +1 -5
- data/lib/relativity/day_time/new.rb +5 -5
- data/lib/relativity/day_time.rb +1 -1
- data/lib/relativity/day_time_range.rb +12 -4
- data/lib/relativity/exception.rb +10 -1
- data/lib/relativity/version.rb +1 -1
- data/spec/day_time/new_spec.rb +1 -1
- data/spec/day_time_range/new_spec.rb +1 -1
- data/spec/day_time_range/output_spec.rb +30 -0
- data/spec/day_time_range/parsing_spec.rb +27 -3
- data/spec/exception_spec.rb +30 -0
- metadata +7 -5
data/Changelog.rdoc
CHANGED
@@ -1,25 +1,22 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.6 2012-02-07:
|
2
|
+
* DayTimeRange input/output now useable
|
3
|
+
* DayTimeRange exceptions improved
|
2
4
|
|
5
|
+
0.0.5 2012-02-06:
|
3
6
|
* DayTimeRange can now be created
|
4
|
-
|
5
7
|
* DayTime.new("8:30") now takes a string
|
6
8
|
|
7
9
|
0.0.4 2012-01-17:
|
8
|
-
|
9
10
|
* Revert seconds_since_start to seconds_since_midnight
|
10
|
-
|
11
|
-
|
11
|
+
("start" was arbitrary; "midnight" is a real world reference)
|
12
12
|
* Added an Relativity::Exception class
|
13
13
|
|
14
14
|
0.0.3 2012-01-12
|
15
|
-
|
16
15
|
* Change RelativeTime into DayTime
|
17
16
|
* Added an initial DayTimeRange class
|
18
17
|
|
19
18
|
0.0.2 2012-01-12
|
20
|
-
|
21
19
|
* Add the MIT license
|
22
20
|
|
23
21
|
0.0.1 2012-01-11
|
24
|
-
|
25
22
|
* Initial version with minimal TimeRelative functionality
|
data/README.rdoc
CHANGED
@@ -23,10 +23,13 @@ ranges of opening hours can be represented.
|
|
23
23
|
dt.seconds #=> 40
|
24
24
|
dt.nano_seconds #=> 568257238
|
25
25
|
|
26
|
+
DayTimeRange.default_separator # => " until "
|
26
27
|
dtr = DayTimeRange.new("8 until 12:30")
|
27
28
|
# => #<DayTimeRange:0x9bfa4ec @start_day_time=08:00:00, @end_day_time=12:30:00>
|
28
29
|
dtr.start # => 08:00:00
|
29
30
|
dtr.end # => 12:30:00
|
31
|
+
dtr.to_s # => "08:00:00 until 12:30:00"
|
30
32
|
|
31
33
|
night_shift = DayTimeRange.new("21:45..06:05", :separator => '..')
|
32
34
|
# => #<DayTimeRange:0x9b997dc @start_day_time=21:45:00, @end_day_time=06:05:00>
|
35
|
+
night_shift.to_s # => "21:45..06:05"
|
@@ -19,11 +19,7 @@ module DayTime::Conversions
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def to_s
|
22
|
-
[hours, minutes, seconds].map{|e| rjust_2_0(e.to_s)}.join(self.class.
|
23
|
-
end
|
24
|
-
|
25
|
-
def self.separator
|
26
|
-
':'
|
22
|
+
[hours, minutes, seconds].map{|e| rjust_2_0(e.to_s)}.join(self.class.default_separator)
|
27
23
|
end
|
28
24
|
|
29
25
|
private
|
@@ -26,16 +26,16 @@ module DayTime::New
|
|
26
26
|
private
|
27
27
|
|
28
28
|
def hh_mm_ss_from_string(input)
|
29
|
-
separator = self.class.
|
30
|
-
|
29
|
+
separator = self.class.default_separator
|
30
|
+
esc_separator = Regexp.escape(separator) # e.g. separator is '.'
|
31
31
|
input.strip!
|
32
32
|
match_hh = '(?<hh>\d\d?)'
|
33
|
-
match_mm = '(' +
|
34
|
-
match_ss = '(' +
|
33
|
+
match_mm = '(' + esc_separator + '(?<mm>\d\d?))?'
|
34
|
+
match_ss = '(' + esc_separator + '(?<ss>\d\d?))?'
|
35
35
|
matcher = '\A' + match_hh + match_mm + match_ss + '\Z'
|
36
36
|
r = Regexp.new(matcher)
|
37
37
|
matchdata = r.match(input)
|
38
|
-
raise Relativity::
|
38
|
+
raise Relativity::InvalidFormatError if matchdata.nil?
|
39
39
|
return [matchdata[:hh].to_i, matchdata[:mm].to_i, matchdata[:ss].to_i]
|
40
40
|
end
|
41
41
|
|
data/lib/relativity/day_time.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
class DayTimeRange
|
2
2
|
|
3
|
+
attr_accessor :separator
|
4
|
+
|
3
5
|
def initialize(first, second = nil)
|
4
6
|
super()
|
7
|
+
# NOTE don't forget to set the @separator
|
5
8
|
@start_day_time, @end_day_time =
|
6
9
|
case first
|
7
10
|
when String
|
@@ -17,18 +20,23 @@ class DayTimeRange
|
|
17
20
|
@end_day_time
|
18
21
|
end
|
19
22
|
|
20
|
-
def
|
23
|
+
def to_s
|
24
|
+
start.to_s + @separator + self.end.to_s
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.default_separator
|
21
28
|
" until "
|
22
29
|
end
|
23
30
|
|
24
31
|
private
|
25
32
|
|
26
33
|
def start_end_from_string(input, options)
|
27
|
-
separator = (options && options[:separator]) || self.class.
|
28
|
-
|
29
|
-
matcher = '\A(?<start>.+)' +
|
34
|
+
@separator = (options && options[:separator]) || self.class.default_separator
|
35
|
+
esc_separator = Regexp.escape(@separator)
|
36
|
+
matcher = '\A(?<start>.+)' + esc_separator + '(?<end>.+)\Z'
|
30
37
|
r = Regexp.new(matcher)
|
31
38
|
matchdata = r.match(input)
|
39
|
+
raise Relativity::InvalidRangeFormatError.new(:separator => @separator) if matchdata.nil?
|
32
40
|
return [DayTime.new(matchdata[:start]), DayTime.new(matchdata[:end])]
|
33
41
|
end
|
34
42
|
|
data/lib/relativity/exception.rb
CHANGED
@@ -3,7 +3,16 @@ module Relativity
|
|
3
3
|
class InternalError < RuntimeError
|
4
4
|
end
|
5
5
|
|
6
|
-
class
|
6
|
+
class InvalidFormatError < ArgumentError
|
7
|
+
end
|
8
|
+
|
9
|
+
class InvalidRangeFormatError < ArgumentError
|
10
|
+
def initialize(options = {})
|
11
|
+
@options = options
|
12
|
+
end
|
13
|
+
def to_s
|
14
|
+
"Maybe the range separator was not set correctly? Separator used was \"#{@options[:separator]}\""
|
15
|
+
end
|
7
16
|
end
|
8
17
|
|
9
18
|
end
|
data/lib/relativity/version.rb
CHANGED
data/spec/day_time/new_spec.rb
CHANGED
@@ -70,7 +70,7 @@ describe DayTime do
|
|
70
70
|
end
|
71
71
|
|
72
72
|
it 'new with string rejects too much separators' do
|
73
|
-
lambda {
|
73
|
+
lambda {DayTime.new("8:35:30:45")}.should raise_error Relativity::InvalidFormatError
|
74
74
|
end
|
75
75
|
|
76
76
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DayTimeRange do
|
4
|
+
|
5
|
+
context "output" do
|
6
|
+
|
7
|
+
it 'does "HH:MM:SS until HH:MM:SS" for to_s' do
|
8
|
+
dtr = DayTimeRange.new("22 until 06:30")
|
9
|
+
dtr.to_s.should match(/\A\d\d:\d\d:\d\d until \d\d:\d\d:\d\d\Z/)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "does correct hours, minutes, seconds for to_s" do
|
13
|
+
dtr = DayTimeRange.new("22 until 06:30")
|
14
|
+
dtr.to_s.should == "22:00:00 until 06:30:00"
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'setting the separator is used for to_s' do
|
18
|
+
dtr = DayTimeRange.new("22 until 06:30")
|
19
|
+
dtr.separator = '..'
|
20
|
+
dtr.to_s.should match(/\d..\d/)
|
21
|
+
end
|
22
|
+
|
23
|
+
#NOTE this is an integration test (many features in 1 test)
|
24
|
+
it 'uses the separator used for initializing also for to_s' do
|
25
|
+
dtr = DayTimeRange.new(" 22..06:30 ", :separator => '..')
|
26
|
+
dtr.to_s.should == "22:00:00..06:30:00"
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe DayTimeRange do
|
4
4
|
|
5
5
|
it "default separator is ' until '" do
|
6
|
-
DayTimeRange.
|
6
|
+
DayTimeRange.default_separator.should == ' until '
|
7
7
|
end
|
8
8
|
|
9
9
|
it "start time is correct" do
|
@@ -11,14 +11,38 @@ describe DayTimeRange do
|
|
11
11
|
dtr.start.should == DayTime.new(8)
|
12
12
|
end
|
13
13
|
|
14
|
-
it "
|
14
|
+
it "parsing is correct with other separator" do
|
15
15
|
dtr = DayTimeRange.new("8 - 11", :separator => " - ")
|
16
16
|
dtr.start.should == DayTime.new(8)
|
17
17
|
end
|
18
18
|
|
19
|
-
it "
|
19
|
+
it "parsing is correct with separator without spaces" do
|
20
20
|
dtr = DayTimeRange.new("8..11", :separator => "..")
|
21
21
|
dtr.start.should == DayTime.new(8)
|
22
22
|
end
|
23
23
|
|
24
|
+
it "InvalidRangeFormatError is raised with incorrect separator format" do
|
25
|
+
lambda {DayTimeRange.new("8..11")}.should raise_error Relativity::InvalidRangeFormatError
|
26
|
+
end
|
27
|
+
|
28
|
+
it "Exception with separator message is raised with incorrect separator format" do
|
29
|
+
lambda {DayTimeRange.new("8..11")}.should raise_error 'Maybe the range separator was not set correctly? Separator used was " until "'
|
30
|
+
end
|
31
|
+
|
32
|
+
it "parsing with other separator remembers the separator" do
|
33
|
+
dtr = DayTimeRange.new("8 - 11", :separator => " - ")
|
34
|
+
dtr.separator.should == " - "
|
35
|
+
end
|
36
|
+
|
37
|
+
it "sets the separator" do
|
38
|
+
dtr = DayTimeRange.new("8 - 11", :separator => " - ")
|
39
|
+
dtr.separator = ".."
|
40
|
+
dtr.separator.should == ".."
|
41
|
+
end
|
42
|
+
|
43
|
+
it "not setting separator returns default_separator" do
|
44
|
+
dtr = DayTimeRange.new("8 until 11")
|
45
|
+
dtr.separator.should == dtr.class.default_separator
|
46
|
+
end
|
47
|
+
|
24
48
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Relativity
|
4
|
+
|
5
|
+
describe "Exceptions" do
|
6
|
+
|
7
|
+
it "InternalError derived from RuntimeError" do
|
8
|
+
Relativity::InternalError.new.should be_a(RuntimeError)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "InvalidFormatError derived from ArgumentError" do
|
12
|
+
Relativity::InvalidFormatError.new.should be_a(ArgumentError)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "InvalidRangeFormatError derived from ArgumentError" do
|
16
|
+
Relativity::InvalidRangeFormatError.new.should be_a(ArgumentError)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "InvalidRangeFormatError accepts a hash with separator" do
|
20
|
+
lambda {e = Relativity::InvalidRangeFormatError.new(:separator => "abcd")}.
|
21
|
+
should_not raise_error
|
22
|
+
end
|
23
|
+
|
24
|
+
it "InvalidRangeFormatError gives a tailured message" do
|
25
|
+
e = Relativity::InvalidRangeFormatError.new(:separator => "abcd")
|
26
|
+
e.message.should == 'Maybe the range separator was not set correctly? Separator used was "abcd"'
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
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.
|
4
|
+
version: 0.0.6
|
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-02-
|
12
|
+
date: 2012-02-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &80549700 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '2.7'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *80549700
|
25
25
|
description: time and time_ranges relative to day, week, month, quarter etc.
|
26
26
|
email:
|
27
27
|
- peter@vandenabeele.com
|
@@ -56,7 +56,9 @@ files:
|
|
56
56
|
- spec/day_time/output_spec.rb
|
57
57
|
- spec/day_time/overflow_spec.rb
|
58
58
|
- spec/day_time_range/new_spec.rb
|
59
|
+
- spec/day_time_range/output_spec.rb
|
59
60
|
- spec/day_time_range/parsing_spec.rb
|
61
|
+
- spec/exception_spec.rb
|
60
62
|
- spec/spec_helper.rb
|
61
63
|
- spec/week_time/week_time_spec.rb
|
62
64
|
homepage: https://github.com/petervandenabeele/relativity
|
@@ -79,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
81
|
version: '0'
|
80
82
|
requirements: []
|
81
83
|
rubyforge_project:
|
82
|
-
rubygems_version: 1.8.
|
84
|
+
rubygems_version: 1.8.15
|
83
85
|
signing_key:
|
84
86
|
specification_version: 3
|
85
87
|
summary: time and time_ranges relative to day, week, month, quarter etc.
|