Permalink
Browse files

Dropbox example, cosmetic changes in base.rb

  • Loading branch information...
gcao committed Dec 30, 2012
1 parent 02535eb commit f901dde5c2088ea4d4b222b09e3f3a0c8504ef26
Showing with 133 additions and 5 deletions.
  1. +123 −0 examples/dropbox_concern_rewrite.rb
  2. +10 −5 lib/aspector/base.rb
@@ -0,0 +1,123 @@
+# http://37signals.com/svn/posts/3372-put-chubby-models-on-a-diet-with-concerns
+
+module Dropboxed
+ extend ActiveSupport::Concern
+
+ included do
+ before_create :generate_dropbox_key
+ end
+
+ def rekey_dropbox
+ generate_dropbox_key
+ save!
+ end
+
+ private
+
+ def generate_dropbox_key
+ self.dropbox_key = SignalId::Token.unique(24) do |key|
+ self.class.find_by_dropbox_key(key)
+ end
+ end
+end
+
+A.send :include, Dropboxd
+
+##############################
+
+class DropboxAspect < Aspector::Base
+ target do
+ def rekey_dropbox
+ generate_dropbox_key
+ save!
+ end
+
+ private
+
+ def generate_dropbox_key
+ self.dropbox_key = SignalId::Token.unique(24) do |key|
+ self.class.find_by_dropbox_key(key)
+ end
+ end
+ end
+
+ before :create, :generate_dropbox_key
+end
+
+DropboxAspect.apply(A)
+
+##############################
+
+# Below aspect can be disabled completely
+class DropboxAspect2 < Aspector::Base
+ target do |aspect|
+ define_method :rekey_dropbox do
+ if aspect.disabled?
+ aspect.logger.warn "Exit from rekey_dropbox because #{aspect} is disabled."
+ return
+ end
+
+ generate_dropbox_key
+ save!
+ end
+
+ define_method :generate_dropbox_key do
+ if aspect.disabled?
+ aspect.logger.warn "Exit from generate_dropbox_key because #{aspect} is disabled."
+ return
+ end
+
+ self.dropbox_key = SignalId::Token.unique(24) do |key|
+ self.class.find_by_dropbox_key(key)
+ end
+ end
+
+ private :generate_dropbox_key
+ end
+
+ before :create, :generate_dropbox_key
+end
+
+DropboxAspect2.apply(A)
+
+a = A.new
+aspect = DropboxAspect2.apply(a)
+a.rekey_dropbox
+aspect.disable
+a.rekey_dropbox # Will do nothing
+
+##############################
+
+class DropboxAspect3 < Aspector::Base
+ target do
+ attr_accessor :dropbox_key
+ end
+
+ def rekey_dropbox
+ if disabled?
+ logger.warn "Exit from rekey_dropbox because #{self} is disabled."
+ return
+ end
+
+ generate_dropbox_key
+ target.save!
+ end
+
+ def generate_dropbox_key
+ if disabled?
+ logger.warn "Exit from generate_dropbox_key because #{self} is disabled."
+ return
+ end
+
+ target.dropbox_key = SignalId::Token.unique(24) do |key|
+ target.class.find_by_dropbox_key(key)
+ end
+ end
+
+ before :create, :generate_dropbox_key
+end
+
+a = A.new
+aspect = DropboxAspect3.apply(a)
+aspect.rekey_dropbox
+
View
@@ -84,11 +84,14 @@ def aop_apply
aop_add_to_instances unless @aop_options[:old_methods_only]
aop_apply_to_methods unless @aop_options[:new_methods_only]
aop_add_method_hooks unless @aop_options[:old_methods_only]
+ # TODO: clear deferred logic results if they are not used in any advice
after_apply
end
alias apply aop_apply
def aop_apply_to_methods
+ return if aop_advices.empty?
+
advices = aop_advices
# If method/methods option is set and all are String or Symbol, apply to those only, instead of
@@ -194,6 +197,8 @@ def aop_define_methods_for_advice_blocks
end
def aop_add_to_instances
+ return if aop_advices.empty?
+
aspect_instances = @aop_context.instance_variable_get(:@aop_instances)
unless aspect_instances
aspect_instances = AspectInstances.new
@@ -203,6 +208,8 @@ def aop_add_to_instances
end
def aop_add_method_hooks
+ return if aop_advices.empty?
+
if @aop_options[:class_methods]
return unless @aop_target.is_a?(Module)
@@ -312,10 +319,8 @@ def aop_recreate_method_with_advices method, before_advices, after_advices, arou
result = catch(:aop_returns) do
% end
-% unless before_advices.empty?
- # Before advices
-% end
% before_advices.each do |advice|
+ # Before advice: <%= advice.name %>
% if aop_logger.visible?(Logging::TRACE)
aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'before-invoke-advice', '<%= advice.name %>'
% end
@@ -337,7 +342,7 @@ def aop_recreate_method_with_advices method, before_advices, after_advices, arou
% end
% if around_advice
- # Around advice
+ # Around advice: <%= around_advice.name %>
% if aop_logger.visible?(Logging::TRACE)
aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'before-invoke-advice', '<%= around_advice.name %>'
% end
@@ -376,8 +381,8 @@ def aop_recreate_method_with_advices method, before_advices, after_advices, arou
% end
% unless after_advices.empty?
- # After advices
% after_advices.each do |advice|
+ # After advice: <%= advice.name %>
% if aop_logger.visible?(Logging::TRACE)
aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'before-invoke-advice', '<%= advice.name %>'
% end

0 comments on commit f901dde

Please sign in to comment.