module Where

A little Ruby module for finding the source location where class and methods are defined. gist.github.com/wtaysom/1236979

Public Class Methods

are_instance_methods(klass, method_name) click to toggle source
# File lib/rbot/core/utils/where_is.rb, line 21
def are_instance_methods(klass, method_name)
  are_via_extractor(:method, klass, method_name)
end
are_methods(klass, method_name) click to toggle source
# File lib/rbot/core/utils/where_is.rb, line 17
def are_methods(klass, method_name)
  are_via_extractor(:method, klass, method_name)
end
edit(location) click to toggle source
# File lib/rbot/core/utils/where_is.rb, line 52
def edit(location)
  unless location.kind_of?(Array)
    raise TypeError,
      "only accepts a [file, line_number] array"
  end
  location
end
is_class(klass) click to toggle source
# File lib/rbot/core/utils/where_is.rb, line 25
def is_class(klass)
  methods = defined_methods(klass)
  file_groups = methods.group_by{|sl| sl[0]}
  file_counts = file_groups.map do |file, sls|
    lines = sls.map{|sl| sl[1]}
    count = lines.size
    line = lines.min
    {file: file, count: count, line: line}
  end
  file_counts.sort_by!{|fc| fc[:count]}
  source_locations = file_counts.map{|fc| [fc[:file], fc[:line]]}
  source_locations
end
is_class_primarily(klass) click to toggle source

Raises ArgumentError if klass does not have any Ruby methods defined in it.

# File lib/rbot/core/utils/where_is.rb, line 40
def is_class_primarily(klass)
  source_locations = is_class(klass)
  if source_locations.empty?
    methods = defined_methods(klass)
    raise ArgumentError, (methods.empty? ?
                          "#{klass} has no methods" :
                          "#{klass} only has built-in methods (#{methods.size} in total)"
                         )
  end
  source_locations[0]
end
is_instance_method(klass, method_name) click to toggle source
# File lib/rbot/core/utils/where_is.rb, line 13
def is_instance_method(klass, method_name)
  source_location(klass.instance_method(method_name))
end
is_method(klass, method_name) click to toggle source
# File lib/rbot/core/utils/where_is.rb, line 9
def is_method(klass, method_name)
  source_location(klass.method(method_name))
end
is_proc(proc) click to toggle source
# File lib/rbot/core/utils/where_is.rb, line 5
def is_proc(proc)
  source_location(proc)
end

Private Class Methods

are_via_extractor(extractor, klass, method_name) click to toggle source
# File lib/rbot/core/utils/where_is.rb, line 69
def are_via_extractor(extractor, klass, method_name)
  methods = klass.ancestors.map do |ancestor|
    method = ancestor.send(extractor, method_name)
    if method.owner == ancestor
      source_location(method)
    else
      nil
    end
  end
  methods.compact!      
  methods
end
defined_methods(klass) click to toggle source
# File lib/rbot/core/utils/where_is.rb, line 82
def defined_methods(klass)
  methods = klass.methods(false).map{|m| klass.method(m)} +
    klass.instance_methods(false).map{|m| klass.instance_method(m)}
  methods.map!(&:source_location)
  methods.compact!
  methods
end
source_location(method) click to toggle source
# File lib/rbot/core/utils/where_is.rb, line 62
def source_location(method)
  method.source_location || (
    method.to_s =~ /: (.*)>/
    $1
  )
end