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.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/aspector/base.rb', line 14

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

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

  # @aop_context is where advices will be applied (i.e. where methods are modified), can be different from target
  @aop_context = aop_get_context

  @aop_wrapped_methods = {}

  after_initialize
end

Instance Attribute Details

#aop_optionsObject (readonly) Also known as: options

Returns the value of attribute aop_options.



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

def aop_options
  @aop_options
end

#aop_targetObject (readonly) Also known as: target

Returns the value of attribute aop_target.



9
10
11
# File 'lib/aspector/base.rb', line 9

def aop_target
  @aop_target
end

#aop_wrapped_methodsObject (readonly)

Returns the value of attribute aop_wrapped_methods.



12
13
14
# File 'lib/aspector/base.rb', line 12

def aop_wrapped_methods
  @aop_wrapped_methods
end

Instance Method Details

#aop_advicesObject Also known as: advices



71
72
73
# File 'lib/aspector/base.rb', line 71

def aop_advices
  self.class.aop_advices
end

#aop_applyObject Also known as: apply



76
77
78
79
80
81
82
83
84
# File 'lib/aspector/base.rb', line 76

def aop_apply
  before_apply
  aop_invoke_deferred_logics
  aop_define_methods_for_advice_blocks
  aop_add_to_instances unless @aop_options[:old_methods_only]
  aop_apply_to_methods unless @aop_options[:new_methods_only]
  aop_add_method_hooks unless @aop_options[:old_methods_only]
  after_apply
end

#aop_apply_to_method(method, advices, scope = nil) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/aspector/base.rb', line 119

def aop_apply_to_method method, advices, scope = nil
  advices = aop_filter_advices advices, method
  return if advices.empty?

  aop_logger.log Logger::APPLY_TO_METHOD, method
  before_apply_to_method method, advices

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

  aop_recreate_method method, advices, scope

  after_apply_to_method method, advices
end

#aop_apply_to_methodsObject



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

def aop_apply_to_methods
  advices = aop_advices

  # If method/methods option is set and all are String or Symbol, apply to those only, instead of
  # iterating through all methods
  methods = [@aop_options[:method] || @aop_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|
      aop_apply_to_method(method.to_s, advices)
    end

    return
  end

  @aop_context.public_instance_methods.each do |method|
    aop_apply_to_method(method.to_s, advices, :public)
  end

  @aop_context.protected_instance_methods.each do |method|
    aop_apply_to_method(method.to_s, advices, :protected)
  end

  if @aop_options[:private_methods]
    @aop_context.private_instance_methods.each do |method|
      aop_apply_to_method(method.to_s, advices, :private)
    end
  end
end

#aop_disableObject Also known as: disable



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

def aop_disable
  class << self
    def aop_disabled?; true; end
  end

  aop_disabled?
end

#aop_disabled?Boolean

Returns:

  • (Boolean)


59
60
# File 'lib/aspector/base.rb', line 59

def aop_disabled?
end

#aop_enableObject Also known as: enable



32
33
34
35
36
37
38
# File 'lib/aspector/base.rb', line 32

def aop_enable
  class << self
    def aop_disabled?; end
  end

  aop_disabled?
end

#aop_loggerObject Also known as: logger



66
67
68
# File 'lib/aspector/base.rb', line 66

def aop_logger
  @aop_logger ||= Logger.new(self, self.class.logger.level)
end

#aop_reset_disabledObject Also known as: reset_disabled



50
51
52
53
54
55
56
# File 'lib/aspector/base.rb', line 50

def aop_reset_disabled
  class << self
    remove_method :aop_disabled?
  end

  aop_disabled?
end

#disabled?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/aspector/base.rb', line 62

def disabled?
  aop_disabled?
end