Class: MethodInfo::AncestorMethodStructure

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

Constant Summary collapse

@@ruby_version_supports_owner_method =

: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) :match Shows only those methods that match this option. It’s value can be either a string or a regexp (default: nil)

nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, options) ⇒ AncestorMethodStructure



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/method_info/ancestor_method_structure.rb', line 39

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



28
29
30
31
32
33
34
35
36
37
# File 'lib/method_info/ancestor_method_structure.rb', line 28

def self.build(object, options)
  # print warning message if a Method does not support the :owner method
  if !options[:suppress_slowness_warning] && !ruby_version_supports_owner_method
    STDERR.puts "You are using a Ruby version (#{RUBY_VERSION}) that does not support the owner method of a Method - this may take a while. It will be faster for >=1.8.7."
  end

  ancestor_method_structure = AncestorMethodStructure.new(object, options)
  ancestor_method_structure.add_selected_methods_to_structure
  ancestor_method_structure
end

Instance Method Details

#add_method_to_ancestor(method) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/method_info/ancestor_method_structure.rb', line 61

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

#add_selected_methods_to_structureObject



121
122
123
124
125
126
127
# File 'lib/method_info/ancestor_method_structure.rb', line 121

def add_selected_methods_to_structure
  select_methods
  apply_match_filter_to_methods
  @methods.each do |method|
    add_method_to_ancestor(method)
  end
end

#to_aObject



71
72
73
# File 'lib/method_info/ancestor_method_structure.rb', line 71

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

#to_sObject



75
76
77
78
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
116
117
118
119
# File 'lib/method_info/ancestor_method_structure.rb', line 75

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

  ancestors_to_show = @ancestor_filter.picked
  unless @options[:include_names_of_methodless_ancestors]
    ancestors_to_show = ancestors_with_methods
  end

  s = ancestors_to_show.map do |ancestor|
    result = "%s::: %s :::\n" % [ancestor.is_a?(Class) ? class_color : module_color,
                                 ancestor.to_s]
    unless @ancestor_methods[ancestor].empty?
      result += "%s%s\n" % [methods_color,
                            @ancestor_methods[ancestor].sort.join("#{punctuation_color}, #{methods_color}")]
    end
    result
  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