activerecord_chronological_records 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,5 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.3
4
+ - jruby
5
+ - ree
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "activerecord", ">= 3.0.0"
4
+
5
+ group :development do
6
+ gem "rspec"
7
+ gem "bundler"
8
+ gem "jeweler"
9
+ gem "sqlite3", :platforms => :ruby
10
+ gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
11
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Toms Mikoss
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.
@@ -0,0 +1,65 @@
1
+ # activerecord_chronological_records [![Build Status](https://secure.travis-ci.org/tmikoss/activerecord_chronological_records.png)](http://travis-ci.org/tmikoss/activerecord_chronological_records)
2
+
3
+ Provides a set of helper methods for dealing with chronological records that have common primary key and date columns denoting when the record is active (example: Oracle EBS tables).
4
+
5
+ ## Usage
6
+
7
+ ### Invocation
8
+
9
+ To add the helper methods, invoke `has_chronological_records` method on your ActiveRecord model:
10
+
11
+ class Employee < ActiveRecord::Base
12
+ has_chronological_records
13
+ end
14
+
15
+ This will assume you have `start_date` and `end_date` columns on the model. To change the column names, pass them as arguments:
16
+
17
+ class Employee < ActiveRecord::Base
18
+ has_chronological_records :start_date, :end_date
19
+ end
20
+
21
+ ### Class methods
22
+
23
+ Following scopes are defined at class level that return all records matching specific condition:
24
+
25
+ * `Class.current` - records valid at current date
26
+ * `Class.at(date)` - records valid at given date
27
+
28
+ ### Instance methods
29
+
30
+ Following instance methods are defined for navigation between versions or record. Can return nil if no version matches the condition.
31
+
32
+ * `instance.current` - version of instance valid at current date
33
+ * `instance.at(date)` - version of instance valid at given date
34
+ * `instance.earliest` - first version of record
35
+ * `instance.latest` - last version of record
36
+ * `instance.previous` - version before the one method is being called on
37
+ * `instance.next` - version after the one method is being called on
38
+
39
+ Additonally, following helper methods are available:
40
+
41
+ * `instance.current?` - checks whether instance is valid at current date
42
+
43
+ ## Installation
44
+
45
+ Add the following line to your `Gemfile`
46
+
47
+ gem 'activerecord_chronological_records'
48
+
49
+ and run the `bundle install` command.
50
+
51
+ ## Contributing to activerecord_chronological_records
52
+
53
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
54
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
55
+ * Fork the project.
56
+ * Start a feature/bugfix branch.
57
+ * Commit and push until you are happy with your contribution.
58
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
59
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
60
+
61
+ ## Copyright
62
+
63
+ Copyright (c) 2012 Toms Mikoss. See LICENSE.txt for
64
+ further details.
65
+
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "activerecord_chronological_records"
18
+ gem.homepage = "http://github.com/tmikoss/activerecord_chronological_records"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Provides a set of helper methods for dealing with chronological records.}
21
+ gem.description = %Q{Provides a set of helper methods for dealing with chronological records that have common primary key and date columns denoting when the record is active (example: Oracle EBS tables).}
22
+ gem.email = "toms.mikoss@gmail.com"
23
+ gem.authors = ["Toms Mikoss"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "activerecord_chronological_records #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,66 @@
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 = "activerecord_chronological_records"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Toms Mikoss"]
12
+ s.date = "2012-11-12"
13
+ s.description = "Provides a set of helper methods for dealing with chronological records that have common primary key and date columns denoting when the record is active (example: Oracle EBS tables)."
14
+ s.email = "toms.mikoss@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ ".travis.yml",
23
+ "Gemfile",
24
+ "LICENSE.txt",
25
+ "README.md",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "activerecord_chronological_records.gemspec",
29
+ "lib/activerecord_chronological_records.rb",
30
+ "spec/activerecord_chronological_records_spec.rb",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+ s.homepage = "http://github.com/tmikoss/activerecord_chronological_records"
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = "1.8.24"
37
+ s.summary = "Provides a set of helper methods for dealing with chronological records."
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_runtime_dependency(%q<activerecord>, [">= 3.0.0"])
44
+ s.add_development_dependency(%q<rspec>, [">= 0"])
45
+ s.add_development_dependency(%q<bundler>, [">= 0"])
46
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
47
+ s.add_development_dependency(%q<sqlite3>, [">= 0"])
48
+ s.add_development_dependency(%q<activerecord-jdbcsqlite3-adapter>, [">= 0"])
49
+ else
50
+ s.add_dependency(%q<activerecord>, [">= 3.0.0"])
51
+ s.add_dependency(%q<rspec>, [">= 0"])
52
+ s.add_dependency(%q<bundler>, [">= 0"])
53
+ s.add_dependency(%q<jeweler>, [">= 0"])
54
+ s.add_dependency(%q<sqlite3>, [">= 0"])
55
+ s.add_dependency(%q<activerecord-jdbcsqlite3-adapter>, [">= 0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<activerecord>, [">= 3.0.0"])
59
+ s.add_dependency(%q<rspec>, [">= 0"])
60
+ s.add_dependency(%q<bundler>, [">= 0"])
61
+ s.add_dependency(%q<jeweler>, [">= 0"])
62
+ s.add_dependency(%q<sqlite3>, [">= 0"])
63
+ s.add_dependency(%q<activerecord-jdbcsqlite3-adapter>, [">= 0"])
64
+ end
65
+ end
66
+
@@ -0,0 +1,51 @@
1
+ module ActiverecordChronologicalRecords
2
+ def has_chronological_records(*options)
3
+ if options.empty?
4
+ start_column, end_column = :start_date, :end_date
5
+ else
6
+ start_column, end_column = options[0], options[1]
7
+ end
8
+
9
+ self.instance_eval <<-EOS
10
+ def at(date)
11
+ where("#{start_column} <= :date AND (#{end_column} >= :date OR #{end_column} IS NULL)", :date => date)
12
+ end
13
+
14
+ def current
15
+ at(Time.now)
16
+ end
17
+ EOS
18
+
19
+ self.class_eval <<-EOS
20
+ def at(date)
21
+ #{self}.at(date).where(:#{primary_key} => self.#{primary_key}).first
22
+ end
23
+
24
+ def current
25
+ at(Time.now)
26
+ end
27
+
28
+ def current?
29
+ #{start_column}.to_time <= Time.now && #{end_column}.to_time >= Time.now
30
+ end
31
+
32
+ def earliest
33
+ #{self}.where(:#{primary_key} => self.#{primary_key}).order("#{start_column} ASC").first
34
+ end
35
+
36
+ def latest
37
+ #{self}.where(:#{primary_key} => self.#{primary_key}).order("#{start_column} DESC").first
38
+ end
39
+
40
+ def previous
41
+ at(#{start_column} - 1.day)
42
+ end
43
+
44
+ def next
45
+ at(#{end_column} + 1.day)
46
+ end
47
+ EOS
48
+ end
49
+ end
50
+
51
+ ActiveRecord::Base.extend ActiverecordChronologicalRecords
@@ -0,0 +1,63 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "ActiverecordChronologicalRecords" do
4
+ def make_employee(attributes)
5
+ Employee.new(attributes).tap{ |e| e.id = 1; e.save! }
6
+ end
7
+
8
+ before(:all) do
9
+ Employee.rebuild_table do |t|
10
+ t.date :start_date
11
+ t.date :end_date
12
+ end
13
+
14
+ Employee.has_chronological_records
15
+ end
16
+
17
+ shared_examples "scopes" do
18
+ specify { Employee.current.all.should eq [@current_record] }
19
+ specify { Employee.at(Date.today).all.should eq [@current_record] }
20
+ specify { Employee.at(Date.today - 2.months).all.should eq [@first_record] }
21
+ specify { Employee.at(Date.today + 2.months).all.should eq [@last_record] }
22
+ end
23
+
24
+ shared_examples "navigation methods" do
25
+ specify { @first_record.current.should eq @current_record }
26
+ specify { @first_record.at(Date.today).should eq @current_record }
27
+ specify { @current_record.earliest.should eq @first_record }
28
+ specify { @current_record.latest.should eq @last_record }
29
+ specify { @current_record.previous.should eq @first_record }
30
+ specify { @current_record.next.should eq @last_record }
31
+ end
32
+
33
+ shared_examples "helper methods" do
34
+ specify { @current_record.should be_current }
35
+ specify { @first_record.should_not be_current }
36
+ end
37
+
38
+ context "When start and end dates are present" do
39
+ before(:all) do
40
+ Employee.destroy_all
41
+ @first_record = make_employee(:start_date => Date.today - 1.year, :end_date => Date.today - 1.month - 1.day)
42
+ @current_record = make_employee(:start_date => Date.today - 1.month, :end_date => Date.today + 1.month)
43
+ @last_record = make_employee(:start_date => Date.today + 1.month + 1.day, :end_date => Date.today + 1.year)
44
+ end
45
+
46
+ include_examples "scopes"
47
+ include_examples "navigation methods"
48
+ include_examples "helper methods"
49
+ end
50
+
51
+ context "When last record does not have end date" do
52
+ before(:all) do
53
+ Employee.destroy_all
54
+ @first_record = make_employee(:start_date => Date.today - 1.year, :end_date => Date.today - 1.month - 1.day)
55
+ @current_record = make_employee(:start_date => Date.today - 1.month, :end_date => Date.today + 1.month)
56
+ @last_record = make_employee(:start_date => Date.today + 1.month + 1.day, :end_date => nil)
57
+ end
58
+
59
+ include_examples "scopes"
60
+ include_examples "navigation methods"
61
+ include_examples "helper methods"
62
+ end
63
+ end
@@ -0,0 +1,29 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'active_record'
5
+ require 'activerecord_chronological_records'
6
+
7
+ # Requires supporting files with custom matchers and macros, etc,
8
+ # in ./support/ and its subdirectories.
9
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
10
+
11
+ ActiveRecord::Base.establish_connection(
12
+ :adapter => defined?(JRUBY_VERSION) ? "jdbcsqlite3": "sqlite3",
13
+ :database => "#{File.dirname(__FILE__)}/activerecord_chronological_records.db"
14
+ )
15
+
16
+ class Employee < ActiveRecord::Base
17
+ self.primary_key = :id
18
+
19
+ def self.rebuild_table
20
+ ActiveRecord::Schema.define do
21
+ self.verbose = false
22
+
23
+ create_table :employees, :force => true, :id => false do |t|
24
+ t.integer :id
25
+ yield t
26
+ end
27
+ end
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord_chronological_records
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Toms Mikoss
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: jeweler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: sqlite3
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: activerecord-jdbcsqlite3-adapter
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: ! 'Provides a set of helper methods for dealing with chronological records
111
+ that have common primary key and date columns denoting when the record is active
112
+ (example: Oracle EBS tables).'
113
+ email: toms.mikoss@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files:
117
+ - LICENSE.txt
118
+ - README.md
119
+ files:
120
+ - .document
121
+ - .rspec
122
+ - .travis.yml
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - VERSION
128
+ - activerecord_chronological_records.gemspec
129
+ - lib/activerecord_chronological_records.rb
130
+ - spec/activerecord_chronological_records_spec.rb
131
+ - spec/spec_helper.rb
132
+ homepage: http://github.com/tmikoss/activerecord_chronological_records
133
+ licenses:
134
+ - MIT
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ! '>='
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ segments:
146
+ - 0
147
+ hash: -609177617
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ none: false
150
+ requirements:
151
+ - - ! '>='
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 1.8.24
157
+ signing_key:
158
+ specification_version: 3
159
+ summary: Provides a set of helper methods for dealing with chronological records.
160
+ test_files: []