Class: Qdocs::Base::Method

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

Direct Known Subclasses

ActiveRecord::Method

Instance Method Summary collapse

Methods included from Helpers

#find_constant, #own_methods, #params_to_hash, #render_response, #source_location_to_str

Constructor Details

#initialize(original_input) ⇒ Method

Returns a new instance of Method.



126
127
128
# File 'lib/qdocs.rb', line 126

def initialize(original_input)
  @original_input = original_input
end

Instance Method Details

#index(const, pattern) {|constant| ... } ⇒ Object

Yields:

  • (constant)


130
131
132
133
134
135
136
137
138
139
140
# File 'lib/qdocs.rb', line 130

def index(const, pattern)
  constant = find_constant const

  yield constant if block_given?

  render_response(constant, :methods, {
    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) {|constant| ... } ⇒ Object

Yields:

  • (constant)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/qdocs.rb', line 142

def show(const, meth, type)
  constant = find_constant(const)

  yield constant if block_given?

  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}. Did you mean #{constant}/#{meth}/ ?"
      end
    when ::Method, UnboundMethod
      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
  sup = method.super_method

  render_response(constant, method_method, {
    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: sup ? Handler::Method.new(@original_input).show(sup.owner, sup, type) : nil,
  })
end