File: aspects.rb

package info (click to toggle)
ruby-facets 2.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,824 kB
  • sloc: ruby: 25,483; xml: 90; makefile: 20
file content (254 lines) | stat: -rw-r--r-- 6,330 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# TITLE:
#
#   Aspects
#
# DESCRIPTION:
#
#   An Aspect is mixin of AOP meta-methods for defining advice.
#
# COPYRIGHT:
#
#   Copyright (c) 2005 George Moschovitis
#
# LICENSE:
#
#   Ruby License
#
#   This module is free software. You may use, modify, and/or redistribute this
#   software under the same terms as Ruby.
#
#   This program is distributed in the hope that it will be useful, but WITHOUT
#   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#   FOR A PARTICULAR PURPOSE.
#
# AUTHORS:
#
#   - George Moschovitis


require 'facets/inheritor'


# = Aspect
#
# An Aspect is a class that defines advices.
#
# == Usage
#
# George, please do!
#

class Aspect
  class << self
    def wrap(target, methods = target.instance_methods, pre = :pre, post = :post)
      target.send(:include, Aspects) unless target.ancestors.include?(Aspects)
      target.wrap(self, :pre => pre, :post => post)
    end

    alias_method :observe, :wrap
  end

  def wrap(target, methods = target.instance_methods, pre = :pre, post = :post)
    target.send(:include, Aspects) unless target.ancestors.include?(Aspects)
    target.wrap(self, :pre => pre, :post => post)
  end
  alias_method :observe, :wrap
end

# Add support for Aspect Oriented Programming (AOP).
#
# === Examples
#
# class Controller
#   pre :force_login, :where => :prepend
#   wrap Benchmark, :on => :index
#   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 }
# end

module Aspects

  # Store the code and the metadata (options) for
  # an Advice.

  Advice = Struct.new(:code, :options)

  # Apply the advices to the target class.

  def self.wrap(target, methods = target.instance_methods)
    include_advice_modules(target)

    for m in [methods].flatten
      target.module_eval <<-end_eval, __FILE__, __LINE__
        alias_method :__unwrapped_#{m}, :#{m}
        def #{m}(*args,&block)
          #{gen_advice_code(m, target.advices, :pre)}
          __unwrapped_#{m}(*args,&block)
          #{gen_advice_code(m, target.advices, :post)}
        end
      end_eval
    end
  end

  class << self
    alias_method :apply_advices, :wrap
  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)
          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)
          add_advices <<  Advice.new(method.to_sym, options)
        end

        del_advices << a
      end
    end

    # 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:
    code = ''

    advices.each_with_index do |advice, idx|
      o = options = advice.options

      if only = options[:only] || options[:on]
        next unless [only].flatten.include?(method.to_sym)
      elsif except = options[:except]
        next if [except].flatten.include?(method.to_sym)
      end

      advice = advice.code

      if advice.is_a?(Symbol) or advice.is_a?(String)
        next if o[:join] != join
        code << "#{advice}; "
      elsif advice.respond_to?('call')
        next if o[:join] != join
        code << "self.class.advices[#{idx}].code.call(self); "
      elsif advice.is_a?(Class)
        if advice.class.ancestors.include?(Class)
          if m = o[join] and advice.methods.include?(m.to_s)
            code << "#{advice}.#{m}(self); "
          end
        else
          # Module, already handled.
        end
      else
        if m = o[join] and advice.methods.include?(m.to_s)
          code << "self.class.advices[#{idx}].code.#{m}(self); "
        end
      end
    end

    return code
  end

  def self.append_features(base)
    super
    base.extend(ClassMethods)
    base.inheritor :advices, [], :+
  end

  module ClassMethods #:nodoc:

    # Add a pre (before) advice.

    def pre(*args, &block)
      o = options = {
        :join => :pre,
        :where => :prepend,
      }
      options.update(args.pop) if args.last.is_a?(Hash)

      if block_given?
        new_advices = [ Advice.new(block, options) ]
      else
        new_advices = args.collect { |a| Advice.new(a, options) }
      end
#       if options[:where] == :prepend
#         self.advices = advices + self.advices
#       else
#         self.advices = self.advices + advices
#       end
      self.advices!.concat(new_advices)
    end
    alias_method :before, :pre

    # Add a post (after) advice.

    def post(*args, &block)
      o = options = {
        :join => :post,
        :where => :append,
      }
      options.update(args.pop) if args.last.is_a?(Hash)

      if block_given?
        new_advices = [ Advice.new(block, options) ]
      else
        new_advices = args.collect { |a| Advice.new(a, options) }
      end
#       if options[:where] == :prepend
#         self.advices = advices + self.advices
#       else
#         self.advices = self.advices + advices
#       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.

    def wrap(*args)
      o = options = {
        :pre => :pre,
        :post => :post
      }
      options.update(args.pop) if args.last.is_a?(Hash)

      for aspect in args
        self.advices! << Advice.new(aspect, options)
      end
    end
    alias_method :around, :wrap
    alias_method :observer, :wrap

  end

end