class Parlour::RbiGenerator::Attribute

Represents an attribute reader, writer or accessor.

Attributes

class_attribute[R]

Whether this attribute belongs to the singleton class.

kind[R]

The kind of attribute this is; one of :writer, :reader, or :accessor. @return [Symbol]

type[R]

The type of this attribute.

Public Class Methods

new(generator, name, kind, type, class_attribute: false, &block) click to toggle source

Creates a new attribute. @note You should use {Namespace#create_attribute} rather than this directly.

@param generator [RbiGenerator] The current RbiGenerator. @param name [String] The name of this attribute. @param kind [Symbol] The kind of attribute this is; one of :writer, :reader or

:accessor.

@param type [String, Types::Type] This attribute's type. @param class_attribute [Boolean] Whether this attribute belongs to the

singleton class.

@param block A block which the new instance yields itself to. @return [void]

Calls superclass method
# 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, &block)
  when :writer
    super(generator, name, [
      Parameter.new(name, type: type)
    ], type, &block)
  else
    raise 'unknown kind'
  end
end

Public Instance Methods

==(other) click to toggle source

Returns true if this instance is equal to another attribute.

@param other [Object] The other instance. If this is not a {Attribute}

(or a subclass of it), this will always return false.

@return [Boolean]

Calls superclass method
# 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
generalize_from_rbi!() click to toggle source
# File lib/parlour/rbi_generator/attribute.rb, line 77
def generalize_from_rbi!
  @type = TypeParser.parse_single_type(@type) if String === @type
end

Private Instance Methods

generate_definition(indent_level, options) click to toggle source

Generates the RBI lines for this method.

@param indent_level [Integer] The indentation level to generate the lines at. @param options [Options] The formatting options to use. @return [Array<String>] The RBI lines, formatted as specified.

# File lib/parlour/rbi_generator/attribute.rb, line 94
def generate_definition(indent_level, options)
  [options.indented(indent_level, "attr_#{kind} :#{name}")]
end