timespan 0.4.5 → 0.4.6

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,6 +2,7 @@ source :rubygems
2
2
 
3
3
  gem 'chronic'
4
4
  gem 'chronic_duration'
5
+ gem 'activesupport', '>= 3.0.0'
5
6
  gem 'spanner'
6
7
  gem 'xduration', '~> 2.2'
7
8
 
data/Gemfile.lock CHANGED
@@ -116,6 +116,7 @@ PLATFORMS
116
116
  ruby
117
117
 
118
118
  DEPENDENCIES
119
+ activesupport (>= 3.0.0)
119
120
  bundler (>= 1.0.0)
120
121
  chronic
121
122
  chronic_duration
data/README.md CHANGED
@@ -176,6 +176,46 @@ account.start_date = tomorrow
176
176
  account.end_date = tomorrow + 5.days
177
177
  ```
178
178
 
179
+ ## Factory method
180
+
181
+ Timespan now has the class level factory methods `#from` and `#untill`.
182
+
183
+ ### From
184
+
185
+ ```ruby
186
+ account = Account.create :period => Timespan.from :tomorrow, 7.days
187
+
188
+ # today
189
+ Timespan.from :today, 7.days
190
+
191
+ # now
192
+ Timespan.from :asap, 7.days
193
+ Timespan.from :now, 7.days
194
+
195
+ # now
196
+ Timespan.from :today, 7.days
197
+
198
+ # starting one week from today
199
+ Timespan.from :next_week, 7.days
200
+
201
+ # starting first day next week
202
+ Timespan.from :next_week, 7.days, start: true
203
+
204
+ # starting first day next month
205
+ Timespan.from :next_month, 7.days, start: true
206
+ ```
207
+
208
+ ### Untill
209
+
210
+ Creates timespan from `Time.now` until the time specified.
211
+
212
+ ```ruby
213
+ Timespan.untill :tomorrow
214
+ Timespan.untill :next_week
215
+ Timespan.untill :next_month, start: true
216
+ Timespan.untill 2.days.from_now
217
+ ```
218
+
179
219
  ## Searching periods
180
220
 
181
221
  ```ruby
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.5
1
+ 0.4.6
data/lib/timespan.rb CHANGED
@@ -7,6 +7,7 @@ require 'timespan/units'
7
7
  require 'timespan/compare'
8
8
  require 'timespan/printer'
9
9
  require 'timespan/span'
10
+ require 'active_support/core_ext/date/calculations.rb'
10
11
 
11
12
  if defined?(Mongoid)
12
13
  require 'timespan/mongoid'
@@ -16,6 +17,12 @@ if defined?(Rails) && Rails::VERSION::STRING >= '3.1'
16
17
  require 'duration/rails/engine'
17
18
  end
18
19
 
20
+ class Date
21
+ def self.next_week
22
+ Date.today.next_week
23
+ end
24
+ end
25
+
19
26
  class Timespan
20
27
  include Span
21
28
  include Printer
@@ -53,6 +60,43 @@ class Timespan
53
60
  @is_new = false
54
61
  end
55
62
 
63
+ def self.from start, duration, options = {}
64
+ start = case start.to_sym
65
+ when :now, :asap
66
+ Time.now
67
+ when :today
68
+ Date.today
69
+ when :tomorrow
70
+ Date.today + 1.day
71
+ when :next_week # requires active_support
72
+ date = Date.today.next_week
73
+ options[:start] ? date.beginning_of_week : date
74
+ when :next_month
75
+ date = Date.today.next_month
76
+ options[:start] ? date.at_beginning_of_month.next_month : date
77
+ else
78
+ start
79
+ end
80
+
81
+ self.new start_date: start, duration: duration
82
+ end
83
+
84
+ def self.untill ending
85
+ ending = case ending.to_sym
86
+ when :tomorrow
87
+ Date.today + 1.day
88
+ when :next_week # requires active_support
89
+ date = Date.today.next_week
90
+ options[:start] ? date.beginning_of_week : date
91
+ when :next_month
92
+ date = Date.today.next_month
93
+ options[:start] ? date.at_beginning_of_month.next_month : date
94
+ else
95
+ ending
96
+ end
97
+ self.new start_date: Date.now, end_date: ending
98
+ end
99
+
56
100
  def start_time= time
57
101
  @start_time = convert_to_time time
58
102
  unless is_new?
@@ -188,7 +232,7 @@ class Timespan
188
232
  end
189
233
 
190
234
  def set_end_time
191
- self.end_time = start_time - duration.total
235
+ self.end_time = start_time + duration.total
192
236
  end
193
237
 
194
238
  def set_start_time_miss
@@ -10,6 +10,104 @@ describe TimeSpan do
10
10
  let(:from) { Chronic.parse("1 day ago") }
11
11
  let(:to) { Time.now }
12
12
 
13
+ context 'factory method #from' do
14
+ describe ':today' do
15
+ let(:account) do
16
+ Account.create period: Timespan.from(:today, 5.days)
17
+ end
18
+
19
+ describe '.start_date' do
20
+ it 'should default to today' do
21
+ DateTime.parse(subject.period.start_date.to_s).strftime('%d %b %Y').should == Date.today.strftime('%d %b %Y')
22
+ end
23
+ end
24
+
25
+ describe '.duration' do
26
+ it 'should be 5 days' do
27
+ subject.period.to_days.should == 5
28
+ end
29
+ end
30
+
31
+ describe '.end_date' do
32
+ it 'should be 5 days from today' do
33
+ DateTime.parse(subject.period.end_date.to_s).strftime('%d %b %Y').should == (Date.today + 5.days).strftime('%d %b %Y')
34
+ end
35
+ end
36
+ end
37
+
38
+ describe ':asap' do
39
+ let(:account) do
40
+ Account.create period: Timespan.from(:asap, 5.days)
41
+ end
42
+
43
+ describe '.start_date' do
44
+ it 'should default to today' do
45
+ DateTime.parse(subject.period.start_date.to_s).strftime('%d %b %Y').should == Date.today.strftime('%d %b %Y')
46
+ end
47
+ end
48
+
49
+ describe '.duration' do
50
+ it 'should be 5 days' do
51
+ subject.period.to_days.should == 5
52
+ end
53
+ end
54
+
55
+ describe '.end_date' do
56
+ it 'should be 5 days from today' do
57
+ DateTime.parse(subject.period.end_date.to_s).strftime('%d %b %Y').should == (Date.today + 5.days).strftime('%d %b %Y')
58
+ end
59
+ end
60
+ end
61
+
62
+ describe ':tomorrow' do
63
+ let(:account) do
64
+ Account.create period: Timespan.from(:tomorrow, 5.days)
65
+ end
66
+
67
+ describe '.start_date' do
68
+ it 'should be tomorrow' do
69
+ DateTime.parse(subject.period.start_date.to_s).strftime('%d %b %Y').should == Date.tomorrow.strftime('%d %b %Y')
70
+ end
71
+ end
72
+
73
+ describe '.duration' do
74
+ it 'should be 5 days' do
75
+ subject.period.to_days.should == 5
76
+ end
77
+ end
78
+
79
+ describe '.end_date' do
80
+ it 'should be 5 days from tomorrow' do
81
+ DateTime.parse(subject.period.end_date.to_s).strftime('%d %b %Y').should == (Date.tomorrow + 5.days).strftime('%d %b %Y')
82
+ end
83
+ end
84
+ end
85
+
86
+ describe ':next_week' do
87
+ let(:account) do
88
+ Account.create period: Timespan.from(:next_week, 5.days)
89
+ end
90
+
91
+ describe '.start_date' do
92
+ it 'should be 1 week from today' do
93
+ DateTime.parse(subject.period.start_date.to_s).strftime('%d %b %Y').should == Date.next_week.strftime('%d %b %Y')
94
+ end
95
+ end
96
+
97
+ describe '.duration' do
98
+ it 'should be 5 days' do
99
+ subject.period.to_days.should == 5
100
+ end
101
+ end
102
+
103
+ describe '.end_date' do
104
+ it 'should be 5 days from next week' do
105
+ DateTime.parse(subject.period.end_date.to_s).strftime('%d %b %Y').should == (Date.next_week + 5.days).strftime('%d %b %Y')
106
+ end
107
+ end
108
+ end
109
+ end
110
+
13
111
  context '2 days duration using factory method' do
14
112
  let(:account) do
15
113
  Account.create_it! '2 days'
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.4.5"
8
+ s.version = "0.4.6"
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-09-19"
12
+ s.date = "2012-09-24"
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 = [
@@ -68,6 +68,7 @@ Gem::Specification.new do |s|
68
68
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
69
69
  s.add_runtime_dependency(%q<chronic>, [">= 0"])
70
70
  s.add_runtime_dependency(%q<chronic_duration>, [">= 0"])
71
+ s.add_runtime_dependency(%q<activesupport>, [">= 3.0.0"])
71
72
  s.add_runtime_dependency(%q<spanner>, [">= 0"])
72
73
  s.add_runtime_dependency(%q<xduration>, ["~> 2.2"])
73
74
  s.add_development_dependency(%q<rspec>, [">= 2.8.0"])
@@ -80,6 +81,7 @@ Gem::Specification.new do |s|
80
81
  else
81
82
  s.add_dependency(%q<chronic>, [">= 0"])
82
83
  s.add_dependency(%q<chronic_duration>, [">= 0"])
84
+ s.add_dependency(%q<activesupport>, [">= 3.0.0"])
83
85
  s.add_dependency(%q<spanner>, [">= 0"])
84
86
  s.add_dependency(%q<xduration>, ["~> 2.2"])
85
87
  s.add_dependency(%q<rspec>, [">= 2.8.0"])
@@ -93,6 +95,7 @@ Gem::Specification.new do |s|
93
95
  else
94
96
  s.add_dependency(%q<chronic>, [">= 0"])
95
97
  s.add_dependency(%q<chronic_duration>, [">= 0"])
98
+ s.add_dependency(%q<activesupport>, [">= 3.0.0"])
96
99
  s.add_dependency(%q<spanner>, [">= 0"])
97
100
  s.add_dependency(%q<xduration>, ["~> 2.2"])
98
101
  s.add_dependency(%q<rspec>, [">= 2.8.0"])
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.4.5
4
+ version: 0.4.6
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-09-19 00:00:00.000000000 Z
12
+ date: 2012-09-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: chronic
@@ -43,6 +43,22 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: activesupport
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 3.0.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 3.0.0
46
62
  - !ruby/object:Gem::Dependency
47
63
  name: spanner
48
64
  requirement: !ruby/object:Gem::Requirement
@@ -248,7 +264,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
248
264
  version: '0'
249
265
  segments:
250
266
  - 0
251
- hash: 4117334554078744124
267
+ hash: -1422999725425630205
252
268
  required_rubygems_version: !ruby/object:Gem::Requirement
253
269
  none: false
254
270
  requirements: