Class: Parlour::RbiGenerator::Parameter

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

Overview

Represents a method parameter with a Sorbet type signature.

Constant Summary collapse

PREFIXES =

A mapping of #kind values to the characteristic prefixes each kind has.

T.let({
  normal: '',
  splat: '*',
  double_splat: '**',
  block: '&'
}.freeze, T::Hash[Symbol, String])

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type: nil, default: nil) ⇒ void

Create a new method parameter.

Examples:

Create a simple Integer parameter named num.

Parlour::RbiGenerator::Parameter.new('num', type: 'Integer')

Create a nilable array parameter.

Parlour::RbiGenerator::Parameter.new('array_of_strings_or_symbols', type: 'T.nilable(T::Array(String, Symbol))')

Create a block parameter.

Parlour::RbiGenerator::Parameter.new('&blk', type: 'T.proc.void')

Create a parameter with a default value.

Parlour::RbiGenerator::Parameter.new('name', type: 'String', default: 'Parlour')

Parameters:

  • name (String)

    The name of this parameter. This may start with *, **, or &, or end with :, which will infer the #kind of this parameter. (If it contains none of those, #kind will be :normal.)

  • type (String, nil) (defaults to: nil)

    A Sorbet string of this parameter’s type, such as “String” or “T.untyped”.

  • default (String, nil) (defaults to: nil)

    A string of Ruby code for this parameter’s default value. For example, the default value of an empty string would be represented as “""” (or ‘“”’). The default value of the decimal 3.14 would be “3.14”.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/parlour/rbi_generator/parameter.rb', line 37

def initialize(name, type: nil, default: nil)
  name = T.must(name)
  @name = name

  prefix = /^(\*\*|\*|\&)?/.match(name)&.captures&.first || ''
  @kind = T.must(PREFIXES.rassoc(prefix)).first

  @kind = :keyword if kind == :normal && name.end_with?(':')

  @type = type || 'T.untyped'
  @default = default
end

Instance Attribute Details

#defaultString? (readonly)

A string of Ruby code for this parameter’s default value. For example, the default value of an empty string would be represented as “""” (or ‘“”’). The default value of the decimal 3.14 would be “3.14”.

Returns:

  • (String, nil)


95
96
97
# File 'lib/parlour/rbi_generator/parameter.rb', line 95

def default
  @default
end

#kindSymbol (readonly)

The kind of parameter that this is. This will be one of :normal, :splat, :double_splat, :block or :keyword.

Returns:

  • (Symbol)


101
102
103
# File 'lib/parlour/rbi_generator/parameter.rb', line 101

def kind
  @kind
end

#nameString (readonly)

The name of this parameter, including any prefixes or suffixes such as *.

Returns:

  • (String)


68
69
70
# File 'lib/parlour/rbi_generator/parameter.rb', line 68

def name
  @name
end

#typeString (readonly)

A Sorbet string of this parameter’s type, such as “String” or “T.untyped”.

Returns:

  • (String)


88
89
90
# File 'lib/parlour/rbi_generator/parameter.rb', line 88

def type
  @type
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if this instance is equal to another method.

Parameters:

Returns:

  • (Boolean)


56
57
58
59
60
61
62
# File 'lib/parlour/rbi_generator/parameter.rb', line 56

def ==(other)
  Parameter === other &&
    name    == other.name &&
    kind    == other.kind &&
    type    == other.type &&
    default == other.default
end

#describe_in_methodObject



139
140
141
142
143
144
145
146
147
148
# File 'lib/parlour/rbi_generator/parameter.rb', line 139

def describe_in_method
  t = type
  t = t.is_a?(String) ? t : t.describe

  if default
    "#{name}: #{t} = #{default}"
  else
    "#{name}: #{t}"
  end
end

#generalize_from_rbi!Object



134
135
136
# File 'lib/parlour/rbi_generator/parameter.rb', line 134

def generalize_from_rbi!
  @type = TypeParser.parse_single_type(@type) if String === @type
end

#name_without_kindString

The name of this parameter, stripped of any prefixes or suffixes. For example, *rest would become rest, or foo: would become foo.

Returns:

  • (String)


75
76
77
78
79
80
81
82
# File 'lib/parlour/rbi_generator/parameter.rb', line 75

def name_without_kind
  return T.must(name[0..-2]) if kind == :keyword

  prefix_match = /^(\*\*|\*|\&)?[a-zA-Z_]/.match(name)
  raise 'unknown prefix' unless prefix_match
  prefix = prefix_match.captures.first || ''
  T.must(name[prefix.length..-1])
end

#to_def_paramString

A string of how this parameter should be defined in a method definition.

Returns:

  • (String)


107
108
109
110
111
112
113
114
115
# File 'lib/parlour/rbi_generator/parameter.rb', line 107

def to_def_param
  if default.nil?
    "#{name}"
  elsif !default.nil? && kind == :keyword
    "#{name} #{default}"
  else
    "#{name} = #{default}"
  end
end

#to_sig_paramString

A string of how this parameter should be defined in a Sorbet sig.

Returns:

  • (String)


121
122
123
# File 'lib/parlour/rbi_generator/parameter.rb', line 121

def to_sig_param
  "#{name_without_kind}: #{String === @type ? @type : @type.generate_rbi}"
end