Class: Parlour::RbiGenerator::StructProp

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/parlour/rbi_generator/struct_prop.rb

Overview

Represents a T::Struct property.

Constant Summary collapse

EXTRA_PROPERTIES =

The optional properties available on instances of this class.

T.let(%i{
  optional enum dont_store foreign default factory immutable array override redaction
}, T::Array[Symbol])

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, optional: nil, enum: nil, dont_store: nil, foreign: nil, default: nil, factory: nil, immutable: nil, array: nil, override: nil, redaction: nil) ⇒ void

Create a new struct property.

For documentation on all optional properties, please refer to the documentation for T::Struct within the sorbet-runtime gem: github.com/sorbet/sorbet/blob/master/gems/sorbet-runtime/lib/types/props/_props.rb#L31-L106

Parameters:

  • name (String)

    The name of this property.

  • type (String)

    This property’s type.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/parlour/rbi_generator/struct_prop.rb', line 33

def initialize(name, type, optional: nil, enum: nil, dont_store: nil,
  foreign: nil, default: nil, factory: nil, immutable: nil, array: nil,
  override: nil, redaction: nil)
  
  @name = name
  @type = type
  @optional = optional
  @enum = enum
  @dont_store = dont_store
  @foreign = foreign
  @default = default
  @factory = factory
  @immutable = immutable
  @array = array
  @override = override
  @redaction = redaction
end

Instance Attribute Details

#arrayObject (readonly)

Returns the value of attribute array.



106
107
108
# File 'lib/parlour/rbi_generator/struct_prop.rb', line 106

def array
  @array
end

#defaultObject (readonly)

Returns the value of attribute default.



97
98
99
# File 'lib/parlour/rbi_generator/struct_prop.rb', line 97

def default
  @default
end

#dont_storeObject (readonly)

Returns the value of attribute dont_store.



91
92
93
# File 'lib/parlour/rbi_generator/struct_prop.rb', line 91

def dont_store
  @dont_store
end

#enumObject (readonly)

Returns the value of attribute enum.



88
89
90
# File 'lib/parlour/rbi_generator/struct_prop.rb', line 88

def enum
  @enum
end

#factoryObject (readonly)

Returns the value of attribute factory.



100
101
102
# File 'lib/parlour/rbi_generator/struct_prop.rb', line 100

def factory
  @factory
end

#foreignObject (readonly)

Returns the value of attribute foreign.



94
95
96
# File 'lib/parlour/rbi_generator/struct_prop.rb', line 94

def foreign
  @foreign
end

#immutableObject (readonly)

Returns the value of attribute immutable.



103
104
105
# File 'lib/parlour/rbi_generator/struct_prop.rb', line 103

def immutable
  @immutable
end

#nameString (readonly)

The name of this parameter, including any prefixes or suffixes such as *.

Returns:

  • (String)


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

def name
  @name
end

#optionalObject (readonly)

Returns the value of attribute optional.



85
86
87
# File 'lib/parlour/rbi_generator/struct_prop.rb', line 85

def optional
  @optional
end

#overrideObject (readonly)

Returns the value of attribute override.



109
110
111
# File 'lib/parlour/rbi_generator/struct_prop.rb', line 109

def override
  @override
end

#redactionObject (readonly)

Returns the value of attribute redaction.



112
113
114
# File 'lib/parlour/rbi_generator/struct_prop.rb', line 112

def redaction
  @redaction
end

#typeTypes::TypeLike? (readonly)

This parameter’s type.

Returns:



82
83
84
# File 'lib/parlour/rbi_generator/struct_prop.rb', line 82

def type
  @type
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if this instance is equal to another instance.

Parameters:

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/parlour/rbi_generator/struct_prop.rb', line 57

def ==(other)
  StructProp === other &&
    name       == other.name &&
    type       == other.type &&
    optional   == other.optional &&
    enum       == other.enum &&
    dont_store == other.dont_store &&
    foreign    == other.foreign &&
    default    == other.default &&
    factory    == other.factory &&
    immutable  == other.immutable &&
    array      == other.array &&
    override   == other.override &&
    redaction  == other.redaction
end

#generalize_from_rbi!Object



134
135
136
# File 'lib/parlour/rbi_generator/struct_prop.rb', line 134

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

#to_prop_callString

Returns the prop call required to create this property.

Returns:

  • (String)


122
123
124
125
126
127
128
129
130
131
# File 'lib/parlour/rbi_generator/struct_prop.rb', line 122

def to_prop_call
  call = "prop :#{name}, #{String === @type ? @type : @type.generate_rbi}"

  EXTRA_PROPERTIES.each do |extra_property|
    value = send extra_property
    call += ", #{extra_property}: #{value}" unless value.nil?
  end

  call
end