module Aquarium::Utils::MethodUtils

Public Class Methods

definer(type_or_instance, method_sym, class_or_instance_only = nil) click to toggle source

Which type in a hierarchy actually defines a method?

# File lib/aquarium/utils/method_utils.rb, line 60
def self.definer type_or_instance, method_sym, class_or_instance_only = nil
  return nil if type_or_instance.nil? or method_sym.nil? 
  return nil unless has_method(type_or_instance, method_sym, class_or_instance_only)
  ancestors  = ancestors_for type_or_instance
  determine_definer ancestors, type_or_instance, method_sym, class_or_instance_only
end
find_method(type_or_instance, method_sym, class_or_instance_only = nil, include_ancestors = true) { |type_or_instance, method_sym, intern| ... } click to toggle source
# File lib/aquarium/utils/method_utils.rb, line 44
def self.find_method type_or_instance, method_sym, class_or_instance_only = nil, include_ancestors = true
  meta_method_suffixes = determine_meta_method_suffixes type_or_instance, class_or_instance_only
  meta_method_suffixes.each do |suffix|
    %w[public protected private].each do |protection|
      meta_method = "#{protection}_#{suffix}"
      found_methods = type_or_instance.send(meta_method, include_ancestors)
      # Try both the symbol (ruby 1.9) and the string (1.8).
      if found_methods.include?(method_sym) or found_methods.include?(method_sym.to_s)
        return yield(type_or_instance, method_sym, protection.intern)
      end
    end
  end
  nil
end
has_method(type_or_instance, method_sym, class_or_instance_only = nil, include_ancestors = true) click to toggle source
# File lib/aquarium/utils/method_utils.rb, line 37
def self.has_method type_or_instance, method_sym, class_or_instance_only = nil, include_ancestors = true
  found = find_method(type_or_instance, method_sym, class_or_instance_only, include_ancestors) do |t_or_o, msym, protection| 
    true
  end 
  found ? true : false   # found could be nil; return false, if so
end
method_args_to_hash(*args) { |arg| ... } click to toggle source
# File lib/aquarium/utils/method_utils.rb, line 18
def self.method_args_to_hash *args
  return {} if args.empty? || (args.size == 1 && args[0].nil?)
  hash = (args[-1] and args[-1].kind_of? Hash) ? args.pop : {}
  args.each do |arg|
    if block_given?
      hash[arg] = yield arg
    else 
      hash[arg] = nil
    end
  end
  hash
end
to_name(string_or_symbol) click to toggle source

The metaprogramming methods such as “public_instance_methods” require strings for 1.8, symbols for 1.9.

# File lib/aquarium/utils/method_utils.rb, line 10
def self.to_name string_or_symbol
  if RUBY_VERSION =~ /^1.8/
    string_or_symbol.to_s
  else
    string_or_symbol.intern
  end
end
visibility(type_or_instance, method_sym, class_or_instance_only = nil, include_ancestors = true) click to toggle source
# File lib/aquarium/utils/method_utils.rb, line 31
def self.visibility type_or_instance, method_sym, class_or_instance_only = nil, include_ancestors = true
  find_method(type_or_instance, method_sym, class_or_instance_only, include_ancestors) do |t_or_o, msym, protection| 
    protection
  end
end