Class: ORI::ListMethod

Inherits:
Object show all
Defined in:
lib/ori/list_method.rb

Overview

Our method representation suitable for listing.

Constant Summary

OWN_MARKER =

:nodoc:

["~", " "]

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (ListMethod) initialize(attrs = {})

Returns a new instance of ListMethod



12
13
14
15
# File 'lib/ori/list_method.rb', line 12

def initialize(attrs = {})
  attrs.each {|k, v| send("#{k}=", v)} 
  clear_cache
end

Instance Attribute Details

- (Object) inspector

Returns the value of attribute inspector



10
11
12
# File 'lib/ori/list_method.rb', line 10

def inspector
  @inspector
end

- (Object) method_name

Returns the value of attribute method_name



9
10
11
# File 'lib/ori/list_method.rb', line 9

def method_name
  @method_name
end

- (Object) obj

Object. Can be anything, including nil.



7
8
9
# File 'lib/ori/list_method.rb', line 7

def obj
  @obj
end

Instance Method Details

- (Object) <=>(other)

Support Enumerable#sort.



218
219
220
# File 'lib/ori/list_method.rb', line 218

def <=>(other)
  [@method_name, access, obj_module_name] <=> [other.method_name, other.access, obj_module_name]
end

- (Object) access

Return method access substring: "::" or "#".



20
21
22
23
24
# File 'lib/ori/list_method.rb', line 20

def access
  # NOTE: It is *WRONG* to rely on Ruby's `inspect` to handle things because
  #       it doesn't work for cases when singleton methods are included from modules.
  @cache[:access] ||= (module? and not inspector.match /instance/) ? "::" : "#"
end

- (Boolean) eql?(other)

Support Array#uniq.

Returns:

  • (Boolean)


228
229
230
# File 'lib/ori/list_method.rb', line 228

def eql?(other)
  hash == other.hash
end

- (Object) format(options = {})

Format self into a string. Options:

:color => true|false

Raises:

  • (ArgumentError)


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ori/list_method.rb', line 137

def format(options = {})
  options = options.dup
  o = {}
  o[k = :color] = (v = options.delete(k)).nil?? true : v
  raise ArgumentError, "Unknown option(s): #{options.inspect}" if not options.empty?

  require_valid

  Colorize.colorize *[
    (own?? [[:list_method, :own_marker], OWN_MARKER[0]] : [[:list_method, :not_own_marker], OWN_MARKER[1]]),
    [[:list_method, :obj_module_name], obj_module_name],
    ([[:list_method, :owner_name], "(#{owner_name})"] if not own?),
    [[:list_method, :access], access],
    [[:list_method, :name], method_name],
    ([[:list_method, :visibility], " [#{visibility}]"] if not public?),
    [[:reset]],
  ].compact.flatten(1).reject {|v| v.is_a? Array and not o[:color]}
end

- (Object) fullmatch(re)

Match entire formatted record against RE.



157
158
159
# File 'lib/ori/list_method.rb', line 157

def fullmatch(re)
  format(:color => false).match(re)
end

- (Object) hash

Support Array#uniq.



223
224
225
# File 'lib/ori/list_method.rb', line 223

def hash
  @cache[:hash] ||= qformat.hash
end

- (Boolean) instance?

Returns:

  • (Boolean)


31
32
33
# File 'lib/ori/list_method.rb', line 31

def instance?
  access == "#"
end

- (Object) match(re)

Match method name against RE.



162
163
164
# File 'lib/ori/list_method.rb', line 162

def match(re)
  @method_name.match(re)
end

- (Object) method_object

Fetch method object.



41
42
43
44
45
46
47
48
49
# File 'lib/ori/list_method.rb', line 41

def method_object
  require_valid

  @cache[:method_object] ||= if @inspector.match /instance/
    @obj._ori_instance_method(@method_name)
  else
    @obj._ori_method(@method_name)
  end
end

- (Boolean) module?

Returns:

  • (Boolean)


51
52
53
54
55
56
# File 'lib/ori/list_method.rb', line 51

def module?
  @cache[:is_module] ||= begin
    require_obj
    @obj.is_a? Module
  end
end

- (Object) obj_module



64
65
66
# File 'lib/ori/list_method.rb', line 64

def obj_module
  @cache[:obj_module] ||= obj.is_a?(Module) ? obj : obj.class
end

- (Object) obj_module_name



68
69
70
# File 'lib/ori/list_method.rb', line 68

def obj_module_name
  @cache[:obj_module_name] ||= Tools.get_module_name(obj_module)
end

- (Object) obj_singleton_class

Get, if possible, obj singleton class. Some objects, e.g. Fixnum instances, don't have a singleton class.



78
79
80
81
82
83
84
85
86
# File 'lib/ori/list_method.rb', line 78

def obj_singleton_class
  @cache[:obj_singleton] ||= begin
    class << obj    #:nodoc:
      self
    end
  rescue
    nil
  end
end

- (Boolean) own?

Return true if method is natively owned by obj class.

Returns:

  • (Boolean)


89
90
91
92
93
94
# File 'lib/ori/list_method.rb', line 89

def own?
  @cache[:is_own] ||= begin
    require_valid
    owner == obj_module || owner == obj_singleton_class
  end
end

- (Object) owner



72
73
74
# File 'lib/ori/list_method.rb', line 72

def owner
  @cache[:owner] ||= method_object.owner
end

- (Object) owner_name



96
97
98
# File 'lib/ori/list_method.rb', line 96

def owner_name
  @cache[:owner_name] ||= Tools.get_module_name(owner)
end

- (Boolean) private?

Returns:

  • (Boolean)


100
101
102
# File 'lib/ori/list_method.rb', line 100

def private?
  visibility == :private
end

- (Boolean) protected?

Returns:

  • (Boolean)


104
105
106
# File 'lib/ori/list_method.rb', line 104

def protected?
  visibility == :protected
end

- (Boolean) public?

Returns:

  • (Boolean)


108
109
110
# File 'lib/ori/list_method.rb', line 108

def public?
  visibility == :public
end

- (Object) qformat

Quick format. No options, no hashes, no checks.



167
168
169
170
# File 'lib/ori/list_method.rb', line 167

def qformat
  #"#{owner_name}#{access}#{@method_name} [#{visibility}]"        # Before multi-obj support.
  "#{obj_module_name}#{access}#{@method_name} [#{visibility}]"
end

- (Object) ri_topics



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/ori/list_method.rb', line 172

def ri_topics
  @cache[:ri_topics] ||= begin
    require_valid

    # Build "hierarchy methods". Single record is:
    #
    #   ["Kernel", "#", "dup"]
    hmethods = []

    # Always stuff self in front of the line regardless of if we have method or not.
    hmethods << [obj_module_name, access, method_name]

    ancestors = []
    ancestors += obj_module.ancestors
    ancestors += obj_singleton_class.ancestors if obj_singleton_class     # E.g. when module extends class.

    ancestors.each do |mod|
      mav = Tools.get_methods(mod, :inspector_arg => false, :to_mav => true)
      ##p "mav", mav
      found = mav.select {|method_name,| method_name == self.method_name}
      ##p "found", found
      found.each do |method_name, access|
        hmethods << [Tools.get_module_name(mod), access, method_name]
      end
    end

    # Misdoc hack -- stuff Object#meth lookup if Kernel#meth is present. For methods like Kernel#is_a?.
    if (found = hmethods.find {|mod, access| [mod, access] == ["Kernel", "#"]}) and not hmethods.find {|mod,| mod == "Object"}
      hmethods << ["Object", "#", found.last]
    end

    hmethods.uniq
  end
end

- (Boolean) singleton?

Returns:

  • (Boolean)


112
113
114
# File 'lib/ori/list_method.rb', line 112

def singleton?
  access == "::"
end

- (Boolean) valid?

Returns:

  • (Boolean)


207
208
209
210
211
212
213
# File 'lib/ori/list_method.rb', line 207

def valid?
  [
    @obj_present,
    @method_name,
    @inspector,
  ].all?
end

- (Object) visibility

Return visibility: :public, :protected, :private.



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ori/list_method.rb', line 117

def visibility
  @cache[:visibility] ||= begin
    require_valid

    if @inspector.match /private/
      :private
    elsif @inspector.match /protected/
      :protected
    else
      :public
    end
  end
end