Module Boson::ArgumentInspector

  1. lib/boson/inspectors/argument_inspector.rb

Extracts arguments and their default values from methods either by by scraping a method’s text or with method_added and brute force eval (thanks to eigenclass).

Methods

public instance

  1. scrape_with_eval
  2. scrape_with_text

Constants

MAX_ARGS = 10   Max number of arguments extracted per method with scrape_with_eval

Public instance methods

scrape_with_eval (meth, klass, object)

Scrapes non-private methods for argument names and default values. Returns arguments as array of argument arrays with optional default value as a second element.

Examples:

def meth1(arg1, arg2='val', options={}) -> [['arg1'], ['arg2', 'val'], ['options', {}]]
def meth2(*args) -> [['*args']]
[show source]
# File lib/boson/inspectors/argument_inspector.rb, line 21
  def scrape_with_eval(meth, klass, object)
    unless %w[initialize].include?(meth.to_s)
      return if class << object; private_instance_methods(true).map {|e| e.to_s } end.include?(meth.to_s)
    end
    params, values, arity, num_args = trace_method_args(meth, klass, object)
    return if local_variables == params # nothing new found
    format_arguments(params, values, arity, num_args)
    rescue Exception
      # puts "#{klass}.#{meth}: #{$!.message}"
    ensure
      set_trace_func(nil)
  end
scrape_with_text (file_string, meth)

Returns same argument arrays as scrape_with_eval but argument defaults haven’t been evaluated.

[show source]
# File lib/boson/inspectors/argument_inspector.rb, line 7
  def scrape_with_text(file_string, meth)
    tabspace = "[ \t]"
    if match = /^#{tabspace}*def#{tabspace}+(?:\w+\.)?#{Regexp.quote(meth)}#{tabspace}*($|(?:\(|\s+)([^\n\)]+)\s*\)?\s*$)/.match(file_string)
      (match.to_a[2] || '').strip.split(/\s*,\s*/).map {|e| e.split(/\s*=\s*/)}
    end
  end