chronological 0.0.3 → 0.0.4

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.
@@ -1,3 +1,3 @@
1
1
  module Chronological
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
data/lib/chronological.rb CHANGED
@@ -1,30 +1,35 @@
1
1
  module Chronological
2
2
  module ClassMethods
3
+ # TODO: Needs to be able to add a validation option which can do the
4
+ # typical timeliness validation such as ended_at should be after started_at
5
+ # and that both should validate timeliness
3
6
  def chronological(options = {})
4
- start_field = options[:start_utc] || options[:start]
5
- end_field = options[:end_utc] || options[:end]
6
- time_zone = options[:time_zone]
7
- start_field_is_utc = options.has_key? :start_utc
8
- end_field_is_utc = options.has_key? :end_utc
9
- start_field_utc_suffix = start_field_is_utc ? '_utc' : ''
10
- end_field_utc_suffix = end_field_is_utc ? '_utc' : ''
7
+ start_time_field = options[:start_utc] || options[:start]
8
+ start_date_field = start_time_field.to_s.gsub(/_at/, '_on')
9
+ end_time_field = options[:end_utc] || options[:end]
10
+ end_date_field = end_time_field.to_s.gsub(/_at/, '_on')
11
+ time_zone = options[:time_zone]
12
+ start_time_field_is_utc = options.has_key? :start_utc
13
+ end_time_field_is_utc = options.has_key? :end_utc
14
+ start_time_field_utc_suffix = start_time_field_is_utc ? '_utc' : ''
15
+ end_time_field_utc_suffix = end_time_field_is_utc ? '_utc' : ''
11
16
 
12
- define_method(:started_at_utc_date) do
13
- return nil unless send(start_field).respond_to? :to_date
17
+ define_method(start_date_field) do
18
+ return nil unless send(start_time_field).respond_to? :to_date
14
19
 
15
- send(start_field).to_date
20
+ send(start_time_field).to_date
16
21
  end
17
22
 
18
- define_method(:ended_at_utc_date) do
19
- return nil unless send(end_field).respond_to? :to_date
23
+ define_method(end_date_field) do
24
+ return nil unless send(end_time_field).respond_to? :to_date
20
25
 
21
- send(end_field).to_date
26
+ send(end_time_field).to_date
22
27
  end
23
28
 
24
29
  define_method(:in_progress?) do
25
30
  return false unless scheduled?
26
31
 
27
- (send(start_field) <= Time.now.utc) && send(end_field).future?
32
+ (send(start_time_field) <= Time.now.utc) && send(end_time_field).future?
28
33
  end
29
34
 
30
35
  define_method(:inactive?) do
@@ -34,13 +39,13 @@ module Chronological
34
39
  define_method(:scheduled?) do
35
40
  optional_time_zone = !options[:time_zone].nil? ? send(time_zone) : true
36
41
 
37
- send(start_field).present? && send(end_field).present? && optional_time_zone
42
+ send(start_time_field).present? && send(end_time_field).present? && optional_time_zone
38
43
  end
39
44
 
40
45
  define_method(:partially_scheduled?) do
41
46
  optional_time_zone = !options[:time_zone].nil? ? send(time_zone) : false
42
47
 
43
- send(start_field).present? || send(end_field).present? || optional_time_zone
48
+ send(start_time_field).present? || send(end_time_field).present? || optional_time_zone
44
49
  end
45
50
 
46
51
  define_method(:duration) do
@@ -55,27 +60,27 @@ module Chronological
55
60
  # Scopes
56
61
  #
57
62
  self.class.send(:define_method, :by_date) do
58
- order "#{table_name}.#{start_field} ASC, #{table_name}.#{end_field} ASC"
63
+ order "#{table_name}.#{start_time_field} ASC, #{table_name}.#{end_time_field} ASC"
59
64
  end
60
65
 
61
66
  self.class.send(:define_method, :by_date_reversed) do
62
- order "#{table_name}.#{start_field} DESC, #{table_name}.#{end_field} DESC"
67
+ order "#{table_name}.#{start_time_field} DESC, #{table_name}.#{end_time_field} DESC"
63
68
  end
64
69
 
65
70
  self.class.send(:define_method, :expired) do
66
- where("#{end_field} <= :now", :now => Time.now.utc)
71
+ where("#{end_time_field} <= :now", :now => Time.now.utc)
67
72
  end
68
73
 
69
74
  self.class.send(:define_method, :current) do
70
- where("#{end_field} > :now", :now => Time.now.utc)
75
+ where("#{end_time_field} > :now", :now => Time.now.utc)
71
76
  end
72
77
 
73
78
  self.class.send(:define_method, :in_progress) do
74
- where("#{start_field} <= :now AND #{end_field} > :now", :now => Time.now.utc)
79
+ where("#{start_time_field} <= :now AND #{end_time_field} > :now", :now => Time.now.utc)
75
80
  end
76
81
 
77
82
  self.class.send(:define_method, :started) do
78
- where("#{start_field} <= :now", :now => Time.now.utc)
83
+ where("#{start_time_field} <= :now", :now => Time.now.utc)
79
84
  end
80
85
 
81
86
  self.class.send(:define_method, :in_progress?) do
@@ -92,17 +97,17 @@ module Chronological
92
97
  #
93
98
  # Aliasing date methods to make code more readable
94
99
  class_eval do
95
- alias_attribute :"starts_at#{start_field_utc_suffix}", start_field.to_sym
96
- alias_attribute :"starting_at#{start_field_utc_suffix}", start_field.to_sym
97
- alias_attribute :"ends_at#{start_field_utc_suffix}", end_field.to_sym
98
- alias_attribute :"ending_at#{start_field_utc_suffix}", end_field.to_sym
100
+ alias_attribute :"starts_at#{start_time_field_utc_suffix}", start_time_field.to_sym
101
+ alias_attribute :"starting_at#{start_time_field_utc_suffix}", start_time_field.to_sym
102
+ alias_attribute :"ends_at#{start_time_field_utc_suffix}", end_time_field.to_sym
103
+ alias_attribute :"ending_at#{start_time_field_utc_suffix}", end_time_field.to_sym
99
104
 
100
105
  alias active? in_progress?
101
106
  end
102
107
 
103
108
  private
104
109
  define_method(:duration_in_seconds) do
105
- (send(end_field) - send(start_field))
110
+ (send(end_time_field) - send(start_time_field))
106
111
  end
107
112
  end
108
113
  end
@@ -130,13 +130,13 @@ describe Chronological, :timecop => true do
130
130
  end
131
131
  end
132
132
 
133
- describe '#started_at_utc_date' do
133
+ describe '#started_on_utc' do
134
134
  context 'when the date field is set to a string' do
135
135
  let(:start_time) { '2012-07-26 03:15:12' }
136
136
  let(:end_time) { nil }
137
137
 
138
138
  it 'properly converts the date' do
139
- chronologicable.started_at_utc_date.should eql Time.utc(2012, 7, 26, 3, 15, 12).to_date
139
+ chronologicable.started_on_utc.should eql Time.utc(2012, 7, 26, 3, 15, 12).to_date
140
140
  end
141
141
  end
142
142
 
@@ -145,18 +145,18 @@ describe Chronological, :timecop => true do
145
145
  let(:end_time) { nil }
146
146
 
147
147
  it 'properly converts the date' do
148
- chronologicable.started_at_utc_date.should eql Time.utc(2012, 7, 26, 3, 15, 12).to_date
148
+ chronologicable.started_on_utc.should eql Time.utc(2012, 7, 26, 3, 15, 12).to_date
149
149
  end
150
150
  end
151
151
  end
152
152
 
153
- describe '#ended_at_utc_date' do
153
+ describe '#ended_on_utc' do
154
154
  context 'when the date field is set to a string' do
155
155
  let(:start_time) { nil }
156
156
  let(:end_time) { '2012-07-26 03:15:12' }
157
157
 
158
158
  it 'properly converts the date' do
159
- chronologicable.ended_at_utc_date.should eql Time.utc(2012, 7, 26, 3, 15, 12).to_date
159
+ chronologicable.ended_on_utc.should eql Time.utc(2012, 7, 26, 3, 15, 12).to_date
160
160
  end
161
161
  end
162
162
 
@@ -165,7 +165,7 @@ describe Chronological, :timecop => true do
165
165
  let(:end_time) { Time.utc(2012, 7, 26, 3, 15, 12) }
166
166
 
167
167
  it 'properly converts the date' do
168
- chronologicable.ended_at_utc_date.should eql Time.utc(2012, 7, 26, 3, 15, 12).to_date
168
+ chronologicable.ended_on_utc.should eql Time.utc(2012, 7, 26, 3, 15, 12).to_date
169
169
  end
170
170
  end
171
171
  end
data/spec/spec_helper.rb CHANGED
@@ -6,7 +6,7 @@ require 'chronological'
6
6
  Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |f| require f }
7
7
 
8
8
  RSpec.configure do |config|
9
- config.before(:all) do
9
+ config.before(:suite) do
10
10
  SQLite3::Database.new 'tmp/test.db'
11
11
 
12
12
  ActiveRecord::Base.establish_connection(
@@ -36,7 +36,7 @@ RSpec.configure do |config|
36
36
  ActiveRecord::Base.connection.execute 'DELETE FROM chronologicables'
37
37
  end
38
38
 
39
- config.after(:all) do
39
+ config.after(:suite) do
40
40
  `rm -f ./tmp/test.db`
41
41
  end
42
42
  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.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-10-06 00:00:00.000000000 Z
13
+ date: 2012-10-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec