lib/glue/aspects.rb in glue-0.23.0 vs lib/glue/aspects.rb in glue-0.24.0

- old
+ new

@@ -1,6 +1,6 @@ -require 'glue/property' +require 'mega/inheritor' module Glue # An Aspect is a class that defines advices. @@ -31,12 +31,12 @@ # post :taraa, :on => login # end # # module Timestamped # pre :on => :og_insert { |this| this.create_time = Time.now } -# pre :on => :og_update { |this| this.update_time = Time.now } -# pre :on => [:og_insert, :og_update] { |this| this.create_time = Time.now } +# pre :on => :og_update { |this| this.update_time = Time.now } +# pre :on => [:og_insert, :og_update] { |this| this.create_time = Time.now } # end module Aspects # Store the code and the metadata (options) for @@ -70,35 +70,44 @@ end # Include Modules that define advices. def self.include_advice_modules(target) + add_advices = [] + del_advices = [] + for a in target.advices if a.code.is_a?(Module) and (!a.code.class.ancestors.include?(Class)) target.module_eval %{ include #{a.code} } options = a.options.reject { |k,v| k == :pre || k == :post } method = (a.options[:pre] || 'pre').to_s if a.code.instance_methods.include?(method) options.update(:where => :prepend, :join => :pre) - target.advices << Advice.new(method, options) + add_advices << Advice.new(method.to_sym, options) end method = (a.options[:post] || 'post').to_s if a.code.instance_methods.include?(method) options.update(:where => :append, :join => :post) - target.advices << Advice.new(method, options) + add_advices << Advice.new(method.to_sym, options) end + + del_advices << a end end - # Remove the original advice. - - target.advices.delete_if do |a| - a.code.is_a?(Module) and (!a.code.class.ancestors.include?(Class)) + # Delete the original advices. + + for a in del_advices + target.advices!.delete(a) end + + # Add the new advices. + + target.advices!.concat(add_advices) end # Generates the code to call the aspects. def self.gen_advice_code(method, advices, join = :pre) # :nodoc: @@ -140,22 +149,11 @@ end def self.append_features(base) super base.extend(ClassMethods) - - base.module_eval %{ - Glue::PropertyUtils.enchant(self) - - def self.advices - __meta[:advices] || [] - end - - def self.advices=(advices) - __meta[:advices] = advices - end - } + base.inheritor :advices, [], :+ end module ClassMethods # Add a pre (before) advice. @@ -166,20 +164,22 @@ :where => :prepend, } options.update(args.pop) if args.last.is_a?(Hash) if block_given? - advices = [ Advice.new(block, options) ] + new_advices = [ Advice.new(block, options) ] else - advices = args.collect { |a| Advice.new(a, options) } + new_advices = args.collect { |a| Advice.new(a, options) } end - +=begin if options[:where] == :prepend self.advices = advices + self.advices else self.advices = self.advices + advices end +=end + self.advices!.concat(new_advices) end alias_method :before, :pre # Add a post (after) advice. @@ -189,20 +189,22 @@ :where => :append, } options.update(args.pop) if args.last.is_a?(Hash) if block_given? - advices = [ Advice.new(block, options) ] + new_advices = [ Advice.new(block, options) ] else - advices = args.collect { |a| Advice.new(a, options) } + new_advices = args.collect { |a| Advice.new(a, options) } end - +=begin if options[:where] == :prepend self.advices = advices + self.advices else self.advices = self.advices + advices end +=end + self.advices!.concat(new_advices) end alias_method :after, :post # Add a wrap (arround) aspect. An aspect is a class that # responds to the before and after advices. @@ -213,10 +215,10 @@ :post => :post } options.update(args.pop) if args.last.is_a?(Hash) for aspect in args - self.advices << Advice.new(aspect, options) + self.advices! << Advice.new(aspect, options) end end alias_method :around, :wrap alias_method :observer, :wrap