Module: Ramaze::Helper::StackedAspect
- Defined in:
- lib/zen/helper/stacked_aspect.rb
Overview
StackedAspect is a helper similar to Innate::Helper::Aspect but allows
calls to methods such as before_all to stack on top of each other
instead of overwriting previously defined ones.
Although this helper works very similar to the Aspect helper there are two main differences besides the calls being stackable:
- All methods are prefixed with
stacked_ - All methods require you to specify a name for the block.
Both these items exist to achieve compatibility with the Aspect helper as well as being able to reload code using the reloader without adding all the blocks each time the code is reloaded.
Basic usage of this helper is as following:
class Posts < Ramaze::Controller
map '/posts'
helper :stacked_aspect
stacked_before_all(:validate_ip) do
validate_ip
end
stacked_before_all(:validate_user) do
validate_user
end
def index
end
end
In this example both methods (validate_ip and validate_user) would
be executed before calling #index().
Defined Under Namespace
Modules: ClassMethods
Constant Summary
- STACKED_AOP =
Hash that will contain all the STACKED_AOP actions.
Hash.new { |h, k| h[k] = Hash.new { |hh, kk| hh[kk] = {} } }
Class Method Summary (collapse)
- + (Object) ancestral_aop(from)
-
+ (Object) aspect_warn(name)
Shows a warning to inform the developer that the aspect name is already in use.
-
+ (Object) included(into)
Called whenever this module is included into a class.
Instance Method Summary (collapse)
-
- (Object) stacked_aspect_call(position, name)
Calls a certain AOP action for the specified position and method name.
- - (Object) stacked_aspect_wrap(action)
Class Method Details
+ (Object) ancestral_aop(from)
58 59 60 61 62 63 64 65 66 |
# File 'lib/zen/helper/stacked_aspect.rb', line 58 def self.ancestral_aop(from) aop = {} from.ancestors.reverse.map do |anc| aop.merge!(STACKED_AOP[anc]) if anc < StackedAspect end return aop end |
+ (Object) aspect_warn(name)
Shows a warning to inform the developer that the aspect name is already in use.
75 76 77 78 79 80 81 82 |
# File 'lib/zen/helper/stacked_aspect.rb', line 75 def self.aspect_warn(name) file, line = caller[1].split(':') Ramaze::Log.warn( "The aspect name \"#{name}\" is already in use, it was redefined " \ "in #{file} on line #{line}" ) end |
+ (Object) included(into)
Called whenever this module is included into a class.
52 53 54 55 |
# File 'lib/zen/helper/stacked_aspect.rb', line 52 def self.included(into) into.extend(ClassMethods) into.add_action_wrapper(7.0, :stacked_aspect_wrap) end |
Instance Method Details
- (Object) stacked_aspect_call(position, name)
Calls a certain AOP action for the specified position and method name.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/zen/helper/stacked_aspect.rb', line 92 def stacked_aspect_call(position, name) return unless aop = StackedAspect.ancestral_aop(self.class) return unless block = aop[position] name = name.to_sym # Bail out if the position is :before or :after but the current method # doesn't match. if [:before, :after].include?(position) and !block[name].is_a?(Hash) return end # Extract the sub hash for the current method, this is needed for # :before and :after. if block.is_a?(Hash) and block[name].is_a?(Hash) block = block[name] end if block.respond_to?(:each) block.each do |k, v| instance_eval(&v) end else instance_eval(&block) end end |
- (Object) stacked_aspect_wrap(action)
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/zen/helper/stacked_aspect.rb', line 120 def stacked_aspect_wrap(action) return yield unless method = action.name stacked_aspect_call(:before_all, method) stacked_aspect_call(:before, method) result = yield stacked_aspect_call(:after, method) stacked_aspect_call(:after_all, method) return result end |