Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
John Duff committed Mar 1, 2013
0 parents commit 6da8b66
Show file tree
Hide file tree
Showing 25 changed files with 1,883 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in identity_cache.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
Copyright (c) 2013 Shopify

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 changes: 29 additions & 0 deletions README.md
@@ -0,0 +1,29 @@
# IdentityCache

Opt in read through ActiveRecord caching.

## Installation

Add this line to your application's Gemfile:

gem 'identity_cache'

And then execute:

$ bundle

Or install it yourself as:

$ gem install identity_cache

## Usage

TODO: Write usage instructions here

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Added some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
16 changes: 16 additions & 0 deletions Rakefile
@@ -0,0 +1,16 @@
#!/usr/bin/env rake
require 'bundler/gem_tasks'

require 'rake/testtask'
require 'rdoc/task'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the identity_cache plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end
24 changes: 24 additions & 0 deletions identity_cache.gemspec
@@ -0,0 +1,24 @@
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/identity_cache/version', __FILE__)

Gem::Specification.new do |gem|
gem.authors = ["Camilo Lopez", "Tom Burns", "John Duff"]
gem.email = ["john.duff@jadedpixel.com"]
gem.description = %q{Opt in read through ActiveRecord caching}
gem.summary = %q{Caches ActiveRecord models in memcache}
gem.homepage = ""

gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "identity_cache"
gem.require_paths = ["lib"]
gem.version = IdentityCache::VERSION


gem.add_dependency('ar_transaction_changes', '0.0.1')
gem.add_dependency('cityhash', '0.6.0')
gem.add_development_dependency('rake')
gem.add_development_dependency('mocha')
gem.add_development_dependency('mysql2')
end
39 changes: 39 additions & 0 deletions lib/belongs_to_caching.rb
@@ -0,0 +1,39 @@
module IdentityCache
module BelongsToCaching

def self.included(base)
base.send(:extend, ClassMethods)
base.class_attribute :cached_belongs_tos
end

module ClassMethods
def cache_belongs_to(association, options = {})
self.cached_belongs_tos ||= {}
self.cached_belongs_tos[association] = options

options[:embed] ||= false
options[:cached_accessor_name] ||= "fetch_#{association}"
options[:foreign_key] ||= reflect_on_association(association).foreign_key
options[:associated_class] ||= reflect_on_association(association).class_name

if options[:embed]
raise NotImplementedError
else
build_normalized_belongs_to_cache(association, options)
end
end

def build_normalized_belongs_to_cache(association, options)
self.class_eval(ruby = <<-CODE, __FILE__, __LINE__)
def #{options[:cached_accessor_name]}
if IdentityCache.should_cache? && #{options[:foreign_key]}.present? && !association(:#{association}).loaded?
self.#{association} = #{options[:associated_class]}.fetch_by_id(#{options[:foreign_key]})
else
#{association}
end
end
CODE
end
end
end
end

0 comments on commit 6da8b66

Please sign in to comment.