timespan 0.1.0 → 0.1.2

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/Gemfile CHANGED
@@ -1,5 +1,10 @@
1
1
  source :rubygems
2
2
 
3
+ group :test do
4
+ gem 'chronic'
5
+ gem 'ruby-duration'
6
+ end
7
+
3
8
  group :development do
4
9
  gem "rspec", ">= 2.8.0"
5
10
  gem "rdoc", ">= 3.12"
data/Gemfile.lock CHANGED
@@ -1,14 +1,30 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
+ activemodel (3.2.3)
5
+ activesupport (= 3.2.3)
6
+ builder (~> 3.0.0)
7
+ activesupport (3.2.3)
8
+ i18n (~> 0.6)
9
+ multi_json (~> 1.0)
10
+ bson (1.6.2)
11
+ builder (3.0.0)
12
+ chronic (0.6.7)
4
13
  diff-lcs (1.1.3)
5
14
  git (1.2.5)
15
+ i18n (0.6.0)
6
16
  jeweler (1.8.3)
7
17
  bundler (~> 1.0)
8
18
  git (>= 1.2.5)
9
19
  rake
10
20
  rdoc
11
21
  json (1.6.6)
22
+ mongo (1.6.2)
23
+ bson (~> 1.6.2)
24
+ mongoid (2.4.9)
25
+ activemodel (~> 3.1)
26
+ mongo (~> 1.3)
27
+ tzinfo (~> 0.3.22)
12
28
  multi_json (1.3.2)
13
29
  rake (0.9.2.2)
14
30
  rdoc (3.12)
@@ -21,17 +37,24 @@ GEM
21
37
  rspec-expectations (2.9.1)
22
38
  diff-lcs (~> 1.1.3)
23
39
  rspec-mocks (2.9.0)
40
+ ruby-duration (2.1.3)
41
+ activesupport (>= 3.0.0)
42
+ i18n
43
+ mongoid (~> 2.4.0)
24
44
  simplecov (0.6.2)
25
45
  multi_json (~> 1.3)
26
46
  simplecov-html (~> 0.5.3)
27
47
  simplecov-html (0.5.3)
48
+ tzinfo (0.3.33)
28
49
 
29
50
  PLATFORMS
30
51
  ruby
31
52
 
32
53
  DEPENDENCIES
33
54
  bundler (>= 1.0.0)
55
+ chronic
34
56
  jeweler (>= 1.8.3)
35
57
  rdoc (>= 3.12)
36
58
  rspec (>= 2.8.0)
59
+ ruby-duration
37
60
  simplecov (>= 0.5)
data/README.md CHANGED
@@ -12,6 +12,10 @@ Will calculate time diff in milliseconds between to dates, then allow you to get
12
12
  t.to_hours = 10800
13
13
  ``
14
14
 
15
+ == TODO
16
+
17
+ Should use Duration for duration calculations!
18
+
15
19
  == Contributing to timespan
16
20
 
17
21
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.2
data/lib/timespan.rb CHANGED
@@ -1,47 +1,179 @@
1
+ require 'duration'
2
+
1
3
  class TimeSpan
2
- attr_reader :start_time, :end_time, :millis
4
+ attr_reader :start_time, :end_time, :seconds
5
+
6
+ alias_method :start_date, :start_time
7
+ alias_method :end_date, :end_time
8
+
9
+ def initialize options = {}
10
+ @is_new = true
11
+
12
+ set_with_options options
13
+
14
+ @is_new = false
15
+ end
3
16
 
4
- def initialize start_time, end_time, options = {}
17
+ def start_time= start_time
5
18
  @start_time = start_time
6
- @end_time = end_time
19
+ refresh!
20
+ end
21
+ alias_method :start_date=, :start_time=
7
22
 
8
- @millis = start_time - end_time
23
+ def end_time= start_time
24
+ @start_time = start_time
25
+ refresh!
9
26
  end
27
+ alias_method :end_date=, :end_time=
28
+
29
+ def seconds= seconds
30
+ @seconds = seconds
31
+ refresh!
32
+ end
33
+
34
+ def duration
35
+ @duration ||= Duration.new(seconds)
36
+ end
37
+
38
+ def duration= duration
39
+ raise ArgumentError, "the duration option must be set to a Duration instance" unless duration.kind_of? Duration
40
+ @duration = duration
41
+ refresh! unless is_new?
42
+ end
43
+
44
+ def to_s
45
+ if duration
46
+ "TimeSpan: from #{start_time} lasting #{duration} = #{seconds} secs" if start_time
47
+ "TimeSpan: from #{end_time} to #{duration} before = #{seconds} secs" if end_time
48
+ return
49
+ end
50
+
51
+ if start_time && end_time
52
+ "TimeSpan: #{start_time} to #{end_time} = #{seconds} secs"
53
+ return
54
+ end
55
+
56
+ if seconds
57
+ "TimeSpan: #{seconds} seconds"
58
+ end
59
+ end
60
+
61
+ include Comparable
62
+
63
+ def <=> time
64
+ raise ArgumentError, "Not a valid argument for timespan comparison, was #{time}" unless valid_compare?(time)
65
+ case time
66
+ when TimeSpan
67
+ millis <=> time.seconds
68
+ when Time
69
+ millis <=> time.to_i
70
+ when Date, DateTime
71
+ time.to_time.to_i
72
+ when Integer
73
+ millis <=> time
74
+ end
75
+ end
76
+
77
+ alias_method :to_secs, :seconds
78
+ alias_method :to_seconds, :seconds
10
79
 
11
- def to_seconds
12
- @to_seconds ||= millis * 1000
80
+ def to_milliseconds
81
+ @to_seconds ||= (seconds * 1000.0).round
13
82
  end
14
- alias_method :to_s, :to_seconds
15
- alias_method :to_secs, :to_seconds
83
+ alias_method :to_mils, :to_milliseconds
84
+ alias_method :millis, :to_mils
16
85
 
17
86
  def to_minutes
18
- @to_minutes ||= to_seconds * 60
87
+ @to_minutes ||= (to_seconds / 60.0).round
19
88
  end
20
89
  alias_method :to_m, :to_minutes
21
90
  alias_method :to_mins, :to_minutes
22
91
 
23
92
  def to_hours
24
- @to_hours ||= to_minutes * 60
93
+ @to_hours ||= (to_minutes / 60.0).round
25
94
  end
26
95
  alias_method :to_h, :to_hours
27
96
 
28
97
  def to_days
29
- @to_days ||= to_hours * 24
98
+ @to_days ||= (to_hours / 24).round
30
99
  end
31
100
  alias_method :to_d, :to_days
32
101
 
33
102
  def to_weeks
34
- @to_weeks ||= to_days * 7
103
+ @to_weeks ||= (to_days / 7).round
35
104
  end
36
105
  alias_method :to_w, :to_weeks
37
106
 
38
107
  def to_months
39
- @to_months ||= to_days * 30
108
+ @to_months ||= (to_days / 30).round
40
109
  end
41
110
  alias_method :to_mon, :to_months
42
111
 
43
112
  def to_years
44
- @to_years ||= (to_days.to_f * 365.25).round
113
+ @to_years ||= (to_days.to_f / 365.25).round
45
114
  end
46
115
  alias_method :to_y, :to_years
116
+
117
+ def units
118
+ %w{seconds minutes hours days weeks months years}
119
+ end
120
+
121
+ protected
122
+
123
+ def set_with_options options = {}
124
+ case options
125
+ when Hash
126
+ duration = options[:duration]
127
+ @start_time = options[:from]
128
+ @end_time = options[:to]
129
+ when Integer
130
+ else
131
+ raise ArgumentError, "Timespan must take Hash or Integer, was: #{options}"
132
+ end
133
+
134
+ set_seconds options
135
+ end
136
+
137
+ def set_seconds options = nil
138
+ set_seconds_opts(options)
139
+
140
+ unless @duration
141
+ if @end_time && @start_time
142
+ @seconds ||= (@end_time - @start_time).to_i
143
+ end
144
+ else
145
+ @seconds ||= @duration.total
146
+ end
147
+ end
148
+
149
+ def set_seconds_opts options = {}
150
+ case options
151
+ when Integer
152
+ @seconds = options
153
+ when Hash
154
+ @seconds = options[:seconds] if options[:seconds]
155
+ self.duration = options[:duration] if options[:duration]
156
+ end
157
+ end
158
+
159
+ def is_new?
160
+ @is_new
161
+ end
162
+
163
+ # reset all stored instance vars for units
164
+ def refresh!
165
+ units.each do |unit|
166
+ var_name = :"@#{unit}"
167
+ instance_variable_set var_name, nil
168
+ end
169
+ set_seconds
170
+ end
171
+
172
+ def valid_compare? time
173
+ valid_compare_types.any? {|type| time.kind_of? type }
174
+ end
175
+
176
+ def valid_compare_types
177
+ [TimeSpan, Time, Date, DateTime, Integer]
178
+ end
47
179
  end
@@ -1,7 +1,46 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require 'spec_helper'
2
+ require 'chronic'
2
3
 
3
4
  describe "Timespan" do
4
- it "fails" do
5
- fail "hey buddy, you should probably rename this file and start specing for real"
5
+ subject { timespan }
6
+
7
+ let(:timespan) { TimeSpan.new :from => from, :to => to}
8
+
9
+ let(:from) { Chronic.parse("1 day ago") }
10
+ let(:to) { Time.now }
11
+
12
+ it "spans 1 day" do
13
+ timespan.to_d.should == 1
14
+ end
15
+
16
+ it "spans 86400 sec" do
17
+ timespan.to_secs.should == 86400
18
+ end
19
+
20
+ it "spans 86400 sec" do
21
+ timespan.to_mils.should be_within(10).of(86400000)
6
22
  end
23
+
24
+ describe 'set with duration' do
25
+ let(:duration) { Duration.new(:days => 3) }
26
+ let(:timespan) { TimeSpan.new :from => from, :duration => duration }
27
+
28
+ it 'should be 3 days' do
29
+ timespan.to_d.should == 3
30
+ end
31
+ end
32
+
33
+ describe 'set start_time to new' do
34
+ let(:timespan) { TimeSpan.new :from => from, :to => to }
35
+
36
+ before :each do
37
+ @old_timespan = timespan.clone
38
+ @new_timespan = timespan.clone
39
+ @new_timespan.start_date = Chronic.parse("2 days ago")
40
+ end
41
+
42
+ it 'should have diff timespans' do
43
+ @old_timespan.to_d.should_not == @new_timespan.to_d
44
+ end
45
+ end
7
46
  end
data/timespan.gemspec ADDED
@@ -0,0 +1,63 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "timespan"
8
+ s.version = "0.1.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Kristian Mandrup"]
12
+ s.date = "2012-04-26"
13
+ s.description = "Easy to calculate time distance in different units"
14
+ s.email = "kmandrup@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.md",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/timespan.rb",
29
+ "spec/spec_helper.rb",
30
+ "spec/timespan_spec.rb",
31
+ "timespan.gemspec"
32
+ ]
33
+ s.homepage = "http://github.com/kristianmandrup/timespan"
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = "1.8.22"
37
+ s.summary = "Use TimeSpans in ruby"
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_development_dependency(%q<rspec>, [">= 2.8.0"])
44
+ s.add_development_dependency(%q<rdoc>, [">= 3.12"])
45
+ s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
46
+ s.add_development_dependency(%q<jeweler>, [">= 1.8.3"])
47
+ s.add_development_dependency(%q<simplecov>, [">= 0.5"])
48
+ else
49
+ s.add_dependency(%q<rspec>, [">= 2.8.0"])
50
+ s.add_dependency(%q<rdoc>, [">= 3.12"])
51
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
52
+ s.add_dependency(%q<jeweler>, [">= 1.8.3"])
53
+ s.add_dependency(%q<simplecov>, [">= 0.5"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<rspec>, [">= 2.8.0"])
57
+ s.add_dependency(%q<rdoc>, [">= 3.12"])
58
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
59
+ s.add_dependency(%q<jeweler>, [">= 1.8.3"])
60
+ s.add_dependency(%q<simplecov>, [">= 0.5"])
61
+ end
62
+ end
63
+
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.1.0
4
+ version: 0.1.2
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-04-25 00:00:00.000000000 Z
12
+ date: 2012-04-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -110,6 +110,7 @@ files:
110
110
  - lib/timespan.rb
111
111
  - spec/spec_helper.rb
112
112
  - spec/timespan_spec.rb
113
+ - timespan.gemspec
113
114
  homepage: http://github.com/kristianmandrup/timespan
114
115
  licenses:
115
116
  - MIT
@@ -125,7 +126,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
125
126
  version: '0'
126
127
  segments:
127
128
  - 0
128
- hash: -4606494975038225470
129
+ hash: -3842705630649213426
129
130
  required_rubygems_version: !ruby/object:Gem::Requirement
130
131
  none: false
131
132
  requirements: