cached_at 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +35 -0
- data/Rakefile +11 -0
- data/cached_at.gemspec +26 -0
- data/lib/cached_at/version.rb +3 -0
- data/lib/cached_at.rb +30 -0
- data/test/cached_at_test.rb +97 -0
- data/test/test_helper.rb +23 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4b1253fed9c1d3d09be31eb8c020547d0457f3b7
|
4
|
+
data.tar.gz: 8fbc50fb8c8c3e2baa3268c4c4245319793bfc0c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c858b0d99ce82233306a5342c10053350fbac67364c89a4384711c9827925bd8699917858e1307577469629b51af4bf6afe7717a4bf173db2da0fc2b7c7aac35
|
7
|
+
data.tar.gz: 82cac81a5325d5a27f88e3de960e99b6e6be1f35b7b18df5e191b563517bdeaf3920b360ee6afe9fbd0477a7b79d09b67d64c54e1f84b8d443f80c58a03e6a89
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Delwyn de Villiers
|
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,35 @@
|
|
1
|
+
# CachedAt
|
2
|
+
|
3
|
+
[![Build Status](https://api.travis-ci.org/delwyn/cached_at.png?branch=master)](http://travis-ci.org/delwyn/cached_at)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/delwyn/cached_at.png)](https://codeclimate.com/github/delwyn/cached_at)
|
5
|
+
|
6
|
+
TODO: Write a gem description
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'cached_at'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install cached_at
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
Too add to any class, just include CachedAt. The cache_at column will be update when a record is created or updated.
|
24
|
+
|
25
|
+
class User
|
26
|
+
include CachedAt
|
27
|
+
end
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/cached_at.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cached_at/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "cached_at"
|
8
|
+
spec.version = CachedAt::VERSION
|
9
|
+
spec.authors = ["Delwyn de Villiers"]
|
10
|
+
spec.email = ["delwyn.d@gmail.com"]
|
11
|
+
spec.description = %q{Use cached_at for ActiveRecord cache key}
|
12
|
+
spec.summary = %q{Use cached_at for ActiveRecord cache key instead of updated at}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "activerecord", "~> 3.2"
|
24
|
+
spec.add_development_dependency "minitest", "~> 5"
|
25
|
+
spec.add_development_dependency "sqlite3"
|
26
|
+
end
|
data/lib/cached_at.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require "cached_at/version"
|
2
|
+
|
3
|
+
module CachedAt
|
4
|
+
def self.included(klass)
|
5
|
+
klass.instance_eval do
|
6
|
+
before_save :_set_cached_at
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def cache_key
|
11
|
+
case
|
12
|
+
when new_record?
|
13
|
+
"#{self.class.model_name.cache_key}/new"
|
14
|
+
when timestamp = self[:cached_at]
|
15
|
+
timestamp = timestamp.utc.to_s(:number)
|
16
|
+
"#{self.class.model_name.cache_key}/#{id}-#{timestamp}"
|
17
|
+
else
|
18
|
+
"#{self.class.model_name.cache_key}/#{id}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def touch(name = nil)
|
23
|
+
update_column :cached_at, current_time_from_proper_timezone
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
def _set_cached_at
|
28
|
+
self.cached_at = current_time_from_proper_timezone if new_record? || self.changed?
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
def key_for(object, key)
|
4
|
+
timestamp = object.send(key).utc.to_s(:number)
|
5
|
+
"#{object.class.model_name.cache_key}/#{object.id}-#{timestamp}"
|
6
|
+
end
|
7
|
+
|
8
|
+
describe 'Cached At' do
|
9
|
+
describe 'Cache Key' do
|
10
|
+
context 'new record' do
|
11
|
+
it 'uses new' do
|
12
|
+
user = User.new
|
13
|
+
user.cache_key.must_equal 'users/new'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'existing record' do
|
18
|
+
it 'uses cached_at' do
|
19
|
+
user = User.create
|
20
|
+
user.cached_at = Time.now + 1.minute
|
21
|
+
user.cache_key.wont_equal key_for(user, :updated_at)
|
22
|
+
user.cache_key.must_equal key_for(user, :cached_at)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'cached_at nil' do
|
27
|
+
it 'user' do
|
28
|
+
user = User.create
|
29
|
+
user.cached_at = nil
|
30
|
+
user.cache_key.must_equal "users/#{user.id}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'Set Cached At' do
|
36
|
+
it 'sets cached_at when creating' do
|
37
|
+
user = User.new
|
38
|
+
user.cached_at.must_be_nil
|
39
|
+
user.save
|
40
|
+
user.cached_at.wont_be_nil
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'updates cached_at when updating' do
|
44
|
+
user = User.create
|
45
|
+
old_cached_at = user.cached_at
|
46
|
+
user.update_attribute(:name, 'Bob')
|
47
|
+
user.cached_at.wont_equal old_cached_at
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'does not update the cached_at when nothing has changed' do
|
51
|
+
user = User.create
|
52
|
+
cached_at = user.cached_at
|
53
|
+
user.save
|
54
|
+
user.cached_at.must_equal cached_at
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe 'Associated' do
|
59
|
+
let(:user) { User.create }
|
60
|
+
|
61
|
+
context 'create association' do
|
62
|
+
let(:post) { Post.new user: user }
|
63
|
+
|
64
|
+
before do
|
65
|
+
@old_cached_at = user.cached_at
|
66
|
+
@old_updated_at = user.updated_at
|
67
|
+
post.save
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'updates the cached_at' do
|
71
|
+
user.cached_at.wont_equal @old_cached_at
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'does not update the updated_at' do
|
75
|
+
user.updated_at.must_equal @old_updated_at
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context 'update association' do
|
80
|
+
let(:post) { Post.create user: user }
|
81
|
+
|
82
|
+
before do
|
83
|
+
@old_cached_at = user.cached_at
|
84
|
+
@old_updated_at = user.updated_at
|
85
|
+
post.update_attribute(:title, 'New')
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'updates the cached_at' do
|
89
|
+
user.cached_at.wont_equal @old_cached_at
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'does not update the updated_at' do
|
93
|
+
user.updated_at.must_equal @old_updated_at
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'cached_at'
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require 'active_record'
|
5
|
+
|
6
|
+
class Minitest::Spec
|
7
|
+
class << self
|
8
|
+
alias :context :describe
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
ActiveRecord::Base::establish_connection(adapter: 'sqlite3', database: ':memory:')
|
13
|
+
|
14
|
+
ActiveRecord::Base.connection.execute(%{CREATE TABLE users (id INTEGER PRIMARY KEY, name VARCHAR, updated_at DATETIME, cached_at DATETIME);})
|
15
|
+
ActiveRecord::Base.connection.execute(%{CREATE TABLE posts (id INTEGER PRIMARY KEY, title STRING, user_id INTEGER, updated_at DATETIME);})
|
16
|
+
|
17
|
+
class User < ActiveRecord::Base
|
18
|
+
include CachedAt
|
19
|
+
end
|
20
|
+
|
21
|
+
class Post < ActiveRecord::Base
|
22
|
+
belongs_to :user, touch: true
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cached_at
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Delwyn de Villiers
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activerecord
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Use cached_at for ActiveRecord cache key
|
84
|
+
email:
|
85
|
+
- delwyn.d@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- cached_at.gemspec
|
96
|
+
- lib/cached_at.rb
|
97
|
+
- lib/cached_at/version.rb
|
98
|
+
- test/cached_at_test.rb
|
99
|
+
- test/test_helper.rb
|
100
|
+
homepage: ''
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata: {}
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 2.0.3
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: Use cached_at for ActiveRecord cache key instead of updated at
|
124
|
+
test_files:
|
125
|
+
- test/cached_at_test.rb
|
126
|
+
- test/test_helper.rb
|