hystorical 0.0.1.alpha
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +100 -0
- data/Rakefile +2 -0
- data/hystorical.gemspec +20 -0
- data/lib/hystorical.rb +17 -0
- data/lib/ruby_hystorical.rb +16 -0
- data/spec/hystorical_spec.rb +25 -0
- data/spec/ruby_hystorical_spec.rb +51 -0
- data/spec/spec_helper.rb +2 -0
- metadata +76 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Joel Quenneville
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
# Hystorical
|
2
|
+
|
3
|
+
Hystorical is a simple solution for managing explicit historical datasets. Provided that your records have a `start_date` and `end_date`, Hystorical will take care figuring which records are currently active, which were active during a particular date range and managing the archiving of records. It makes the following assumptions:
|
4
|
+
|
5
|
+
* Objects in the collection have `start_date` and `end_date` attributes
|
6
|
+
* `start_date` and `end_date` return a ruby `Date` object
|
7
|
+
* the start and end dates can be accessed via either `[]` method or the `.` operator
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'hystorical'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install hystorical
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
### current
|
26
|
+
Hystorical returns the current object which is determined as those with `nil` end_date
|
27
|
+
```ruby
|
28
|
+
Hystorical.current(@subscriptions)
|
29
|
+
# => returns enumerable collection of objects
|
30
|
+
```
|
31
|
+
|
32
|
+
### current_on
|
33
|
+
|
34
|
+
Hystorical allows you to find which objects were current at a particular point in time.
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
date = Date.new(2012, 01, 10)
|
38
|
+
Hystorical.current_on(@subscriptions, date)
|
39
|
+
# => returns enumerable collection of objects that were current on January 10th
|
40
|
+
```
|
41
|
+
|
42
|
+
|
43
|
+
## Examples
|
44
|
+
|
45
|
+
### Pure Ruby
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
Subscription = Struct.new(:start_date, :end_date)
|
49
|
+
subscriptions = # collection of several Subscription objects
|
50
|
+
|
51
|
+
Hystorical.current(subscriptions)
|
52
|
+
# => returns enumerable collection of objects
|
53
|
+
|
54
|
+
date = Date.new(2012, 01, 10)
|
55
|
+
Hystorical.current_on(subscriptions, date)
|
56
|
+
# => returns enumerable collection of objects that were current on January 10th
|
57
|
+
```
|
58
|
+
|
59
|
+
### Using Rails
|
60
|
+
|
61
|
+
Hystorical accepts ActiveRecord::Relation collections. This means that all your scopes integrate fully with Hystorical.
|
62
|
+
|
63
|
+
In a model
|
64
|
+
```ruby
|
65
|
+
class Subscription < ActiveRecord::Base
|
66
|
+
attr_accessible :user_id, :start_date, :end_date
|
67
|
+
|
68
|
+
def self.for_user(user_id)
|
69
|
+
where user_id: user_id
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.user_subscription_count_on(user_id, date)
|
73
|
+
subscriptions = Subscription.for_user(user_id)
|
74
|
+
Hystorical.current subscriptions, date
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
In a controller
|
81
|
+
```ruby
|
82
|
+
def index
|
83
|
+
@users_current_subscriptions = Hystorical.current Subscription.for_user(params[:user_id])
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
## Philosophy
|
88
|
+
This gem was created using TDD and README-driven development. The architecture was designed with a strong focus on modularity and extensibility. Using ruby's `Enumerable` methods to return current object was chosen because of it's great flexibility to adapt to all ruby projects. However, when working with large datasets stored in a relational database, using SQL would yield greater performance. Adding an adapter for ActiveRecord or any other ORM (such as DataMapper or Mongoid) is as simple as creating a new class that defines all the methods in the public api and adding a conditional in `Hystorical.delegate_class`.
|
89
|
+
|
90
|
+
## TODO
|
91
|
+
* Add ability to search via SQL when passed `ActiveRecord::Relation`
|
92
|
+
* Add ability to pass in a block option that can further filter results
|
93
|
+
|
94
|
+
## Contributing
|
95
|
+
|
96
|
+
1. Fork it
|
97
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
98
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
99
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
100
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/hystorical.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/hystorical', __FILE__)
|
3
|
+
require File.expand_path('../lib/ruby_hystorical', __FILE__)
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.authors = ["Joel Quenneville"]
|
7
|
+
gem.email = ["joel.quen@gmail.com"]
|
8
|
+
gem.description = %q{Hystorical is a simple solution for managing explicit historical datasets}
|
9
|
+
gem.summary = %q{Hystorical is a simple solution for managing explicit historical datasets}
|
10
|
+
gem.homepage = ""
|
11
|
+
|
12
|
+
gem.files = `git ls-files`.split($\)
|
13
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
14
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
15
|
+
gem.name = "hystorical"
|
16
|
+
gem.require_paths = ["lib"]
|
17
|
+
gem.version = Hystorical::VERSION
|
18
|
+
|
19
|
+
gem.add_development_dependency "rspec"
|
20
|
+
end
|
data/lib/hystorical.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
class Hystorical
|
2
|
+
|
3
|
+
VERSION = "0.0.1.alpha"
|
4
|
+
|
5
|
+
def self.delegate_class
|
6
|
+
RubyHystorical
|
7
|
+
# add conditional to determine which subclass to use once other strategies are used (i.e. ActiveRecord)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.current(collection)
|
11
|
+
delegate_class.current(collection)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.current_on(collection, date)
|
15
|
+
delegate_class.current_on(collection, date)
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class RubyHystorical
|
2
|
+
|
3
|
+
def self.current(collection)
|
4
|
+
collection.select { |obj| obj.respond_to?(:[]) ? obj[:end_date].nil? : obj.end_date.nil? }
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.current_on(collection, date)
|
8
|
+
collection.select do |obj|
|
9
|
+
if obj.respond_to?(:[])
|
10
|
+
obj[:start_date] <= date && obj[:end_date] >= date
|
11
|
+
else
|
12
|
+
obj.start_date <= date && obj.end_date >= date
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
describe Hystorical do
|
2
|
+
|
3
|
+
describe ".delegate_class" do
|
4
|
+
it "should return RubyHystorical" do
|
5
|
+
Hystorical.delegate_class.should be RubyHystorical
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
let(:collection) { stub }
|
10
|
+
before { Hystorical.stub(:delegate_class).and_return(RubyHystorical) }
|
11
|
+
describe ".current" do
|
12
|
+
it "send current message to delegate class" do
|
13
|
+
RubyHystorical.should_receive(:current).with(collection)
|
14
|
+
Hystorical.current(collection)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe ".current_on" do
|
19
|
+
it "send current_on message to delegate class" do
|
20
|
+
date = stub
|
21
|
+
RubyHystorical.should_receive(:current_on).with(collection, date)
|
22
|
+
Hystorical.current_on(collection, date)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
describe RubyHystorical do
|
2
|
+
class HistoricalObjet
|
3
|
+
attr_accessor :start_date, :end_date
|
4
|
+
|
5
|
+
def initialize(start_date, end_date)
|
6
|
+
@start_date = start_date
|
7
|
+
@end_date = end_date
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:obj1) { {start_date: Date.new(2012, 8, 15), end_date: Date.new(2012, 8, 26)} }
|
12
|
+
let(:obj2) { {start_date: Date.new(2012, 8, 27), end_date: Date.new(2012, 9, 4)} }
|
13
|
+
let(:obj3) { {start_date: Date.new(2012, 9, 5), end_date: nil} }
|
14
|
+
let(:collection) { [obj1, obj2, obj3] }
|
15
|
+
|
16
|
+
|
17
|
+
let(:obj4) { HistoricalObjet.new(Date.new(2012, 8, 15), Date.new(2012, 8, 26)) }
|
18
|
+
let(:obj5) { HistoricalObjet.new(Date.new(2012, 8, 27), Date.new(2012, 9, 4)) }
|
19
|
+
let(:obj6) { HistoricalObjet.new(Date.new(2012, 9, 5), nil) }
|
20
|
+
let(:collection2) { [obj4, obj5, obj6] }
|
21
|
+
|
22
|
+
describe ".current" do
|
23
|
+
context "when attributes are accessed via []" do
|
24
|
+
it "should return the current object" do
|
25
|
+
RubyHystorical.current(collection).should eq [obj3]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context "when attributes are accessed via ." do
|
30
|
+
it "should return the current object" do
|
31
|
+
RubyHystorical.current(collection2).should eq [obj6]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe ".current_on" do
|
37
|
+
let(:date) { Date.new(2012, 9, 1) }
|
38
|
+
|
39
|
+
context "when attributes are accessed via []" do
|
40
|
+
it "should return the current object" do
|
41
|
+
RubyHystorical.current_on(collection, date).should eq [obj2]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "when attributes are accessed via ." do
|
46
|
+
it "should return the current object" do
|
47
|
+
RubyHystorical.current_on(collection2, date).should eq [obj5]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hystorical
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.alpha
|
5
|
+
prerelease: 6
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Joel Quenneville
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Hystorical is a simple solution for managing explicit historical datasets
|
31
|
+
email:
|
32
|
+
- joel.quen@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- .rspec
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- hystorical.gemspec
|
44
|
+
- lib/hystorical.rb
|
45
|
+
- lib/ruby_hystorical.rb
|
46
|
+
- spec/hystorical_spec.rb
|
47
|
+
- spec/ruby_hystorical_spec.rb
|
48
|
+
- spec/spec_helper.rb
|
49
|
+
homepage: ''
|
50
|
+
licenses: []
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>'
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: 1.3.1
|
67
|
+
requirements: []
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.8.24
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Hystorical is a simple solution for managing explicit historical datasets
|
73
|
+
test_files:
|
74
|
+
- spec/hystorical_spec.rb
|
75
|
+
- spec/ruby_hystorical_spec.rb
|
76
|
+
- spec/spec_helper.rb
|