Class: Robe::Sash::DocFor

Inherits:
Object
  • Object
show all
Defined in:
lib/robe/sash/doc_for.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method) ⇒ DocFor

Returns a new instance of DocFor.



13
14
15
# File 'lib/robe/sash/doc_for.rb', line 13

def initialize(method)
  @method = method
end

Class Method Details

.method_struct(method) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/robe/sash/doc_for.rb', line 36

def self.method_struct(method)
  begin
    info = Pry::Method.new(method)

    if info.dynamically_defined?
      doc = ""
      source = "# This method was defined outside of a source file."
    else
      doc = info.doc
      source = (info.source? ? info.source : "# Not available.")
    end

    OpenStruct.new(docstring: doc, source: source,
                   aliases: info.aliases.map(&:to_sym))
  rescue Pry::CommandError
    message = $!.message =~ /pry-doc/ ? $!.message : ""
    return OpenStruct.new(docstring: message)
  end
end

Instance Method Details

#formatObject



17
18
19
20
21
22
23
# File 'lib/robe/sash/doc_for.rb', line 17

def format
  info = self.class.method_struct(@method)
  {docstring: info.docstring,
   source: info.source,
   aliases: info.aliases,
   visibility: visibility}
end

#visibilityObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/robe/sash/doc_for.rb', line 25

def visibility
  owner, name = @method.owner, @method.name
  if owner.__public_instance_methods__(false).include?(name)
    :public
  elsif owner.__protected_instance_methods__(false).include?(name)
    :protected
  elsif owner.__private_instance_methods__(false).include?(name)
    :private
  end
end