Class: Peeky::Renderer::MethodSignatureRender

Inherits:
Object
  • Object
show all
Defined in:
lib/peeky/renderer/method_signature_render.rb

Overview

Render: Method Signature in compact format

Example output (:default):

def simple(first_param)
end

def complex(aaa, bbb = nil, *ccc, ddd:, eee: nil, **fff, &ggg)
end

Example output (:compact):

def simple(first_param);                                           end
def complex(aaa, bbb = nil, *ccc, ddd:, eee: nil, **fff, &ggg);    end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_signature, **_opts) ⇒ MethodSignatureRender

Returns a new instance of MethodSignatureRender.


37
38
39
40
41
# File 'lib/peeky/renderer/method_signature_render.rb', line 37

def initialize(method_signature, **_opts)
  @method_signature = method_signature
  @indent = ''
  @style = :compact
end

Instance Attribute Details

#indentObject

Indentation prefix as a string.

Defaults to ''

If you were writing a class into a file with an existing module, you may set the indent to ' ' if you wanted this render to indent by two spaces


25
26
27
# File 'lib/peeky/renderer/method_signature_render.rb', line 25

def indent
  @indent
end

#method_signatureObject (readonly)

Method signature stores a MethodInfo object


35
36
37
# File 'lib/peeky/renderer/method_signature_render.rb', line 35

def method_signature
  @method_signature
end

#styleSymbol

Style of method rendering [:default, :compact]

default

will render def method_name and end on separate lines

compact

will render def method_name and end on same line

Returns:

  • (Symbol)

    style


32
33
34
# File 'lib/peeky/renderer/method_signature_render.rb', line 32

def style
  @style
end

Instance Method Details

#renderObject

Render the method signature in the selected style


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/peeky/renderer/method_signature_render.rb', line 44

def render
  output = []
  if @style == :compact
    signature = "#{render_signature};"
    output.push "#{signature.ljust(80)}#{render_end}"
  end
  if @style == :default
    output.push render_signature
    output.push render_end
  end
  output.join("\n")
end

#render_endObject


65
66
67
# File 'lib/peeky/renderer/method_signature_render.rb', line 65

def render_end
  "#{@indent}end"
end

#render_signatureObject


57
58
59
60
61
62
63
# File 'lib/peeky/renderer/method_signature_render.rb', line 57

def render_signature
  name = method_signature.name

  formatted_parameters = method_signature.parameters.map(&:signature_format).join(', ')
  params = formatted_parameters.length.zero? ? '' : "(#{formatted_parameters})"
  "#{@indent}def #{name}#{params}"
end