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, #type_parameters

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, class_attribute: false, &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”.

  • class_attribute (Boolean) (defaults to: false)

    Whether this attribute belongs to the singleton class.

  • block

    A block which the new instance yields itself to.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/parlour/rbi_generator/attribute.rb', line 29

def initialize(generator, name, kind, type, class_attribute: false, &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
  @class_attribute = class_attribute
  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

#class_attributeObject (readonly)

Whether this attribute belongs to the singleton class.



56
57
58
# File 'lib/parlour/rbi_generator/attribute.rb', line 56

def class_attribute
  @class_attribute
end

#kindSymbol (readonly)

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

Returns:

  • (Symbol)


52
53
54
# File 'lib/parlour/rbi_generator/attribute.rb', line 52

def kind
  @kind
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if this instance is equal to another attribute.

Parameters:

Returns:

  • (Boolean)


64
65
66
67
68
69
70
# File 'lib/parlour/rbi_generator/attribute.rb', line 64

def ==(other)
  T.must(
    super(other) && Attribute === other &&
      kind            == other.kind &&
      class_attribute == other.class_attribute
  )
end