relativity 0.0.7 → 0.0.8

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.8 2012-02-15:
2
+ * DayTime.normalize
3
+ * DayTimeRange.normalize
4
+ * useful for persisting to a database column
5
+
1
6
  0.0.7 2012-02-15:
2
7
  * Allow arbitrary separator and parse it automatically
3
8
  * The default_separator for DayTimeRange is removed
data/README.rdoc CHANGED
@@ -32,3 +32,33 @@ ranges of opening hours can be represented.
32
32
 
33
33
  wrong_format = DayTimeRange.new("11 to 15", :separator => '..')
34
34
  Relativity::InvalidRangeFormatError: Maybe the range separator was not set correctly? Separator used was ".."
35
+
36
+ # normalize is intended for persisting a DayTime and DayTimeRange.
37
+
38
+ DayTime.normalize("8") # => "08:00:00"
39
+ DayTime.normalize(nil) # => nil
40
+ DayTimeRange.normalize("8..12") # => "08:00:00..12:00:00"
41
+ DayTimeRange.normalize("8 to 12", :separator => "..")
42
+ # => Relativity::InvalidRangeFormatError: Maybe the range separator\
43
+ was not set correctly? Separator used was ".."
44
+ DayTimeRange.normalize(nil) # => nil
45
+
46
+ # usage in ActiveRecord (>= 3.2.x)
47
+
48
+ class Shop < ActiveRecord::Base
49
+
50
+ def opening_hours=(dtr)
51
+ super(
52
+ begin
53
+ DayTimeRange.normalize(dtr, separator => '..')
54
+ rescue Relativity::FormatError
55
+ nil # or set a special error code here
56
+ end)
57
+ end
58
+ end
59
+
60
+ def opening_hours
61
+ dtr = super and DayTimeRange.new(dtr, :separator => '..')
62
+ end
63
+
64
+ end
@@ -2,7 +2,7 @@ module DayTime::New
2
2
 
3
3
  attr_reader :seconds_since_midnight
4
4
 
5
- def initialize(first= nil, minutes = nil, seconds = nil, nano_seconds = nil)
5
+ def initialize(first = (no_args = true; nil), minutes = nil, seconds = nil, nano_seconds = nil)
6
6
  super()
7
7
  case first
8
8
  when Integer
@@ -14,6 +14,7 @@ module DayTime::New
14
14
  hh, mm, ss = hh_mm_ss_from_string(first)
15
15
  nn = 0
16
16
  else
17
+ raise Relativity::InvalidFormatError unless no_args
17
18
  t = Time.new
18
19
  hh = t.hour
19
20
  mm = t.min
@@ -25,4 +25,9 @@ class DayTime
25
25
  match_hh + match_mm + match_ss
26
26
  end
27
27
 
28
+ def self.normalize(*args)
29
+ return nil if args == [nil]
30
+ new(*args).to_s
31
+ end
32
+
28
33
  end
@@ -9,6 +9,8 @@ class DayTimeRange
9
9
  case first
10
10
  when String
11
11
  start_end_from_string(first, second)
12
+ else
13
+ raise Relativity::InvalidRangeFormatError
12
14
  end
13
15
  end
14
16
 
@@ -24,6 +26,11 @@ class DayTimeRange
24
26
  start.to_s + @separator + self.end.to_s
25
27
  end
26
28
 
29
+ def self.normalize(*args)
30
+ return nil if args[0] == nil
31
+ new(*args).to_s
32
+ end
33
+
27
34
  private
28
35
 
29
36
  def start_end_from_string(input, options)
@@ -1,12 +1,15 @@
1
1
  module Relativity
2
2
 
3
- class InternalError < RuntimeError
3
+ class InternalError < ::RuntimeError
4
4
  end
5
5
 
6
- class InvalidFormatError < ArgumentError
6
+ class FormatError < ::ArgumentError
7
7
  end
8
8
 
9
- class InvalidRangeFormatError < ArgumentError
9
+ class InvalidFormatError < FormatError
10
+ end
11
+
12
+ class InvalidRangeFormatError < FormatError
10
13
  def initialize(options = {})
11
14
  @options = options
12
15
  end
@@ -1,3 +1,3 @@
1
1
  module Relativity
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe DayTime, "normalize" do
4
+
5
+ it "normalize with 1 argument" do
6
+ DayTime.normalize("8").should == "08:00:00"
7
+ end
8
+
9
+ it "incorrect entry raises error" do
10
+ lambda {DayTime.normalize("A")}.should raise_error Relativity::InvalidFormatError
11
+ end
12
+
13
+ it "with nil returns nil" do
14
+ DayTime.normalize(nil).should be_nil
15
+ end
16
+
17
+ end
@@ -3,7 +3,11 @@ require 'spec_helper'
3
3
  describe DayTime do
4
4
 
5
5
  it "new builds a DayTime" do
6
- lambda { subject }.should_not raise_error
6
+ subject
7
+ end
8
+
9
+ it "new(nil) fails" do
10
+ lambda {DayTime.new(nil)}.should raise_error(Relativity::InvalidFormatError)
7
11
  end
8
12
 
9
13
  it "new creates a DayTime close to now" do
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe DayTimeRange, "normalize" do
4
+
5
+ it "normalizes with 1 argument" do
6
+ DayTimeRange.normalize("8 until 11").should == "08:00:00 until 11:00:00"
7
+ end
8
+
9
+ it "incorrect entry raises error" do
10
+ lambda {DayTimeRange.normalize("8")}.should raise_error Relativity::InvalidRangeFormatError
11
+ end
12
+
13
+ it "checks separator" do
14
+ lambda {DayTimeRange.normalize("8..10", :separator => "to")}.should raise_error Relativity::InvalidRangeFormatError
15
+ end
16
+
17
+ it "with nil returns nil" do
18
+ DayTimeRange.normalize(nil).should be_nil
19
+ end
20
+
21
+ it "with nil, second_arg returns nil" do
22
+ DayTimeRange.normalize(nil, :second_arg).should be_nil
23
+ end
24
+
25
+ end
@@ -6,6 +6,10 @@ describe DayTimeRange do
6
6
  DayTimeRange.new("8 until 11").should_not be_nil
7
7
  end
8
8
 
9
+ it "new(nil) raises error" do
10
+ lambda {DayTimeRange.new(nil)}.should raise_error Relativity::InvalidRangeFormatError
11
+ end
12
+
9
13
  it "start day_time is correct" do
10
14
  dtr = DayTimeRange.new("8 until 11")
11
15
  dtr.start.should == DayTime.new(8)
@@ -8,12 +8,16 @@ module Relativity
8
8
  Relativity::InternalError.new.should be_a(RuntimeError)
9
9
  end
10
10
 
11
- it "InvalidFormatError derived from ArgumentError" do
12
- Relativity::InvalidFormatError.new.should be_a(ArgumentError)
11
+ it "FormatError derived from ArgumentError" do
12
+ Relativity::FormatError.new.should be_a(ArgumentError)
13
13
  end
14
14
 
15
- it "InvalidRangeFormatError derived from ArgumentError" do
16
- Relativity::InvalidRangeFormatError.new.should be_a(ArgumentError)
15
+ it "InvalidFormatError derived from FormatError" do
16
+ Relativity::InvalidFormatError.new.should be_a(Relativity::FormatError)
17
+ end
18
+
19
+ it "InvalidRangeFormatError derived from FormatError" do
20
+ Relativity::InvalidRangeFormatError.new.should be_a(Relativity::FormatError)
17
21
  end
18
22
 
19
23
  it "InvalidRangeFormatError accepts a hash with separator" do
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.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &80499580 !ruby/object:Gem::Requirement
16
+ requirement: &83122250 !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: *80499580
24
+ version_requirements: *83122250
25
25
  description: time and time_ranges relative to day, week, month, quarter etc.
26
26
  email:
27
27
  - peter@vandenabeele.com
@@ -49,12 +49,14 @@ files:
49
49
  - lib/relativity/version.rb
50
50
  - lib/relativity/week_time.rb
51
51
  - relativity.gemspec
52
+ - spec/day_time/class_methods_spec.rb
52
53
  - spec/day_time/comparison_spec.rb
53
54
  - spec/day_time/conversions_spec.rb
54
55
  - spec/day_time/exception_spec.rb
55
56
  - spec/day_time/new_spec.rb
56
57
  - spec/day_time/output_spec.rb
57
58
  - spec/day_time/overflow_spec.rb
59
+ - spec/day_time_range/class_methods_spec.rb
58
60
  - spec/day_time_range/marshal_spec.rb
59
61
  - spec/day_time_range/new_spec.rb
60
62
  - spec/day_time_range/output_spec.rb
@@ -82,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
84
  version: '0'
83
85
  requirements: []
84
86
  rubyforge_project:
85
- rubygems_version: 1.8.10
87
+ rubygems_version: 1.8.15
86
88
  signing_key:
87
89
  specification_version: 3
88
90
  summary: time and time_ranges relative to day, week, month, quarter etc.