Class: Parlour::RbiGenerator::Parameter
- Inherits:
-
Object
- Object
- Parlour::RbiGenerator::Parameter
- 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.
{ normal: '', splat: '*', double_splat: '**', block: '&' }.freeze
Instance Attribute Summary collapse
-
#default ⇒ String?
readonly
A string of Ruby code for this parameter’s default value.
-
#kind ⇒ Symbol
readonly
The kind of parameter that this is.
-
#name ⇒ String
readonly
The name of this parameter, including any prefixes or suffixes such as *.
-
#type ⇒ String?
readonly
A Sorbet string of this parameter’s type, such as “String” or “T.untyped”.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Returns true if this instance is equal to another method.
-
#initialize(name, type: nil, default: nil) ⇒ void
constructor
Create a new method parameter.
-
#name_without_kind ⇒ String
The name of this parameter, stripped of any prefixes or suffixes.
-
#to_def_param ⇒ String
A string of how this parameter should be defined in a method definition.
-
#to_sig_param ⇒ String
A string of how this parameter should be defined in a Sorbet
sig
.
Constructor Details
#initialize(name, type: nil, default: nil) ⇒ void
Create a new method parameter.
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/parlour/rbi_generator/parameter.rb', line 36 def initialize(name, type: nil, default: nil) name = T.must(name) @name = name prefix = /^(\*\*|\*|\&)?/.match(name)&.captures&.first || '' @kind = PREFIXES.rassoc(prefix).first @kind = :keyword if kind == :normal && name.end_with?(':') @type = type @default = default end |
Instance Attribute Details
#default ⇒ String? (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”.
94 95 96 |
# File 'lib/parlour/rbi_generator/parameter.rb', line 94 def default @default end |
#kind ⇒ Symbol (readonly)
The kind of parameter that this is. This will be one of :normal
, :splat
, :double_splat
, :block
or :keyword
.
100 101 102 |
# File 'lib/parlour/rbi_generator/parameter.rb', line 100 def kind @kind end |
#name ⇒ String (readonly)
The name of this parameter, including any prefixes or suffixes such as *.
67 68 69 |
# File 'lib/parlour/rbi_generator/parameter.rb', line 67 def name @name end |
#type ⇒ String? (readonly)
A Sorbet string of this parameter’s type, such as “String” or “T.untyped”.
87 88 89 |
# File 'lib/parlour/rbi_generator/parameter.rb', line 87 def type @type end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true if this instance is equal to another method.
55 56 57 58 59 60 61 |
# File 'lib/parlour/rbi_generator/parameter.rb', line 55 def ==(other) Parameter === other && name == other.name && kind == other.kind && type == other.type && default == other.default end |
#name_without_kind ⇒ String
The name of this parameter, stripped of any prefixes or suffixes. For example, *rest would become rest
, or foo:
would become foo
.
74 75 76 77 78 79 80 81 |
# File 'lib/parlour/rbi_generator/parameter.rb', line 74 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_param ⇒ String
A string of how this parameter should be defined in a method definition.
106 107 108 109 110 111 112 113 114 |
# File 'lib/parlour/rbi_generator/parameter.rb', line 106 def to_def_param if default.nil? "#{name}" elsif !default.nil? && kind == :keyword "#{name} #{default}" else "#{name} = #{default}" end end |
#to_sig_param ⇒ String
A string of how this parameter should be defined in a Sorbet sig
.
120 121 122 |
# File 'lib/parlour/rbi_generator/parameter.rb', line 120 def to_sig_param "#{name_without_kind}: #{type || 'T.untyped'}" end |