class_cacher 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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ ## v0.0.1
2
+ * initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in class_cacher.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Nicolai Seerup
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,45 @@
1
+ # ClassCacher
2
+
3
+ Generates a unique cache key for Rails classes
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'class_cacher'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install class_cacher
18
+
19
+ ## Usage
20
+ ```ruby
21
+ class User < ActiveRecord::Base
22
+ include ClassCacher
23
+ end
24
+ ```
25
+ then use it like this
26
+
27
+ ```ruby
28
+ User.unique_class_cache_key
29
+ ```
30
+
31
+ or even better
32
+
33
+ ```ruby
34
+ User.cached_unique_class_cache_key
35
+ ```
36
+
37
+ Shuold be used together with memcached and the dalli gem (or similar)
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -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 'class_cacher/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "class_cacher"
8
+ spec.version = ClassCacher::VERSION
9
+ spec.authors = ["Nicolai Seerup"]
10
+ spec.email = ["mail@soundtracktor.com"]
11
+ spec.description = %q{Generates a unique cache key Rails classes}
12
+ spec.summary = %q{What is a gem summary}
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 "rspec"
24
+ spec.add_development_dependency "sqlite3-ruby"
25
+ spec.add_dependency "activerecord", ">= 3.2"
26
+ end
@@ -0,0 +1,25 @@
1
+ module ClassCacher
2
+ module ModelAdditions
3
+ def unique_class_cache
4
+ after_commit :flush_unique_class_cache_key
5
+
6
+ private
7
+ def flush_unique_class_cache_key
8
+ Rails.cache.delete(["cached_unique_class_cache_key", self.class.name])
9
+ end
10
+
11
+ def self.unique_class_cache_key
12
+ max_updated_at = self.pluck("MAX(updated_at)").first.to_s
13
+ parsed_max_updated_at = "#{ max_updated_at.present? ? DateTime.parse(max_updated_at).utc.to_s(:number) : nil }"
14
+ count = self.count
15
+
16
+
17
+ Digest::SHA1.hexdigest "#{ [parsed_max_updated_at, self.name, count] }"
18
+ end
19
+
20
+ def self.cached_unique_class_cache_key
21
+ Rails.cache.fetch(["cached_unique_class_cache_key", self.name]) { self.unique_class_cache_key }
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ module ClassCacher
2
+ class Railtie < Rails::Railtie
3
+ initializer 'class_cacher.model_additions' do
4
+ ActiveSupport.on_load :active_record do
5
+ extend ModelAdditions
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module ClassCacher
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ require "class_cacher/version"
2
+ require "class_cacher/model_additions"
3
+ require "class_cacher/railtie" if defined? Rails
4
+
5
+ module ClassCacher
6
+
7
+ end
@@ -0,0 +1 @@
1
+ require 'active_record'
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe ClassCacher::ModelAdditions do
4
+ it 'generates a unique class cache key' do
5
+ User.unique_class_cache_key.should_not be_nil
6
+ User.unique_class_cache_key.should be_an_instance_of(String)
7
+ end
8
+
9
+ it 'generates a new key on save' do
10
+ User.create!(name: "John")
11
+ first_key = User.unique_class_cache_key
12
+ User.create!(name: "Johnny")
13
+ User.unique_class_cache_key.should_not eq(first_key)
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ require 'class_cacher'
2
+ require 'class_cacher_active_record'
3
+
4
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => File.dirname(__FILE__) + "/class_cacher_active_record.sqlite3")
5
+ load File.dirname(__FILE__) + '/support/schema.rb'
6
+ load File.dirname(__FILE__) + '/support/models.rb'
7
+ load File.dirname(__FILE__) + '/support/data.rb'
@@ -0,0 +1 @@
1
+ User.create(:name => "John Doe")
@@ -0,0 +1,4 @@
1
+ class User < ActiveRecord::Base
2
+ extend ClassCacher::ModelAdditions
3
+ unique_class_cache
4
+ end
@@ -0,0 +1,8 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :users, :force => true do |t|
5
+ t.string :name
6
+ t.timestamps
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: class_cacher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nicolai Seerup
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
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: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
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: rspec
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: sqlite3-ruby
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: activerecord
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '3.2'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '3.2'
94
+ description: Generates a unique cache key Rails classes
95
+ email:
96
+ - mail@soundtracktor.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - .rspec
103
+ - CHANGELOG.md
104
+ - Gemfile
105
+ - LICENSE.txt
106
+ - README.md
107
+ - Rakefile
108
+ - class_cacher.gemspec
109
+ - lib/class_cacher.rb
110
+ - lib/class_cacher/model_additions.rb
111
+ - lib/class_cacher/railtie.rb
112
+ - lib/class_cacher/version.rb
113
+ - lib/class_cacher_active_record.rb
114
+ - spec/class_cacher/model_additions_spec.rb
115
+ - spec/class_cacher_active_record.sqlite3
116
+ - spec/spec_helper.rb
117
+ - spec/support/data.rb
118
+ - spec/support/models.rb
119
+ - spec/support/schema.rb
120
+ homepage: ''
121
+ licenses:
122
+ - MIT
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ! '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ segments:
134
+ - 0
135
+ hash: 3879508204601036332
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ segments:
143
+ - 0
144
+ hash: 3879508204601036332
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 1.8.24
148
+ signing_key:
149
+ specification_version: 3
150
+ summary: What is a gem summary
151
+ test_files:
152
+ - spec/class_cacher/model_additions_spec.rb
153
+ - spec/class_cacher_active_record.sqlite3
154
+ - spec/spec_helper.rb
155
+ - spec/support/data.rb
156
+ - spec/support/models.rb
157
+ - spec/support/schema.rb