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, #final, #implementation, #overridable, #override, #parameters, #return_type, #type_parameters

Attributes inherited from RbiObject

#generator

Attributes inherited from TypedObject

#comments, #generated_by, #name

Instance Method Summary collapse

Methods inherited from Method

#generate_rbi, #merge_into_self, #mergeable?

Methods inherited from RbiObject

#generate_rbi, #merge_into_self, #mergeable?

Methods inherited from TypedObject

#add_comment, #describe, #describe_tree

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, Types::Type)

    This attribute’s type.

  • class_attribute (Boolean) (defaults to: false)

    Whether this attribute belongs to the singleton class.

  • block

    A block which the new instance yields itself to.



28
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 28

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) }

  @type = type
  @kind = kind
  @class_attribute = class_attribute
  case kind
  when :accessor, :reader
    super(generator, name, [], type, &T.cast(block, T.nilable(T.proc.params(x: Method).void)))
  when :writer
    super(generator, name, [
      Parameter.new(name, type: type)
    ], type, &T.cast(block, T.nilable(T.proc.params(x: Method).void)))
  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

#typeObject (readonly)

The type of this attribute.



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

def type
  @type
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if this instance is equal to another attribute.

Parameters:

Returns:

  • (Boolean)


68
69
70
71
72
73
74
# File 'lib/parlour/rbi_generator/attribute.rb', line 68

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

#describe_attrsObject



82
83
84
85
86
87
88
# File 'lib/parlour/rbi_generator/attribute.rb', line 82

def describe_attrs
  [
    :kind,
    {type: type}, # avoid quotes
    :class_attribute
  ]
end

#generalize_from_rbi!Object



77
78
79
# File 'lib/parlour/rbi_generator/attribute.rb', line 77

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