timespan 0.2.7 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -18,6 +18,10 @@ Will calculate time diff between two dates, then allow you to get the time diffe
18
18
 
19
19
  t = Timespan.new("3 hrs").from(2.days.from_now)
20
20
 
21
+ t = Timespan.new(:from => 2.days.ago)
22
+
23
+ t = Timespan.new(:end_date => 4.days.from_now)
24
+
21
25
  t = Timespan.new(:from => Date.today, :to => "6 weeks from now")
22
26
 
23
27
  t = Timespan.new(:from => Date.today, :duration => "7 weeks 3 days")
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.7
1
+ 0.2.8
data/lib/timespan.rb CHANGED
@@ -25,8 +25,8 @@ class Timespan
25
25
  alias_method :start_date, :start_time
26
26
  alias_method :end_date, :end_time
27
27
 
28
- START_KEYS = [:start, :from]
29
- END_KEYS = [:to, :end]
28
+ START_KEYS = [:start, :from, :start_date]
29
+ END_KEYS = [:to, :end, :end_date]
30
30
  DURATION_KEYS = [:duration, :lasting]
31
31
 
32
32
  ALL_KEYS = START_KEYS + END_KEYS + DURATION_KEYS
@@ -102,16 +102,15 @@ class Timespan
102
102
 
103
103
  # uses init_options to configure
104
104
  def configure! options = {}
105
- from = options[first_from START_KEYS, options]
106
- to = options[first_from END_KEYS, options]
107
- dur = options[first_from DURATION_KEYS, options]
105
+ from = options[first_from(START_KEYS, options)]
106
+ to = options[first_from(END_KEYS, options)]
107
+ dur = options[first_from(DURATION_KEYS, options)]
108
108
 
109
109
  self.duration = dur if dur
110
110
  self.start_time = from if from
111
111
  self.end_time = to if to
112
112
 
113
- default_from_now! unless start_time || end_time
114
-
113
+ default_from_now!
115
114
  calculate_miss!
116
115
  rescue ArgumentError => e
117
116
  raise TimeParseError, e.message
@@ -121,7 +120,8 @@ class Timespan
121
120
  end
122
121
 
123
122
  def default_from_now!
124
- self.start_time = Time.now
123
+ self.start_time = Time.now unless start_time || (end_time && duration)
124
+ self.end_time = Time.now unless end_time || (start_time && duration)
125
125
  end
126
126
 
127
127
  def validate!
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe Timespan do
4
+ subject { timespan }
5
+
6
+ let(:from) { Chronic.parse("1 day ago") }
7
+ let(:to) { Time.now }
8
+
9
+ context 'End date only - use Today as from (start_date)' do
10
+ let(:timespan) { Timespan.new :to => 1.day.from_now}
11
+
12
+ describe '.start_date' do
13
+ it 'should default to today' do
14
+ DateTime.parse(subject.start_date.to_s).strftime('%d %b %Y').should == Date.today.strftime('%d %b %Y')
15
+ end
16
+ end
17
+
18
+ describe '.duration' do
19
+ specify { subject.duration.should_not be_nil }
20
+
21
+ it 'should be 1 day' do
22
+ subject.to_d.should == 1
23
+ end
24
+ end
25
+ end
26
+
27
+ context 'Start date only - use Today as to (end_date)' do
28
+ let(:timespan) { Timespan.new :from => 1.day.ago}
29
+
30
+ describe '.end_date' do
31
+ it 'should default to today' do
32
+ DateTime.parse(subject.end_date.to_s).strftime('%d %b %Y').should == Date.today.strftime('%d %b %Y')
33
+ end
34
+ end
35
+
36
+ describe '.duration' do
37
+ specify { subject.duration.should_not be_nil }
38
+
39
+ it 'should be 1 day' do
40
+ subject.to_d.should == 1
41
+ end
42
+ end
43
+ end
44
+ end
@@ -38,6 +38,7 @@ describe Timespan do
38
38
  end
39
39
  end
40
40
 
41
+
41
42
  context '3 hrs duration from now 2 days from now' do
42
43
  let(:timespan) { Timespan.new("3 hrs").from(2.days.from_now) }
43
44
 
data/timespan.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "timespan"
8
- s.version = "0.2.7"
8
+ s.version = "0.2.8"
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"]
12
- s.date = "2012-05-17"
12
+ s.date = "2012-05-29"
13
13
  s.description = "Makes it easy to calculate time distance in different units"
14
14
  s.email = "kmandrup@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -43,6 +43,7 @@ Gem::Specification.new do |s|
43
43
  "spec/timespan/printer_spec.rb",
44
44
  "spec/timespan/span_spec.rb",
45
45
  "spec/timespan/units_spec.rb",
46
+ "spec/timespan_init_spec.rb",
46
47
  "spec/timespan_spec.rb",
47
48
  "timespan.gemspec"
48
49
  ]
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.2.7
4
+ version: 0.2.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-17 00:00:00.000000000 Z
12
+ date: 2012-05-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: chronic
@@ -237,6 +237,7 @@ files:
237
237
  - spec/timespan/printer_spec.rb
238
238
  - spec/timespan/span_spec.rb
239
239
  - spec/timespan/units_spec.rb
240
+ - spec/timespan_init_spec.rb
240
241
  - spec/timespan_spec.rb
241
242
  - timespan.gemspec
242
243
  homepage: http://github.com/kristianmandrup/timespan
@@ -254,7 +255,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
254
255
  version: '0'
255
256
  segments:
256
257
  - 0
257
- hash: 1077927940562943309
258
+ hash: 43834805566888461
258
259
  required_rubygems_version: !ruby/object:Gem::Requirement
259
260
  none: false
260
261
  requirements: