Class: ORI::ListMethod
Overview
Our method representation suitable for listing.
Constant Summary collapse
- OWN_MARKER =
:nodoc:
["~", " "]
Instance Attribute Summary collapse
-
#inspector ⇒ Object
Returns the value of attribute inspector.
-
#method_name ⇒ Object
Returns the value of attribute method_name.
-
#obj ⇒ Object
Object.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Support
Enumerable#sort
. -
#access ⇒ Object
Return method access substring: “::” or “#”.
-
#eql?(other) ⇒ Boolean
Support
Array#uniq
. -
#format(options = {}) ⇒ Object
Format self into a string.
-
#fullmatch(re) ⇒ Object
Match entire formatted record against RE.
-
#hash ⇒ Object
Support
Array#uniq
. -
#initialize(attrs = {}) ⇒ ListMethod
constructor
A new instance of ListMethod.
- #instance? ⇒ Boolean
-
#match(re) ⇒ Object
Match method name against RE.
-
#method_object ⇒ Object
Fetch method object.
- #module? ⇒ Boolean
- #obj_module ⇒ Object
- #obj_module_name ⇒ Object
-
#obj_singleton_class ⇒ Object
Get, if possible,
obj
singleton class. -
#own? ⇒ Boolean
Return
true
if method is natively owned byobj
class. - #owner ⇒ Object
- #owner_name ⇒ Object
- #private? ⇒ Boolean
- #protected? ⇒ Boolean
- #public? ⇒ Boolean
-
#qformat ⇒ Object
Quick format.
- #ri_topics ⇒ Object
- #singleton? ⇒ Boolean
- #valid? ⇒ Boolean
-
#visibility ⇒ Object
Return visibility:
:public
,:protected
,:private
.
Constructor Details
#initialize(attrs = {}) ⇒ ListMethod
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
#inspector ⇒ Object
Returns the value of attribute inspector.
10 11 12 |
# File 'lib/ori/list_method.rb', line 10 def inspector @inspector end |
#method_name ⇒ Object
Returns the value of attribute method_name.
9 10 11 |
# File 'lib/ori/list_method.rb', line 9 def method_name @method_name end |
#obj ⇒ Object
Object. Can be anything, including nil.
7 8 9 |
# File 'lib/ori/list_method.rb', line 7 def obj @obj end |
Instance Method Details
#<=>(other) ⇒ Object
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 |
#access ⇒ Object
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 |
#eql?(other) ⇒ Boolean
Support Array#uniq
.
228 229 230 |
# File 'lib/ori/list_method.rb', line 228 def eql?(other) hash == other.hash end |
#format(options = {}) ⇒ Object
Format self into a string. Options:
:color => true|false
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( = {}) = .dup o = {} o[k = :color] = (v = .delete(k)).nil?? true : v raise ArgumentError, "Unknown option(s): #{.inspect}" if not .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 |
#fullmatch(re) ⇒ Object
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 |
#hash ⇒ Object
Support Array#uniq
.
223 224 225 |
# File 'lib/ori/list_method.rb', line 223 def hash @cache[:hash] ||= qformat.hash end |
#instance? ⇒ Boolean
31 32 33 |
# File 'lib/ori/list_method.rb', line 31 def instance? access == "#" end |
#match(re) ⇒ Object
Match method name against RE.
162 163 164 |
# File 'lib/ori/list_method.rb', line 162 def match(re) @method_name.match(re) end |
#method_object ⇒ 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 |
#module? ⇒ 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 |
#obj_module ⇒ Object
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 |
#obj_module_name ⇒ Object
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 |
#obj_singleton_class ⇒ Object
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 |
#own? ⇒ Boolean
Return true
if method is natively owned by obj
class.
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 |
#owner ⇒ Object
72 73 74 |
# File 'lib/ori/list_method.rb', line 72 def owner @cache[:owner] ||= method_object.owner end |
#owner_name ⇒ Object
96 97 98 |
# File 'lib/ori/list_method.rb', line 96 def owner_name @cache[:owner_name] ||= Tools.get_module_name(owner) end |
#private? ⇒ Boolean
100 101 102 |
# File 'lib/ori/list_method.rb', line 100 def private? visibility == :private end |
#protected? ⇒ Boolean
104 105 106 |
# File 'lib/ori/list_method.rb', line 104 def protected? visibility == :protected end |
#public? ⇒ Boolean
108 109 110 |
# File 'lib/ori/list_method.rb', line 108 def public? visibility == :public end |
#qformat ⇒ Object
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 |
#ri_topics ⇒ Object
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 |
#singleton? ⇒ Boolean
112 113 114 |
# File 'lib/ori/list_method.rb', line 112 def singleton? access == "::" end |
#valid? ⇒ 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 |
#visibility ⇒ Object
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 |