Class: Aspectory::Introspector

Inherits:
Object
  • Object
show all
Defined in:
lib/aspectory/introspector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, options = {}) ⇒ Introspector

Returns a new instance of Introspector.



5
6
7
8
# File 'lib/aspectory/introspector.rb', line 5

def initialize(klass, options={})
  @klass, @options = klass, options
  @observed_methods = { }
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



3
4
5
# File 'lib/aspectory/introspector.rb', line 3

def klass
  @klass
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/aspectory/introspector.rb', line 3

def options
  @options
end

Instance Method Details

#defined_methodsObject



23
24
25
# File 'lib/aspectory/introspector.rb', line 23

def defined_methods
  (klass.instance_methods - Object.instance_methods).map(&:to_sym)
end

#has_method?(method_id) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/aspectory/introspector.rb', line 27

def has_method?(method_id)
  klass.instance_method(method_id) rescue false
end

#observe(method_id, options = {}, &block) ⇒ Object



10
11
12
13
14
# File 'lib/aspectory/introspector.rb', line 10

def observe(method_id, options={}, &block)
  observe_klass!
  @observed_methods[method_id] ||= ObservedMethod.new(method_id, options)
  @observed_methods[method_id].push(block) if block_given?
end

#observe_klass!Object



16
17
18
19
20
21
# File 'lib/aspectory/introspector.rb', line 16

def observe_klass!
  @observed ||= begin
    name = options[:meta] ? :singleton_method_added : :method_added
    install_hook(name) or true
  end
end

#observing?(method_id) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/aspectory/introspector.rb', line 31

def observing?(method_id)
  not not @observed_methods[method_id]
end