acts_as_historical 0.0.1

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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ coverage.data
19
+ rdoc
20
+ pkg
21
+
22
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,13 @@
1
+ ActsAsHistoric
2
+ ==============
3
+
4
+ Introduction goes here.
5
+
6
+
7
+ Example
8
+ =======
9
+
10
+ Example goes here.
11
+
12
+
13
+ Copyright (c) 2009 [name of plugin creator], released under the MIT license
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = acts_as_historical
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2009 hasclass. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "acts_as_historical"
8
+ gem.summary = %Q{ActiveRecord plugin for historical data (stock prices, pageviews, etc).}
9
+ gem.description = %Q{}
10
+ gem.email = "sebastian.burkhard@gmail.com"
11
+ gem.homepage = "http://github.com/hasclass/acts_as_historical"
12
+ gem.authors = ["hasclass"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test' << 'rails'
24
+ test.pattern = 'test/**/*_test.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/*_test.rb'
33
+ test.rcov_opts = %w{--rails --exclude osx\/objc,gems\/,spec\/,features\/ --aggregate coverage.data}
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "acts_as_historical #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{acts_as_historical}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["hasclass"]
12
+ s.date = %q{2010-01-11}
13
+ s.description = %q{}
14
+ s.email = %q{sebastian.burkhard@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ ".gitignore",
23
+ "LICENSE",
24
+ "README",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "acts_as_historical.gemspec",
29
+ "lib/acts_as_historical.rb",
30
+ "rails/init.rb",
31
+ "test/acts_as_historical_test.rb",
32
+ "test/database.yml",
33
+ "test/schema.rb",
34
+ "test/test_helper.rb"
35
+ ]
36
+ s.homepage = %q{http://github.com/hasclass/acts_as_historical}
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.5}
40
+ s.summary = %q{ActiveRecord plugin for historical data (stock prices, pageviews, etc).}
41
+ s.test_files = [
42
+ "test/acts_as_historical_test.rb",
43
+ "test/schema.rb",
44
+ "test/test_helper.rb"
45
+ ]
46
+
47
+ if s.respond_to? :specification_version then
48
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
52
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
53
+ else
54
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
58
+ end
59
+ end
60
+
@@ -0,0 +1,184 @@
1
+
2
+ module ActsAsHistorical
3
+
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ # acts_as_historical
11
+ #
12
+ #
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
+ # @option opts [Symbol] :scope (nil)
16
+ #
17
+ def acts_as_historical(opts = {})
18
+ configuration = {
19
+ :date_column => "snapshot_date",
20
+ :days => :all_days,
21
+ :scope => nil
22
+ }
23
+ configuration.update(options) if opts.is_a?(Hash)
24
+
25
+ send :include, InstanceMethods
26
+ send :extend, DynamicClassMethods
27
+
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
+ self.cattr_accessor :historical_date_col, :historical_scope, :only_weekdays
35
+ self.historical_date_col = configuration[:date_column]
36
+ self.historical_scope = configuration[:scope]
37
+
38
+ order_desc = "#{self.historical_date_col_sql} DESC"
39
+ order_asc = "#{self.historical_date_col_sql} ASC"
40
+
41
+ default_scope :order => order_desc
42
+
43
+ # named_scopes - sortings
44
+ named_scope :asc, :order => order_asc
45
+ named_scope :desc, :order => order_desc
46
+
47
+ named_scope :oldest, :limit => 1, :order => order_asc
48
+ named_scope :newest, :limit => 1
49
+ named_scope :newest_two, :limit => 2
50
+
51
+ # one snapshot per week (every wednesday)
52
+ named_scope :weekly,
53
+ :conditions => "DAYOFWEEK(#{self.historical_date_col_sql}) = 2"
54
+
55
+ %w[sundays mondays tuesdays wednesdays thursdays fridays saturdays].each_with_index do |name, day_of_week|
56
+ named_scope name,
57
+ :conditions => "DAYOFWEEK(#{self.historical_date_col_sql}) = #{day_of_week+1}"
58
+ end
59
+
60
+ named_scope :within_month, lambda {{
61
+ :conditions => ["#{self.historical_date_col_sql} > ?", Date.today - 30]
62
+ }}
63
+
64
+ named_scope :within_year, lambda {{
65
+ :conditions => ["#{self.historical_date_col_sql} > ?", Date.today - 364]
66
+ }}
67
+
68
+ named_scope :same_scope, lambda {|record|
69
+ if self.historical_scope.nil?
70
+ {}
71
+ else
72
+ {:conditions => {self.historical_scope => record[self.historical_scope]} }
73
+ end
74
+ }
75
+
76
+ named_scope :at_date, lambda {|date| {
77
+ :conditions => { :snapshot_date => date },
78
+ :limit => 1
79
+ }}
80
+
81
+ named_scope :between, lambda {|*args|
82
+ from, to = args
83
+ range = from.to_date..to.to_date
84
+ {
85
+ :conditions => {self.historical_date_col => range }
86
+ }}
87
+
88
+
89
+ # nearest(date, 1)
90
+ # nearest(date, (date_from..date_to))
91
+ #
92
+ named_scope :nearest, lambda {|*args|
93
+ date = args.first.to_date
94
+ range = self.tolerance_to_range(date, args[1])
95
+
96
+ {
97
+ :conditions => {self.historical_date_col => range},
98
+ :order => ["ABS(DATEDIFF(#{self.historical_date_col_sql}, '#{date.to_s(:db)}')) ASC"]
99
+ }}
100
+
101
+ # TODO
102
+ named_scope :until
103
+ named_scope :from
104
+
105
+ named_scope :opt, lambda {|attributes_for_select| {:select => [:snapshot_date, attributes_for_select].flatten.uniq.join(', ') } }
106
+
107
+ # validations
108
+ validate :valid_date?, :on => :save
109
+ nil
110
+ end
111
+ end
112
+
113
+ module DynamicClassMethods
114
+ def historical_date_col_sql
115
+ "`#{self.table_name}`.`#{self.historical_date_col}`"
116
+ end
117
+
118
+ def tolerance_to_range(date,range)
119
+ if range.is_a?(Numeric)
120
+ range = (date - range)..(date + range)
121
+ elsif range.respond_to?(:to_date_range)
122
+ range = range.to_date_range
123
+ elsif range.is_a?(Range)
124
+ range
125
+ end
126
+ end
127
+ end
128
+
129
+ module InstanceMethods
130
+ def valid_date?
131
+ if snapshot_date.nil?
132
+ errors.add_to_base('snapshot_date missing')
133
+ return false
134
+ end
135
+ if self.class.only_weekdays and snapshot_date and snapshot_date.cwday >= 6
136
+ errors.add_to_base('snapshot_date is not a weekday')
137
+ return false
138
+ end
139
+ if self.snapshot_date >= Date.tomorrow
140
+ errors.add_to_base('snapshot_date is in future')
141
+ return false
142
+ end
143
+ true
144
+ end
145
+
146
+ def previous; find_record_at(prev_day); end
147
+ def next; find_record_at(next_day); end
148
+
149
+ def to_date
150
+ self.send(self.class.historical_date_col)
151
+ end
152
+
153
+ private
154
+ def find_record_at(date)
155
+ self.class.at_date(date).same_scope(self).find(:first)
156
+ end
157
+
158
+ def next_day; self.class.step_date(to_date, 1); end
159
+ def prev_day; self.class.step_date(to_date, -1); end
160
+ end
161
+
162
+ module AllDays
163
+ module ClassMethods
164
+ def step_date(date, step_size)
165
+ date + step_size
166
+ end
167
+ end
168
+ end
169
+
170
+ module WeekDays
171
+ module ClassMethods
172
+ def step_date(date, step_size)
173
+ date = date + step_size
174
+ while date.cwday > 5
175
+ date = step_size < 0 ? date - 1 : date + 1
176
+ end
177
+ date
178
+ end
179
+ end
180
+ end
181
+
182
+ end
183
+
184
+ ActiveRecord::Base.send :include, ActsAsHistorical
data/rails/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ # Include hook code here
2
+ require 'acts_as_historical'
3
+
@@ -0,0 +1,153 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class ActsAsHistoricalTest < ActiveSupport::TestCase
4
+ load_schema
5
+
6
+ class Record < ActiveRecord::Base
7
+ acts_as_historical
8
+ end
9
+
10
+ class RecordWeekday < ActiveRecord::Base
11
+ acts_as_historical :days => :weekdays
12
+ end
13
+
14
+ def test_schema_has_loaded_correctly
15
+ assert Record.all
16
+ end
17
+
18
+ def test_to_date
19
+ date = Date.today
20
+ assert_equal Record.create(:snapshot_date => date).to_date, date
21
+ end
22
+
23
+ context 'named_scopes with 5 weekdays' do
24
+ setup {
25
+ Record.delete_all
26
+ @mon = Date.new(2009,11,30)
27
+ @tue = Date.new(2009,12,1)
28
+ @wed = Date.new(2009,12,2)
29
+ @thu = Date.new(2009,12,3)
30
+ @fri = Date.new(2009,12,4)
31
+
32
+ @r_mon = Record.create! :snapshot_date => @mon
33
+ @r_tue = Record.create! :snapshot_date => @tue
34
+ @r_wed = Record.create! :snapshot_date => @wed
35
+ @r_thu = Record.create! :snapshot_date => @thu
36
+ @r_fri = Record.create! :snapshot_date => @fri
37
+ }
38
+
39
+ should "create 5 records" do
40
+ assert_equal 5, Record.count
41
+ end
42
+
43
+ context "nearest" do
44
+ should "always return monday no matter what tolerance range" do
45
+ assert_equal @r_mon, Record.nearest(@mon-1, 1).first
46
+ assert_equal @r_mon, Record.nearest(@mon, 0).first
47
+ assert_equal @r_mon, Record.nearest(@mon, 1).first
48
+ assert_equal @r_mon, Record.nearest(@mon, 2).first
49
+ end
50
+
51
+ should "return nothing when no records within range" do
52
+ Record.stubs(:only_weekdays).returns(false)
53
+ assert Record.nearest(@mon - 1, 0).empty?
54
+ assert Record.nearest(@fri + 1, 0).empty?
55
+ end
56
+ end
57
+
58
+ context "within" do
59
+ setup { @records = Record.between(@tue, @wed)}
60
+
61
+ should "include only days within range" do
62
+ assert @records.include?(@r_tue)
63
+ assert @records.include?(@r_wed)
64
+ assert !@records.include?(@r_mon)
65
+ assert !@records.include?(@r_thu)
66
+ assert !@records.include?(@r_fri)
67
+ end
68
+
69
+ should "return record when range start and end are the same" do
70
+ assert Record.between(@mon, @mon).include?(@r_mon)
71
+ end
72
+ end
73
+ 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
+ end
data/test/database.yml ADDED
@@ -0,0 +1,6 @@
1
+ mysql:
2
+ :adapter: mysql
3
+ :host: localhost
4
+ :username: root
5
+ :password:
6
+ :database: acts_as_historic_plugin_test
data/test/schema.rb ADDED
@@ -0,0 +1,8 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :records, :force => true do |t|
3
+ t.string :snapshot_date
4
+ end
5
+ create_table :record_weekdays, :force => true do |t|
6
+ t.string :snapshot_date
7
+ end
8
+ end
@@ -0,0 +1,34 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+ require 'active_record/fixtures'
4
+ require 'active_support'
5
+ require 'active_support/test_case'
6
+ require 'test/unit'
7
+ require 'shoulda'
8
+ require 'mocha'
9
+ require 'redgreen'
10
+
11
+ ENV['RAILS_ENV'] = 'test'
12
+ #ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
13
+
14
+ #require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
15
+
16
+ def load_schema
17
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
18
+
19
+ db_adapter = ENV['DB']
20
+
21
+ # no db passed, try one of these fine config-free DBs before bombing.
22
+ db_adapter ||= 'mysql'
23
+
24
+ if db_adapter.nil?
25
+ raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
26
+ end
27
+
28
+ ActiveRecord::Base.establish_connection(config[db_adapter])
29
+ #Fixtures.create_fixtures(File.dirname(__FILE__), ActiveRecord::Base.connection.tables)
30
+
31
+
32
+ load(File.dirname(__FILE__) + "/schema.rb")
33
+ require File.dirname(__FILE__) + '/../rails/init.rb'
34
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_historical
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - hasclass
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-11 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: ""
26
+ email: sebastian.burkhard@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README
34
+ - README.rdoc
35
+ files:
36
+ - .document
37
+ - .gitignore
38
+ - LICENSE
39
+ - README
40
+ - README.rdoc
41
+ - Rakefile
42
+ - VERSION
43
+ - acts_as_historical.gemspec
44
+ - lib/acts_as_historical.rb
45
+ - rails/init.rb
46
+ - test/acts_as_historical_test.rb
47
+ - test/database.yml
48
+ - test/schema.rb
49
+ - test/test_helper.rb
50
+ has_rdoc: true
51
+ homepage: http://github.com/hasclass/acts_as_historical
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options:
56
+ - --charset=UTF-8
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ requirements: []
72
+
73
+ rubyforge_project:
74
+ rubygems_version: 1.3.5
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: ActiveRecord plugin for historical data (stock prices, pageviews, etc).
78
+ test_files:
79
+ - test/acts_as_historical_test.rb
80
+ - test/schema.rb
81
+ - test/test_helper.rb