acts_as_historical 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.1.0
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{acts_as_historical}
8
- s.version = "0.0.4"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["hasclass"]
@@ -11,13 +11,12 @@ module ActsAsHistorical
11
11
  #
12
12
  #
13
13
  # @option opts [Symbol] :date_column (:snapshot_date) the database column for the date of the record
14
- # @option opts [Symbol] :days (:all_days) what days are records valid.
15
14
  # @option opts [Symbol] :scope (nil)
16
15
  #
17
16
  def acts_as_historical(opts = {})
18
17
  configuration = {
19
- # :date_column => "snapshot_date",
20
- :days => :all_days,
18
+ :date_column => "snapshot_date",
19
+ :
21
20
  :scope => nil
22
21
  }
23
22
  configuration.update(opts) if opts.is_a?(Hash)
@@ -25,12 +24,6 @@ module ActsAsHistorical
25
24
  send :include, InstanceMethods
26
25
  send :extend, DynamicClassMethods
27
26
 
28
- case configuration[:days].to_sym
29
- when :all_days
30
- send :extend, AllDays::ClassMethods
31
- when :weekdays
32
- send :extend, WeekDays::ClassMethods
33
- end
34
27
  self.cattr_accessor :historical_date_col, :historical_scope, :only_weekdays
35
28
  self.historical_date_col = 'snapshot_date' #configuration[:date_column]
36
29
  self.historical_scope = configuration[:scope]
@@ -71,35 +64,53 @@ module ActsAsHistorical
71
64
  end
72
65
  }
73
66
 
74
- named_scope :at_date, lambda {|date| {
75
- :conditions => { :snapshot_date => date },
67
+ named_scope :on_date, lambda {|date| {
68
+ :conditions => { :snapshot_date => date.to_date },
76
69
  :limit => 1
77
70
  }}
78
71
 
79
72
  named_scope :between, lambda {|*args|
80
73
  from, to = args
81
74
  range = from.to_date..to.to_date
82
- {
83
- :conditions => {self.historical_date_col => range }
84
- }}
85
-
86
-
75
+ { :conditions => {self.historical_date_col => range } }
76
+ }
77
+
87
78
  # nearest(date, 1)
88
79
  # nearest(date, (date_from..date_to))
89
80
  #
90
81
  named_scope :nearest, lambda {|*args|
91
- date = args.first.to_date
92
- range = self.tolerance_to_range(date, args[1])
93
-
82
+ date, tolerance = args
83
+ range = self.tolerance_to_range(date.to_date, tolerance)
94
84
  {
95
85
  :conditions => {self.historical_date_col => range},
96
- :order => ["ABS(DATEDIFF(#{self.historical_date_col_sql}, '#{date.to_s(:db)}')) ASC"]
97
- }}
86
+ :order => ["ABS(DATEDIFF(#{self.historical_date_col_sql}, '#{date.to_date.to_s(:db)}')) ASC"]
87
+ }
88
+ }
89
+
90
+ # Does not include date
91
+ #
92
+ named_scope :upto, lambda {|date|
93
+ raise "passed parameter does not respond_to? to_date" if date.respond_to?(:to_date)
94
+ { :conditions => ["#{self.historical_date_col_sql} < ?", date.to_date] }
95
+ }
96
+
97
+ # Includes date
98
+ #
99
+ named_scope :upto_including, lambda {|date|
100
+ raise "passed parameter does not respond_to? to_date" if date.respond_to?(:to_date)
101
+ { :conditions => ["#{self.historical_date_col_sql} <= ?", date.to_date] }
102
+ }
103
+
104
+ named_scope :from, lambda {|date|
105
+ raise "passed parameter does not respond_to? to_date" if date.respond_to?(:to_date)
106
+ { :conditions => ["#{self.historical_date_col_sql} > ?", date.to_date] }
107
+ }
108
+
109
+ named_scope :from_including, lambda {|date|
110
+ raise "passed parameter does not respond_to? to_date" if date.respond_to?(:to_date)
111
+ { :conditions => ["#{self.historical_date_col_sql} >= ?", date.to_date] }
112
+ }
98
113
 
99
- # TODO
100
- named_scope :until
101
- named_scope :from
102
-
103
114
  named_scope :opt, lambda {|attributes_for_select| {:select => [:snapshot_date, attributes_for_select].flatten.uniq.join(', ') } }
104
115
 
105
116
  # validations
@@ -126,16 +137,12 @@ module ActsAsHistorical
126
137
 
127
138
  module InstanceMethods
128
139
  def valid_date?
129
- if snapshot_date.nil?
130
- errors.add_to_base('snapshot_date missing')
140
+ if self.to_date.nil?
141
+ errors.add_to_base('date missing')
131
142
  return false
132
143
  end
133
- if self.class.only_weekdays and snapshot_date and snapshot_date.cwday >= 6
134
- errors.add_to_base('snapshot_date is not a weekday')
135
- return false
136
- end
137
- if self.snapshot_date >= Date.tomorrow
138
- errors.add_to_base('snapshot_date is in future')
144
+ if self.to_date >= Date.tomorrow
145
+ errors.add_to_base('date is in future')
139
146
  return false
140
147
  end
141
148
  true
@@ -147,16 +154,16 @@ module ActsAsHistorical
147
154
  def to_date
148
155
  self.send(self.class.historical_date_col)
149
156
  end
150
-
157
+
151
158
  private
152
159
  def find_record_at(date)
153
160
  self.class.at_date(date).same_scope(self).find(:first)
154
- end
155
-
161
+ end
162
+
156
163
  def next_day; self.class.step_date(to_date, 1); end
157
164
  def prev_day; self.class.step_date(to_date, -1); end
158
165
  end
159
-
166
+
160
167
  module AllDays
161
168
  module ClassMethods
162
169
  def step_date(date, step_size)
@@ -164,19 +171,6 @@ module ActsAsHistorical
164
171
  end
165
172
  end
166
173
  end
167
-
168
- module WeekDays
169
- module ClassMethods
170
- def step_date(date, step_size)
171
- date = date + step_size
172
- while date.cwday > 5
173
- date = step_size < 0 ? date - 1 : date + 1
174
- end
175
- date
176
- end
177
- end
178
- end
179
-
180
174
  end
181
175
 
182
176
  ActiveRecord::Base.send :include, ActsAsHistorical
@@ -7,10 +7,6 @@ class ActsAsHistoricalTest < ActiveSupport::TestCase
7
7
  acts_as_historical
8
8
  end
9
9
 
10
- class RecordWeekday < ActiveRecord::Base
11
- acts_as_historical :days => :weekdays
12
- end
13
-
14
10
  def test_schema_has_loaded_correctly
15
11
  assert Record.all
16
12
  end
@@ -71,83 +67,4 @@ class ActsAsHistoricalTest < ActiveSupport::TestCase
71
67
  end
72
68
  end
73
69
  end
74
-
75
- context ":days => :weekdays" do
76
- context "day present" do
77
- setup {
78
- RecordWeekday.delete_all
79
- @old = RecordWeekday.create! :snapshot_date => Date.new(2009,12,4)
80
- @new = RecordWeekday.create! :snapshot_date => Date.new(2009,12,7)
81
- }
82
- context "#previous" do
83
- should "return record of previous weekday" do
84
- assert_equal @old, @new.previous
85
- end
86
- end
87
- context "#next" do
88
- should "return record of next weekday" do
89
- assert_equal @new, @old.next
90
- end
91
- end
92
- end
93
-
94
- context "day missing" do
95
- setup {
96
- RecordWeekday.delete_all
97
- @record = RecordWeekday.create! :snapshot_date => Date.new(2009,12,3)
98
- }
99
- context "#previous" do
100
- should "return nil if record on previous weekday" do
101
- assert_equal nil, @record.previous
102
- end
103
- end
104
-
105
- context "#next" do
106
- should "return nil if record on next weekday" do
107
- assert_equal nil, @record.next
108
- end
109
- end
110
- end
111
- end
112
-
113
- context ":days => :all_days" do
114
- context "day present" do
115
- setup {
116
- Record.delete_all
117
- @old = Record.create! :snapshot_date => Date.new(2009,12,3)
118
- @new = Record.create! :snapshot_date => Date.new(2009,12,4)
119
- }
120
- context "#previous" do
121
- should "return record of previous day" do
122
- assert_equal @old, @new.previous
123
- end
124
- end
125
- context "#next" do
126
- should "return record of next day" do
127
- assert_equal @new, @old.next
128
- end
129
- end
130
- end
131
-
132
- context "day missing" do
133
- setup {
134
- Record.delete_all
135
- @record = Record.create! :snapshot_date => Date.new(2009,12,3)
136
- }
137
- context "#previous" do
138
- should "return nil if record on previous day" do
139
- assert_equal nil, @record.previous
140
- end
141
- end
142
-
143
- context "#next" do
144
- should "return nil if record on next day" do
145
- assert_equal nil, @record.next
146
- end
147
- end
148
- end
149
- end
150
-
151
-
152
-
153
70
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_historical
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - hasclass