New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement proxy class methods caching. #2

Open
wants to merge 1 commit into
base: master
from
Jump to file or symbol
Failed to load files and symbols.
+79 −12
Diff settings

Always

Just for now

Implement proxy class methods caching.

Now we have a proxy classes with the methods predefined to prevent
tons of `method_missing` calls.
  • Loading branch information...
nikitug committed Mar 31, 2013
commit 2a7b686b0b01ed2e642ab73143aab2f3bc33d6f1
@@ -1,20 +1,49 @@
module AttrAbility
module Model
class InstanceProxy
instance_methods.each { |m| undef_method m unless m =~ /(^__|^send$|^object_id$)/ }
module InstanceProxy
@@initialization_mutex = Mutex.new
def initialize(model, sanitizer)
@model = model
@sanitizer = sanitizer
def self.[](klass)
@@initialization_mutex.synchronize do
@_proxy_classes ||= {}
@_proxy_classes[klass] ||= Class.new do
include ProxyMethods
end
end
end
protected
module ProxyMethods
extend ActiveSupport::Concern
def method_missing(name, *args, &block)
@model.mass_assignment_sanitizer = @sanitizer
@model.send(name, *args, &block)
ensure
@model.mass_assignment_sanitizer = nil
included do
alias __proxy_class__ class
instance_methods.each { |m| undef_method m unless m =~ /(^__|^send$|^object_id$|^initialize$|^method_missing$)/ }
end
def initialize(model, sanitizer)
@model = model
@sanitizer = sanitizer
end
protected
def method_missing(name, *args, &block)
delegate_method(name)
self.send(name, *args, &block)
end
private
def delegate_method(name)
__proxy_class__.class_eval <<-METHOD, __FILE__, __LINE__ + 1
def #{name}(*args, &block)
@model.mass_assignment_sanitizer = @sanitizer
@model.#{name}(*args, &block)
ensure
@model.mass_assignment_sanitizer = nil
end
METHOD
end
end
end
end
@@ -62,7 +62,7 @@ def ability(action, attributes)
end
def as(ability)
AttrAbility::Model::InstanceProxy.new(self, self.class.build_sanitizer(ability))
AttrAbility::Model::InstanceProxy[self.class].new(self, self.class.build_sanitizer(ability))
end
def as_system
@@ -0,0 +1,38 @@
require 'spec_helper'
describe AttrAbility::Model::InstanceProxy do
with_model :ProxyPost do
table do |t|
t.string :title
t.integer :author_id
t.string :status
end
end
with_model :ProxyAuthor do
table do |t|
t.string :name
end
end
subject { AttrAbility::Model::InstanceProxy }
let(:sanitizer) { AttrAbility::Model::Sanitizer.new(SanitizerTestAbility.new(:system)) }
let(:post) { ProxyPost.new(title: "some") }
let(:author) { ProxyAuthor.new(name: "some") }
let(:proxied_post) { subject[ProxyPost].new(post, sanitizer) }
let(:proxied_author) { subject[ProxyAuthor].new(author, sanitizer) }
it "caches class" do
subject[ProxyPost] == subject[ProxyPost]
end
it "proxies methods" do
proxied_post.title.should == post.title
end
it "does not leak methods to another model" do
# ensure post cached
proxied_post.title
proxied_author.methods.should_not include(:title)
end
end
ProTip! Use n and p to navigate between commits in a pull request.