Class: Aspector::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/aspector/base.rb,
lib/aspector/base_class_methods.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, options = {}) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/aspector/base.rb', line 9

def initialize target, options = {}
  @target = target

  default_options = self.class.default_options
  if default_options and not default_options.empty?
    @options = default_options.merge(options)
  else
    @options = options
  end

  @wrapped_methods = {}
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/aspector/base.rb', line 7

def options
  @options
end

#targetObject (readonly)

Returns the value of attribute target.



6
7
8
# File 'lib/aspector/base.rb', line 6

def target
  @target
end

Instance Method Details

#advicesObject



34
35
36
# File 'lib/aspector/base.rb', line 34

def advices
  self.class.advices
end

#applyObject



38
39
40
41
42
43
44
45
46
# File 'lib/aspector/base.rb', line 38

def apply
  include_extension_module
  invoke_deferred_logics
  define_methods_for_advice_blocks
  add_to_instances unless @options[:old_methods_only]
  apply_to_methods unless @options[:new_methods_only]
  add_method_hooks unless @options[:old_methods_only]
  # TODO: clear deferred logic results if they are not used in any advice
end

#apply_to_method(method, scope = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/aspector/base.rb', line 80

def apply_to_method method, scope = nil
  filtered_advices = filter_advices advices, method
  return if filtered_advices.empty?

  logger.log Logging::DEBUG, 'apply-to-method', method

  scope ||=
      if context.private_instance_methods.include?(RUBY_VERSION.index('1.9') ? method.to_sym : method.to_s)
        :private
      elsif context.protected_instance_methods.include?(RUBY_VERSION.index('1.9') ? method.to_sym : method.to_s)
        :protected
      else
        :public
      end

  recreate_method method, filtered_advices, scope
end

#apply_to_methodsObject



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
# File 'lib/aspector/base.rb', line 48

def apply_to_methods
  return if advices.empty?

  # If method/methods option is set and all are String or Symbol, apply to those only, instead of
  # iterating through all methods
  methods = [@options[:method] || @options[:methods]]
  methods.compact!
  methods.flatten!

  if not methods.empty? and methods.all?{|method| method.is_a? String or method.is_a? Symbol }
    methods.each do |method|
      apply_to_method(method.to_s)
    end

    return
  end

  context.public_instance_methods.each do |method|
    apply_to_method(method.to_s, :public)
  end

  context.protected_instance_methods.each do |method|
    apply_to_method(method.to_s, :protected)
  end

  if @options[:private_methods]
    context.private_instance_methods.each do |method|
      apply_to_method(method.to_s, :private)
    end
  end
end

#disabled?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/aspector/base.rb', line 22

def disabled?
  # Enabled by default
end

#loggerObject



26
27
28
29
30
31
32
# File 'lib/aspector/base.rb', line 26

def logger
  return @logger if @logger

  @logger = Logging.get_logger(self)
  @logger.level = self.class.logger.level
  @logger
end