Module: Boson::MethodInspector

Extended by:
MethodInspector
Included in:
MethodInspector
Defined in:
lib/boson/inspectors/method_inspector.rb

Overview

Gathers method attributes by redefining method_added and capturing method calls before a method. This module also saves method locations so CommentInspector can scrape their commented method attributes.

Constant Summary collapse

METHODS =
[:config, :desc, :options, :render_options]
METHOD_CLASSES =
{:config=>Hash, :desc=>String, :options=>Hash, :render_options=>Hash}
ALL_METHODS =
METHODS + [:option]
CALLER_REGEXP =
RUBY_VERSION < '1.9' ? /in `load_source'/ : /in `<module:.*>'/

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_moduleObject

Returns the value of attribute current_module.



7
8
9
# File 'lib/boson/inspectors/method_inspector.rb', line 7

def current_module
  @current_module
end

#mod_storeObject

Returns the value of attribute mod_store.



7
8
9
# File 'lib/boson/inspectors/method_inspector.rb', line 7

def mod_store
  @mod_store
end

Instance Method Details

#find_method_locations(stack) ⇒ Object

Returns an array of the file and line number at which a method starts using a caller array. Necessary information for CommentInspector to function.



61
62
63
64
65
# File 'lib/boson/inspectors/method_inspector.rb', line 61

def find_method_locations(stack)
  if (line = stack.find {|e| e =~ CALLER_REGEXP })
    (line =~ /^(.*):(\d+)/) ? [$1, $2.to_i] : nil
  end
end

#find_method_locations_for_19(klass, meth) ⇒ Object

:stopdoc:



68
69
70
71
72
73
# File 'lib/boson/inspectors/method_inspector.rb', line 68

def find_method_locations_for_19(klass, meth)
  if (klass = Util.any_const_get(klass)) && (meth_location = klass.method(meth).source_location) &&
    meth_location[0]
    meth_location
  end
end

#has_inspector_method?(meth, inspector) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/boson/inspectors/method_inspector.rb', line 85

def has_inspector_method?(meth, inspector)
  (store[inspector] && store[inspector].key?(meth.to_s)) || inspector_in_file?(meth.to_s, inspector)
end

#inspector_in_file?(meth, inspector_method) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
# File 'lib/boson/inspectors/method_inspector.rb', line 89

def inspector_in_file?(meth, inspector_method)
  return false if !(file_line = store[:method_locations] && store[:method_locations][meth])
  if File.exists?(file_line[0]) && (options = CommentInspector.scrape(
    FileLibrary.read_library_file(file_line[0]), file_line[1], @current_module, inspector_method) )
    (store[inspector_method] ||= {})[meth] = options
  end
end

#new_method_added(mod, meth) ⇒ Object

The method_added used while scraping method attributes.



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

def new_method_added(mod, meth)
  return unless mod.to_s[/^Boson::Commands::/]
  self.current_module = mod
  store[:temp] ||= {}
  METHODS.each do |e|
    store[e][meth.to_s] = store[:temp][e] if store[:temp][e]
  end
  (store[:options][meth.to_s] ||= {}).merge! store[:temp][:option] if store[:temp][:option]

  if store[:temp].size < ALL_METHODS.size
    store[:method_locations] ||= {}
    if (result = find_method_locations(caller))
      store[:method_locations][meth.to_s] = result
    end
  end
  store[:temp] = {}
  scrape_arguments(meth) if has_inspector_method?(meth, :options) || has_inspector_method?(meth,:render_options)
end

#option(mod, name, value) ⇒ Object



40
41
42
43
44
# File 'lib/boson/inspectors/method_inspector.rb', line 40

def option(mod, name, value)
  (@mod_store[mod] ||= {})[:options] ||= {}
  (store(mod)[:temp] ||= {})[:option] ||= {}
  (store(mod)[:temp] ||= {})[:option][name] = value
end

#scrape_arguments(meth) ⇒ Object

Scrapes a method’s arguments using ArgumentInspector.



47
48
49
50
51
52
53
54
55
56
# File 'lib/boson/inspectors/method_inspector.rb', line 47

def scrape_arguments(meth)
  store[:args] ||= {}

  o = Object.new
  o.extend(@current_module)
  # private methods return nil
  if (val = ArgumentInspector.scrape_with_eval(meth, @current_module, o))
    store[:args][meth.to_s] = val
  end
end

#store(mod = @current_module) ⇒ Object

Hash of a module’s method attributes i.e. descriptions, options by method and then attribute



76
77
78
# File 'lib/boson/inspectors/method_inspector.rb', line 76

def store(mod=@current_module)
  @mod_store[mod]
end