Class: Parlour::RbiGenerator::Attribute

Inherits:
Method show all
Defined in:
lib/parlour/rbi_generator/attribute.rb

Overview

Represents an attribute reader, writer or accessor.

Instance Attribute Summary collapse

Attributes inherited from Method

#abstract, #class_method, #implementation, #overridable, #override, #parameters, #return_type

Attributes inherited from RbiObject

#comments, #generated_by, #generator, #name

Instance Method Summary collapse

Methods inherited from Method

#==, #describe, #generate_rbi, #merge_into_self, #mergeable?

Methods inherited from RbiObject

#add_comment, #describe, #generate_rbi, #merge_into_self, #mergeable?

Constructor Details

#initialize(generator, name, kind, type, &block) ⇒ void

Note:

You should use Namespace#create_attribute rather than this directly.

Creates a new attribute.

Parameters:

  • generator (RbiGenerator)

    The current RbiGenerator.

  • name (String)

    The name of this attribute.

  • kind (Symbol)

    The kind of attribute this is; one of :writer, :reader or :accessor.

  • type (String)

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

  • block

    A block which the new instance yields itself to.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/parlour/rbi_generator/attribute.rb', line 26

def initialize(generator, name, kind, type, &block)
  # According to this source: 
  #   https://github.com/sorbet/sorbet/blob/2275752e51604acfb79b30a0a96debc996c089d9/test/testdata/dsl/attr_multi.rb
  # attr_accessor and attr_reader should have: sig { returns(X) }
  # attr_writer :foo should have: sig { params(foo: X).returns(X) }

  @kind = kind
  case kind
  when :accessor, :reader
    super(generator, name, [], type, &block)
  when :writer
    super(generator, name, [
      Parameter.new(name, type: type)
    ], type, &block)
  else
    raise 'unknown kind'
  end
end

Instance Attribute Details

#kindSymbol (readonly)

The kind of attribute this is; one of :writer, :reader, or :accessor.

Returns:

  • (Symbol)


48
49
50
# File 'lib/parlour/rbi_generator/attribute.rb', line 48

def kind
  @kind
end