Class: MethodInfo::AncestorMethodStructure

Inherits:
Object
  • Object
show all
Defined in:
lib/method_info/ancestor_method_structure.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, options) ⇒ AncestorMethodStructure

Returns a new instance of AncestorMethodStructure.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/method_info/ancestor_method_structure.rb', line 43

def initialize(object, options)
  @object = object
  @options = options

  @ancestors = []
  @unattributed_methods = []

  if options[:singleton_methods]
    begin
      @ancestors << (class << object; self; end)
    rescue TypeError
    end
  end
  @ancestors += object.class.ancestors
  @ancestor_filter = AncestorFilter.new(@ancestors,
                                        :include => options[:ancestors_to_show],
                                        :exclude => options[:ancestors_to_exclude])

  @ancestor_methods = Hash.new
  @ancestors.each { |ancestor| @ancestor_methods[ancestor] = [] }
end

Class Method Details

.build(object, options) ⇒ Object

:ancestors_to_show (default: []) (Overrules the hiding of any ancestors as specified

by the :ancestors_to_exclude option)

:ancestors_to_exclude (default: []) (If a class is excluded, all modules included

under it are excluded as well, an ancestor specified in
:ancestors_to_show will be shown regardless of the this value)

:method_missing (default: false) :public_methods (default: true) :protected_methods (default: false) :private_methods (default: false) :singleton_methods (default: true) :include_names_of_excluded_ancestors (default: true) :include_names_of_methodless_ancestors (default: true) :enable_colors (default: false) :class_color Set colour for a line printing out a class (only used when :enable_colors is true) :module_color Set colour for a line printing out a module (only used when :enable_colors is true) :message_color Set colour for a line with a message (only used when :enable_colors is true) :methods_color Set colour for a line with methods (only used when :enable_colors is true) :punctuation_color Set colour for punctuation (only used when :enable_colors is true) :suppress_slowness_warning Does not print out the warning about slowness on older ruby versions (default: false)



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/method_info/ancestor_method_structure.rb', line 24

def self.build(object, options)
  if VERSION < "1.8.7" && !options[:suppress_slowness_warning]
    STDERR.puts "You are using Ruby #{VERSION}, this may take a while. It will be faster for >=1.8.7."
  end

  methods = []
  methods += object.methods if options[:public_methods]
  methods += object.protected_methods if options[:protected_methods]
  methods += object.private_methods if options[:private_methods]
  methods -= object.singleton_methods unless options[:singleton_methods]

  ancestor_method_structure = AncestorMethodStructure.new(object, options)

  methods.each do |method|
    ancestor_method_structure.add_method_to_ancestor(method)
  end
  ancestor_method_structure
end

Instance Method Details

#add_method_to_ancestor(method) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/method_info/ancestor_method_structure.rb', line 65

def add_method_to_ancestor(method)
  ancestor = method_owner(method)
  if @ancestors.include?(ancestor)
    @ancestor_methods[ancestor] << method
  end
  unless ancestor
    @unattributed_methods << method
  end
end

#to_aObject



75
76
77
# File 'lib/method_info/ancestor_method_structure.rb', line 75

def to_a
  ancestors_with_methods.map { |ancestor| [ancestor, @ancestor_methods[ancestor].sort] }
end

#to_sObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/method_info/ancestor_method_structure.rb', line 79

def to_s
  if @options[:enable_colors]
    require 'term/ansicolor'

    class_color = @options[:color_class] || Term::ANSIColor.yellow
    module_color = @options[:color_module] || Term::ANSIColor.red
    message_color = @options[:color_message] || Term::ANSIColor.green
    methods_color = @options[:color_methods] || Term::ANSIColor.reset
    punctuation_color = @options[:color_punctuation] || Term::ANSIColor.reset
    reset_color = Term::ANSIColor.reset
  else
    class_color = ""
    module_color = ""
    message_color = ""
    methods_color = ""
    reset_color = ""
    punctuation_color = ""
  end

  s = ancestors_with_methods.map do |ancestor|
    "%s::: %s :::\n%s%s\n" % [ancestor.is_a?(Class) ? class_color : module_color,
                              ancestor.to_s,
                              methods_color,
                              @ancestor_methods[ancestor].sort.join("#{punctuation_color}, #{methods_color}")]
  end.join('')
  if @options[:include_names_of_methodless_ancestors] && ! methodless_ancestors.empty?
    s += "#{message_color}Methodless:#{reset_color} " + methodless_ancestors.join(', ') + "\n"
  end
  if @options[:include_names_of_excluded_ancestors] && ! @ancestor_filter.excluded.empty?
    s += "#{message_color}Excluded:#{reset_color} " + @ancestor_filter.excluded.join(', ') + "\n"
  end
  if @options[:include_names_of_unattributed_methods] && ! @unattributed_methods.empty?
    s += "#{message_color}Unattributed methods:#{reset_color} " + @unattributed_methods.join(', ') + "\n"
  end
  s += reset_color
  s
end