timespan 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -30,6 +30,21 @@ Will calculate time diff between two dates, then allow you to get the time diffe
30
30
 
31
31
  See specs for more examples of usage
32
32
 
33
+ ## Comparison
34
+
35
+ ```ruby
36
+ timespan.between?(2.days.ago, 1.minute.from_now)
37
+ timespan.between?(1.days, 3.days)
38
+ timespan < 3.days
39
+ ```
40
+
41
+ ## Math
42
+
43
+ ```ruby
44
+ 3_days_more = timespan + 3.days
45
+ day_less = timespan -1 3.day
46
+ ```
47
+
33
48
  ## Spanner
34
49
 
35
50
  Internally Timespan uses Spanner to parse duration strings.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.3.2
@@ -21,6 +21,20 @@ class Timespan
21
21
  module Compare
22
22
  include Comparable
23
23
 
24
+ def between? cfrom, cto
25
+ case cfrom
26
+ when Date, Time, DateTime
27
+ unless any_kind_of?(cto, Time, Date, DateTime)
28
+ raise ArgumentError, "Arguments must both be Date or Time, was: #{cfrom}, #{cto}"
29
+ end
30
+ (self.start_time.to_i >= cfrom.to_i) && (self.end_time.to_i <= cto.to_i)
31
+ when ::Duration, String, Integer, ActiveSupport::Duration
32
+ self >= cfrom && self <= cto
33
+ else
34
+ raise ArgumentError, "Not valid arguments for between comparison: #{cfrom}, #{cto}"
35
+ end
36
+ end
37
+
24
38
  def time_left time = nil
25
39
  time_compare = time || now
26
40
  diff = end_time - time_compare
@@ -57,6 +71,12 @@ class Timespan
57
71
  self
58
72
  end
59
73
 
74
+ protected
75
+
76
+ def any_kind_of? obj, *types
77
+ types.flatten.compact.any?{|type| obj.kind_of? type }
78
+ end
79
+
60
80
 
61
81
  def valid_compare? time
62
82
  valid_compare_types.any? {|type| time.kind_of? type }
@@ -3,29 +3,71 @@ require 'spec_helper'
3
3
  describe "Timespan" do
4
4
  subject { timespan }
5
5
 
6
- context 'From and To with 1 day apart' do
6
+ context 'From and To with 2 day apart' do
7
7
  let(:timespan) { Timespan.new :from => from, :to => to}
8
8
 
9
- let(:from) { Chronic.parse("1 day ago") }
9
+ let(:from) { 2.days.ago }
10
10
  let(:to) { Time.now }
11
11
 
12
- describe '.compare == ' do
13
- specify do
14
- (subject == 1.day).should be_true
15
- end
16
- end
17
-
18
- describe '.compare < ' do
19
- specify do
20
- (subject < 1.day).should be_false
21
- end
22
- end
23
-
24
- describe '.compare > ' do
25
- specify do
26
- (subject > 1.day).should be_false
27
- end
28
- end
12
+ describe 'Compare' do
13
+ describe '==' do
14
+ specify do
15
+ (subject == 2.days).should be_true
16
+ end
17
+ end
18
+
19
+ describe '<' do
20
+ specify do
21
+ (subject < 2.day).should be_false
22
+ end
23
+
24
+ specify do
25
+ (subject < 3.days).should be_true
26
+ end
27
+ end
28
+
29
+ describe '>' do
30
+ specify do
31
+ (subject > 2.day).should be_false
32
+ end
33
+
34
+ specify do
35
+ (subject > 1.day).should be_true
36
+ end
37
+ end
38
+
39
+ describe '>=' do
40
+ specify do
41
+ (subject >= 2.days).should be_true
42
+ end
43
+ end
44
+
45
+ describe '<=' do
46
+ specify do
47
+ (subject <= 2.days).should be_true
48
+ end
49
+ end
50
+
51
+ describe 'between? dates' do
52
+ specify do
53
+ subject.between?(2.days.ago, 1.minute.from_now).should be_true
54
+ end
55
+
56
+ specify do
57
+ subject.between?(Time.now, 1.day.from_now).should be_false
58
+ end
59
+ end
60
+
61
+ describe 'between? durations' do
62
+ specify do
63
+ subject.between?(1.days, 3.days).should be_true
64
+ end
65
+
66
+ specify do
67
+ subject.between?(3.days, 4.days).should be_false
68
+ end
69
+ end
70
+ end
29
71
  end
30
72
 
31
73
  context 'From 2 days ago until today' do
data/timespan.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "timespan"
8
- s.version = "0.3.1"
8
+ s.version = "0.3.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timespan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -256,7 +256,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
256
256
  version: '0'
257
257
  segments:
258
258
  - 0
259
- hash: -4280308012345220837
259
+ hash: 1296550633399706024
260
260
  required_rubygems_version: !ruby/object:Gem::Requirement
261
261
  none: false
262
262
  requirements: