Module: Aquarium::Utils::MethodUtils

Defined in:
lib/aquarium/utils/method_utils.rb

Class Method Summary collapse

Class Method Details

.definer(type_or_instance, method_sym, class_or_instance_only = nil) ⇒ Object

Which type in a hierarchy actually defines a method?



60
61
62
63
64
65
# 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) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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) ⇒ Object



37
38
39
40
41
42
# 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) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# 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) ⇒ Object

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



10
11
12
13
14
15
16
# 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) ⇒ Object



31
32
33
34
35
# 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