Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit d2d75286a81ea6360e0e9ee66682d65975bf40d1 @garybernhardt committed Apr 17, 2011
Showing with 74 additions and 0 deletions.
  1. +38 −0 lib/do_not_want.rb
  2. +32 −0 spec/do_not_want_spec.rb
  3. +4 −0 spec/gems/fake_gem.rb
@@ -0,0 +1,38 @@
+class Object
+ def self.do_not_want!(method_name, reason)
+ @@do_not_want_original_methods ||= {}
+ @@do_not_want_original_methods[method_name] = instance_method(method_name)
+ self.send :define_method, method_name do
+ if DoNotWant.should_ignore_caller(caller)
+ return @@do_not_want_original_methods[method_name].bind(self).call
+ end
+ raise DoNotWant::NotSafe.new(self.class, method_name, reason)
+ end
+ end
+end
+
+module ActiveRecord
+ class Base
+ do_not_want!(:update_attribute, 'it skips validation')
+ do_not_want!(:save, 'it skips validation')
+ end
+end
+
+module DoNotWant
+ class NotSafe < Exception
+ def initialize(called_class, called_method, reason)
+ class_name = called_class.name
+ method_name = called_method.to_s
+ super "%s#%s isn't safe because %s" % [
+ class_name,
+ method_name,
+ reason
+ ]
+ end
+ end
+
+ def self.should_ignore_caller(caller)
+ /\/gems\//.match(caller[0])
+ end
+end
+
@@ -0,0 +1,32 @@
+module ActiveRecord
+ class Base
+ def update_attribute; end
+ def save; end
+ end
+end
+
+require 'do_not_want'
+require 'gems/fake_gem'
+
+describe 'do not want' do
+ let(:model_object) { ActiveRecord::Base.new }
+
+ it "raises an error for unwanted method calls" do
+ expect do
+ model_object.update_attribute(:attribute, :value)
+ end.to raise_error(DoNotWant::NotSafe)
+ end
+
+ it "lets other methods through" do
+ model_object.class.should == ActiveRecord::Base
+ end
+
+ context "caller filtering" do
+ it "ignores calls from gems" do
+ expect do
+ call_activerecord_update_attribute
+ end.not_to raise_error
+ end
+ end
+end
+
@@ -0,0 +1,4 @@
+def call_activerecord_update_attribute
+ ActiveRecord::Base.new.update_attribute
+end
+

0 comments on commit d2d7528

Please sign in to comment.