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 60 def self.definer type_or_instance, method_sym, class_or_instance_only = nil 61 return nil if type_or_instance.nil? or method_sym.nil? 62 return nil unless has_method(type_or_instance, method_sym, class_or_instance_only) 63 ancestors = ancestors_for type_or_instance 64 determine_definer ancestors, type_or_instance, method_sym, class_or_instance_only 65 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 44 def self.find_method type_or_instance, method_sym, class_or_instance_only = nil, include_ancestors = true 45 meta_method_suffixes = determine_meta_method_suffixes type_or_instance, class_or_instance_only 46 meta_method_suffixes.each do |suffix| 47 %w[public protected private].each do |protection| 48 meta_method = "#{protection}_#{suffix}" 49 found_methods = type_or_instance.send(meta_method, include_ancestors) 50 # Try both the symbol (ruby 1.9) and the string (1.8). 51 if found_methods.include?(method_sym) or found_methods.include?(method_sym.to_s) 52 return yield(type_or_instance, method_sym, protection.intern) 53 end 54 end 55 end 56 nil 57 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 37 def self.has_method type_or_instance, method_sym, class_or_instance_only = nil, include_ancestors = true 38 found = find_method(type_or_instance, method_sym, class_or_instance_only, include_ancestors) do |t_or_o, msym, protection| 39 true 40 end 41 found ? true : false # found could be nil; return false, if so 42 end
method_args_to_hash(*args) { |arg| ... }
click to toggle source
# File lib/aquarium/utils/method_utils.rb 18 def self.method_args_to_hash *args 19 return {} if args.empty? || (args.size == 1 && args[0].nil?) 20 hash = (args[-1] and args[-1].kind_of? Hash) ? args.pop : {} 21 args.each do |arg| 22 if block_given? 23 hash[arg] = yield arg 24 else 25 hash[arg] = nil 26 end 27 end 28 hash 29 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 10 def self.to_name string_or_symbol 11 if RUBY_VERSION =~ /^1.8/ 12 string_or_symbol.to_s 13 else 14 string_or_symbol.intern 15 end 16 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 31 def self.visibility type_or_instance, method_sym, class_or_instance_only = nil, include_ancestors = true 32 find_method(type_or_instance, method_sym, class_or_instance_only, include_ancestors) do |t_or_o, msym, protection| 33 protection 34 end 35 end