Class: Parlour::RbiGenerator::Options

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/parlour/rbi_generator/options.rb

Overview

A set of immutable formatting options passed to all calls of RbiObject#generate_rbi.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(break_params:, tab_size:, sort_namespaces:) ⇒ void

Creates a new set of formatting options.

Examples:

Create Options with break_params of 4 and tab_size of 2.

Parlour::RbiGenerator::Options.new(break_params: 4, tab_size: 2)

Parameters:

  • break_params (Integer)

    If there are at least this many parameters in a Sorbet sig, then it is broken onto separate lines.

  • tab_size (Integer)

    The number of spaces to use per indent.

  • sort_namespaces (Boolean)

    Whether to sort all items within a namespace alphabetically.



21
22
23
24
25
# File 'lib/parlour/rbi_generator/options.rb', line 21

def initialize(break_params:, tab_size:, sort_namespaces:)
  @break_params = break_params
  @tab_size = tab_size
  @sort_namespaces = sort_namespaces
end

Instance Attribute Details

#break_paramsInteger (readonly)

If there are at least this many parameters in a Sorbet sig, then it is broken onto separate lines.

# With break_params: 5
sig { params(name: String, age: Integer, hobbies: T::Array(String), country: Symbol).void }

# With break_params: 4
sig do
  params(
    name: String,
    age: Integer,
    hobbies: T::Array(String),
    country: Symbol
  ).void
end

Returns:

  • (Integer)


45
46
47
# File 'lib/parlour/rbi_generator/options.rb', line 45

def break_params
  @break_params
end

#sort_namespacesBoolean (readonly)

Whether to sort all items within a namespace alphabetically. Items which are typically grouped together, such as “include” or “extend” calls, will remain grouped together when sorted. If true, items are sorted by their name when the RBI is generated. If false, items are generated in the order they are added to the namespace.

Returns:

  • (Boolean)


60
61
62
# File 'lib/parlour/rbi_generator/options.rb', line 60

def sort_namespaces
  @sort_namespaces
end

#tab_sizeInteger (readonly)

The number of spaces to use per indent.

Returns:

  • (Integer)


50
51
52
# File 'lib/parlour/rbi_generator/options.rb', line 50

def tab_size
  @tab_size
end

Instance Method Details

#indented(level, str) ⇒ String

Returns a string indented to the given indent level, according to the set #tab_size.

Parameters:

  • level (Integer)

    The indent level, as an integer. 0 is totally unindented.

  • str (String)

    The string to indent.

Returns:

  • (String)

    The indented string.



69
70
71
# File 'lib/parlour/rbi_generator/options.rb', line 69

def indented(level, str)
  " " * (level * tab_size) + str
end