Class: Parlour::RbsGenerator::Attribute

Inherits:
Method show all
Extended by:
T::Sig
Defined in:
lib/parlour/rbs_generator/attribute.rb

Overview

Represents an attribute reader, writer or accessor.

Instance Attribute Summary collapse

Attributes inherited from Method

#class_method, #signatures

Attributes inherited from RbsObject

#generator

Attributes inherited from TypedObject

#comments, #generated_by, #name

Instance Method Summary collapse

Methods inherited from Method

#merge_into_self, #mergeable?

Methods inherited from RbsObject

#merge_into_self, #mergeable?

Methods inherited from TypedObject

#add_comment, #describe, #describe_tree

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 (RbsGenerator)

    The current RbsGenerator.

  • 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.

  • block

    A block which the new instance yields itself to.



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

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

#kindSymbol (readonly)

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

Returns:

  • (Symbol)


45
46
47
# File 'lib/parlour/rbs_generator/attribute.rb', line 45

def kind
  @kind
end

#typeObject (readonly)

The type of this attribute.



49
50
51
# File 'lib/parlour/rbs_generator/attribute.rb', line 49

def type
  @type
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if this instance is equal to another attribute.

Parameters:

Returns:

  • (Boolean)


75
76
77
78
79
# File 'lib/parlour/rbs_generator/attribute.rb', line 75

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

#describe_attrsObject



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

def describe_attrs
  [
    :kind,
    :class_attribute
  ]
end

#generate_rbs(indent_level, options) ⇒ Array<String>

Generates the RBS lines for this arbstrary code.

Parameters:

  • indent_level (Integer)

    The indentation level to generate the lines at.

  • options (Options)

    The formatting options to use.

Returns:

  • (Array<String>)

    The RBS lines, formatted as specified.



62
63
64
65
66
67
# File 'lib/parlour/rbs_generator/attribute.rb', line 62

def generate_rbs(indent_level, options)
  generate_comments(indent_level, options) + [options.indented(
    indent_level,
    "attr_#{kind} #{name}: #{String === @type ? @type : @type.generate_rbs}"
  )]
end