Module: SimpleAspect

Defined in:
lib/simple_aspect.rb,
lib/simple_aspect/version.rb

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/simple_aspect.rb', line 6

def self.extended(base)
  base.instance_eval do
    @_sa_watcher_aspects = {}
    @_sa_ignoring_methods = false
    @_sa_lock = Mutex.new
  end
end

Instance Method Details

#aspect_around(method, instance_around_method = instance_around_method_name(method), &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/simple_aspect.rb', line 14

def aspect_around(
  method, instance_around_method = instance_around_method_name(method), &block
)
  @_sa_watcher_aspects[method] = instance_around_method

  if block
    define_method(instance_around_method, &block)
    self.instance_eval { private instance_around_method }
  end
end

#instance_around_method_name(method) ⇒ Object



51
52
53
# File 'lib/simple_aspect.rb', line 51

def instance_around_method_name(method)
  "_sa_around_#{method}".to_sym
end

#method_added(method) ⇒ Object



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
# File 'lib/simple_aspect.rb', line 25

def method_added(method)
  aspect = @_sa_watcher_aspects[method]

  return if @_sa_ignoring_methods || !aspect

  @_sa_lock.synchronize do
    begin
      @_sa_ignoring_methods = true

      orig_impl = instance_method(method)

      define_method(method) do |*args, &block|
        result = nil

        send(aspect, *args) do
          result = orig_impl.bind(self).call(*args, &block)
        end

        result
      end
    ensure
      @_sa_ignoring_methods = false
    end
  end
end