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.



85
86
87
# File 'lib/qdocs.rb', line 85

def initialize(original_input)
  @original_input = original_input
end

Instance Method Details

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

Yields:

  • (constant)


89
90
91
92
93
94
95
96
97
98
99
# File 'lib/qdocs.rb', line 89

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)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/qdocs.rb', line 101

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
      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.show(sup.owner, sup, type) : nil,
  })
end