Class: Qdocs::Method

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/qdocs.rb

Instance Method Summary collapse

Methods included from Helpers

#find_constant, #own_methods, #params_to_hash, #source_location_to_str

Instance Method Details

#index(const, pattern) ⇒ Object



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

def index(const, pattern)
  constant = find_constant const
  {
    constant: constant,
    singleton_methods: own_methods(constant.methods.grep(pattern)).sort,
    instance_methods: own_methods(constant.instance_methods.grep(pattern)).sort,
  }
end

#show(const, meth, type) ⇒ Object



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
120
121
122
# File 'lib/qdocs.rb', line 75

def show(const, meth, type)
  constant = begin
               find_constant(const)
             rescue UnknownClassError
               abort "Unknown class #{const.inspect}"
             end
  method = case meth
           when Symbol, String
             method_method = case type
                             when :instance
                               :instance_method
                             when :singleton, :class
                               :method
                             else
                               raise UnknownMethodTypeError, "Unknown method type #{type}"
                             end

             begin
               constant.send method_method, meth
             rescue NameError
               raise UnknownMethodError, "No method #{meth.inspect} for #{constant.inspect}. Did you mean #{constant.inspect}/#{meth}/ ?"
             end
           when Method
             meth
           else
             raise InvalidArgumentError, "#{meth.inspect} must be of type Symbol, String, or Method"
           end

  parameters = params_to_hash(method.parameters)
  src = method.source rescue nil
  source = if src
             lines = src.lines
             first_line = lines.first
             indent_amount = first_line.length - first_line.sub(/^\s*/, '').length
             lines.map { |l| l[indent_amount..-1] }.join
           end

  {
    defined_at: source_location_to_str(method.source_location),
    source: source,
    arity: method.arity,
    parameters: parameters,
    comment: (method.comment.strip rescue nil),
    name: method.name,
    belongs_to: method.owner,
    super_method: method.super_method,
  }
end