Module: SimpleAspect

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

Constant Summary collapse

VERSION =
'0.2.1'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def self.extended(base)
  base.instance_eval do
    @sa_methods_to_aspect_methods_name = {}
    @sa_ignoring_new_methods = false
    @sa_mutex = Mutex.new
  end
end

Instance Method Details

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

Register an aspect method around the target method

Examples:

without a defined method

class Worker
  extend SimpleAspect

  aspect_around :perform do |*args, &original|
    result = original.call
    log(result)
  end

  def perform(*args)
    # do_work
  end

  def log(message)
    # log
  end
end

with a defined method


class Worker
  extend SimpleAspect

  aspect_around :perform, :log

  def perform(*args)
    # do_work
  end

  def log(*args)
    # log *args
    result = yield
    # log result
  end
end

Parameters:

  • method (Symbol)

    name of the target method

  • instance_around_method_name (Symbol) (defaults to: sa_instance_around_method_name(method))

    name of the aspect method. If not provided, the definition of the aspect method should be provided with a Proc/lambda

  • block (Proc)

    the definition of the aspect method if its name was not provided. This Proc needs to receive two arguments: *orig_args, &orig_block



60
61
62
63
64
65
66
# File 'lib/simple_aspect.rb', line 60

def aspect_around(
  method,
  instance_around_method_name = sa_instance_around_method_name(method),
  &block
)
  sa_register_aspect_on_method(method, instance_around_method_name, &block)
end

#method_added(method) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



69
70
71
72
73
# File 'lib/simple_aspect.rb', line 69

def method_added(method)
  return if sa_should_not_redefine?(method)

  sa_redefine_original_method(method, sa_get_aspect_method_name(method))
end