chronological 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module Chronological
2
- VERSION = "0.0.1"
2
+ VERSION = '0.0.2'
3
3
  end
data/lib/chronological.rb CHANGED
@@ -38,38 +38,36 @@ module Chronological
38
38
 
39
39
  def self.included(base)
40
40
  base.extend ClassMethods
41
- end
42
-
43
- ###
44
- # Aliases
45
- #
46
- # Aliasing date methods to make code more readable
47
41
 
48
- alias_attribute :starts_at, :started_at
49
- alias_attribute :starting_at, :started_at
50
- alias_attribute :ends_at, :ended_at
51
- alias_attribute :ending_at, :ended_at
52
- alias_attribute :starts_at_utc, :started_at_utc
53
- alias_attribute :starting_at_utc, :started_at_utc
54
- alias_attribute :ends_at_utc, :ended_at_utc
55
- alias_attribute :ending_at_utc, :ended_at_utc
42
+ ###
43
+ # Aliases
44
+ #
45
+ # Aliasing date methods to make code more readable
46
+
47
+ base.instance_eval <<-EVALING
48
+ alias_attribute :starts_at_utc, :started_at_utc
49
+ alias_attribute :starting_at_utc, :started_at_utc
50
+ alias_attribute :ends_at_utc, :ended_at_utc
51
+ alias_attribute :ending_at_utc, :ended_at_utc
52
+ EVALING
53
+ end
56
54
 
57
- def started_at_date
58
- return nil unless started_at.respond_to? :to_date
55
+ def started_at_utc_date
56
+ return nil unless started_at_utc.respond_to? :to_date
59
57
 
60
- started_at.to_date
58
+ started_at_utc.to_date
61
59
  end
62
60
 
63
- def ended_at_date
64
- return nil unless ended_at.respond_to? :to_date
61
+ def ended_at_utc_date
62
+ return nil unless ended_at_utc.respond_to? :to_date
65
63
 
66
- ended_at.to_date
64
+ ended_at_utc.to_date
67
65
  end
68
66
 
69
67
  def in_progress?
70
68
  return false unless scheduled?
71
69
 
72
- self.started_at.past? && self.ending_at.future?
70
+ (started_at_utc <= Time.now.utc) && ended_at_utc.future?
73
71
  end
74
72
 
75
73
  alias active? in_progress?
@@ -79,7 +77,11 @@ module Chronological
79
77
  end
80
78
 
81
79
  def scheduled?
82
- started_at.present? || ended_at.present? || time_zone.present?
80
+ started_at_utc.present? && ended_at_utc.present?
81
+ end
82
+
83
+ def partially_scheduled?
84
+ started_at_utc.present? || ended_at_utc.present?
83
85
  end
84
86
 
85
87
  def duration
@@ -91,6 +93,6 @@ module Chronological
91
93
 
92
94
  private
93
95
  def duration_in_minutes
94
- @duration_in_minutes ||= (ended_at - started_at) / 60
96
+ @duration_in_minutes ||= (ended_at_utc - started_at_utc) / 60
95
97
  end
96
98
  end
@@ -0,0 +1,206 @@
1
+ require 'spec_helper'
2
+
3
+ class Chronologicable < ActiveRecord::Base
4
+ include Chronological
5
+ end
6
+
7
+ describe Chronological do
8
+ let(:later) { Time.utc(2012, 7, 26, 6, 0, 26) }
9
+ let(:now) { Time.utc(2012, 7, 26, 6, 0, 25) }
10
+ let(:past) { Time.utc(2012, 7, 26, 6, 0, 24) }
11
+
12
+ before { Timecop.freeze(now) }
13
+
14
+ let(:chronologicable) do
15
+ Chronologicable.create(
16
+ :started_at_utc => start_time,
17
+ :ended_at_utc => end_time)
18
+ end
19
+
20
+ it { Chronologicable.new.respond_to?(:starts_at_utc).should be_true }
21
+ it { Chronologicable.new.respond_to?(:starting_at_utc).should be_true }
22
+ it { Chronologicable.new.respond_to?(:ends_at_utc).should be_true }
23
+ it { Chronologicable.new.respond_to?(:ending_at_utc).should be_true }
24
+
25
+ describe '#started_at_utc_date' do
26
+ context 'when the date field is set to a string' do
27
+ let(:start_time) { '2012-07-26 03:15:12' }
28
+ let(:end_time) { nil }
29
+
30
+ it 'properly converts the date' do
31
+ chronologicable.started_at_utc_date.should eql Time.utc(2012, 7, 26, 3, 15, 12).to_date
32
+ end
33
+ end
34
+
35
+ context 'when the date field is set to a date' do
36
+ let(:start_time) { Time.utc(2012, 7, 26, 3, 15, 12) }
37
+ let(:end_time) { nil }
38
+
39
+ it 'properly converts the date' do
40
+ chronologicable.started_at_utc_date.should eql Time.utc(2012, 7, 26, 3, 15, 12).to_date
41
+ end
42
+ end
43
+ end
44
+
45
+ describe '#ended_at_utc_date' do
46
+ context 'when the date field is set to a string' do
47
+ let(:start_time) { nil }
48
+ let(:end_time) { '2012-07-26 03:15:12' }
49
+
50
+ it 'properly converts the date' do
51
+ chronologicable.ended_at_utc_date.should eql Time.utc(2012, 7, 26, 3, 15, 12).to_date
52
+ end
53
+ end
54
+
55
+ context 'when the date field is set to a date' do
56
+ let(:start_time) { nil }
57
+ let(:end_time) { Time.utc(2012, 7, 26, 3, 15, 12) }
58
+
59
+ it 'properly converts the date' do
60
+ chronologicable.ended_at_utc_date.should eql Time.utc(2012, 7, 26, 3, 15, 12).to_date
61
+ end
62
+ end
63
+ end
64
+
65
+ context 'when a start time is set' do
66
+ let(:start_time) { Time.now }
67
+
68
+ context 'but no end time is set' do
69
+ let(:end_time) { nil }
70
+
71
+ describe '#scheduled?' do
72
+ it 'is false' do
73
+ chronologicable.should_not be_scheduled
74
+ end
75
+ end
76
+
77
+ describe '#partially_scheduled?' do
78
+ it 'is true' do
79
+ chronologicable.should be_partially_scheduled
80
+ end
81
+ end
82
+ end
83
+
84
+ context 'and an end time is set' do
85
+ let(:end_time) { Time.now }
86
+
87
+ describe '#scheduled?' do
88
+ it 'is true' do
89
+ chronologicable.should be_scheduled
90
+ end
91
+ end
92
+
93
+ describe '#partially_scheduled?' do
94
+ it 'is true' do
95
+ chronologicable.should be_partially_scheduled
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+ context 'when an end time is set' do
102
+ let(:end_time) { Time.now }
103
+
104
+ context 'but no start time is set' do
105
+ let(:start_time) { nil }
106
+
107
+ describe '#scheduled?' do
108
+ it 'is false' do
109
+ chronologicable.should_not be_scheduled
110
+ end
111
+ end
112
+
113
+ describe '#partially_scheduled?' do
114
+ it 'is true' do
115
+ chronologicable.should be_partially_scheduled
116
+ end
117
+ end
118
+ end
119
+ end
120
+
121
+ context 'when neither a start time nor an end time is set' do
122
+ let(:start_time) { nil }
123
+ let(:end_time) { nil }
124
+
125
+ describe '#scheduled?' do
126
+ it 'is false' do
127
+ chronologicable.should_not be_scheduled
128
+ end
129
+ end
130
+
131
+ describe '#partially_scheduled?' do
132
+ it 'is false' do
133
+ chronologicable.should_not be_partially_scheduled
134
+ end
135
+ end
136
+ end
137
+
138
+ context 'when there is a chronologicable that has already started' do
139
+ let(:start_time) { past }
140
+
141
+ context 'and has already ended' do
142
+ let(:end_time) { past }
143
+
144
+ describe '#in_progress?' do
145
+ it 'is false' do
146
+ chronologicable.should_not be_in_progress
147
+ end
148
+ end
149
+ end
150
+
151
+ context 'and ends now' do
152
+ let(:end_time) { now }
153
+
154
+ describe '#in_progress?' do
155
+ it 'is false' do
156
+ chronologicable.should_not be_in_progress
157
+ end
158
+ end
159
+ end
160
+
161
+ context 'and ends later' do
162
+ let(:end_time) { later }
163
+
164
+ describe '#in_progress?' do
165
+ it 'is true' do
166
+ chronologicable.should be_in_progress
167
+ end
168
+ end
169
+ end
170
+ end
171
+
172
+ context 'when there is a chronologicable that starts now' do
173
+ let(:start_time) { now }
174
+
175
+ context 'and ends now' do
176
+ let(:end_time) { now }
177
+
178
+ describe '#in_progress?' do
179
+ it 'is false' do
180
+ chronologicable.should_not be_in_progress
181
+ end
182
+ end
183
+ end
184
+
185
+ context 'and ends later' do
186
+ let(:end_time) { later }
187
+
188
+ describe '#in_progress?' do
189
+ it 'is true' do
190
+ chronologicable.should be_in_progress
191
+ end
192
+ end
193
+ end
194
+ end
195
+
196
+ context 'when there is a chronologicable that has not yet started' do
197
+ let(:start_time) { later }
198
+ let(:end_time) { later }
199
+
200
+ describe '#in_progress?' do
201
+ it 'is false' do
202
+ chronologicable.should_not be_in_progress
203
+ end
204
+ end
205
+ end
206
+ end
@@ -0,0 +1,32 @@
1
+ require 'sqlite3'
2
+ require 'active_record'
3
+ require 'timecop'
4
+ require 'chronological'
5
+
6
+ Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |f| require f }
7
+
8
+ RSpec.configure do |config|
9
+ config.before(:all) do
10
+ SQLite3::Database.new 'tmp/test.db'
11
+
12
+ ActiveRecord::Base.establish_connection(
13
+ :adapter => 'sqlite3',
14
+ :database => 'tmp/test.db'
15
+ )
16
+
17
+ class SetupTests < ActiveRecord::Migration
18
+ def up
19
+ create_table :chronologicables do |t|
20
+ t.datetime :started_at_utc
21
+ t.datetime :ended_at_utc
22
+ end
23
+ end
24
+ end
25
+
26
+ SetupTests.new.migrate(:up)
27
+ end
28
+
29
+ config.after(:all) do
30
+ `rm -f ./tmp/test.db`
31
+ end
32
+ end
@@ -0,0 +1,7 @@
1
+ RSpec.configure do |config|
2
+ # Configure RSpec to run focused specs, and also respect the alias 'fit' for focused specs
3
+ config.treat_symbols_as_metadata_keys_with_true_values = true
4
+ config.filter_run :focused => true
5
+ config.alias_example_to :fit, :focused => true
6
+ config.run_all_when_everything_filtered = true
7
+ end
@@ -0,0 +1,4 @@
1
+ RSpec.configure do |config|
2
+ # Configure RSpec to respect the alias 'pit' for pending specs
3
+ config.alias_example_to :pit, :pending => true
4
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chronological
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,8 +10,152 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-10-02 00:00:00.000000000 Z
14
- dependencies: []
13
+ date: 2012-10-05 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '2.11'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: '2.11'
31
+ - !ruby/object:Gem::Dependency
32
+ name: fuubar
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: '1.0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '1.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: guard
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.4.0
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: 1.4.0
63
+ - !ruby/object:Gem::Dependency
64
+ name: guard-rspec
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ version: 2.0.0
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ version: 2.0.0
79
+ - !ruby/object:Gem::Dependency
80
+ name: rb-fsevent
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ~>
85
+ - !ruby/object:Gem::Version
86
+ version: 0.9.1
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ~>
93
+ - !ruby/object:Gem::Version
94
+ version: 0.9.1
95
+ - !ruby/object:Gem::Dependency
96
+ name: awesome_print
97
+ requirement: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ~>
101
+ - !ruby/object:Gem::Version
102
+ version: 1.1.0
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: 1.1.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: activerecord
113
+ requirement: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ~>
117
+ - !ruby/object:Gem::Version
118
+ version: 3.1.8
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ~>
125
+ - !ruby/object:Gem::Version
126
+ version: 3.1.8
127
+ - !ruby/object:Gem::Dependency
128
+ name: sqlite3
129
+ requirement: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ~>
133
+ - !ruby/object:Gem::Version
134
+ version: 1.3.6
135
+ type: :development
136
+ prerelease: false
137
+ version_requirements: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ~>
141
+ - !ruby/object:Gem::Version
142
+ version: 1.3.6
143
+ - !ruby/object:Gem::Dependency
144
+ name: timecop
145
+ requirement: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ~>
149
+ - !ruby/object:Gem::Version
150
+ version: 0.5.2
151
+ type: :development
152
+ prerelease: false
153
+ version_requirements: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ~>
157
+ - !ruby/object:Gem::Version
158
+ version: 0.5.2
15
159
  description: ''
16
160
  email: support@chirrpy.com
17
161
  executables: []
@@ -25,6 +169,10 @@ files:
25
169
  - Rakefile
26
170
  - README.md
27
171
  - LICENSE
172
+ - spec/chronological_spec.rb
173
+ - spec/spec_helper.rb
174
+ - spec/support/focused.rb
175
+ - spec/support/pending.rb
28
176
  homepage: https://github.com/chirrpy/chronological
29
177
  licenses: []
30
178
  post_install_message:
@@ -50,4 +198,8 @@ rubygems_version: 1.8.24
50
198
  signing_key:
51
199
  specification_version: 3
52
200
  summary: Easy Accessors for ActiveModel Objects
53
- test_files: []
201
+ test_files:
202
+ - spec/chronological_spec.rb
203
+ - spec/spec_helper.rb
204
+ - spec/support/focused.rb
205
+ - spec/support/pending.rb