Class: Parlour::RbiGenerator::Attribute
- Inherits:
-
Method
- Object
- TypedObject
- RbiObject
- Method
- Parlour::RbiGenerator::Attribute
- Defined in:
- lib/parlour/rbi_generator/attribute.rb
Overview
Represents an attribute reader, writer or accessor.
Instance Attribute Summary collapse
-
#class_attribute ⇒ Object
readonly
Whether this attribute belongs to the singleton class.
-
#kind ⇒ Symbol
readonly
The kind of attribute this is; one of
:writer
,:reader
, or:accessor
. -
#type ⇒ Object
readonly
The type of this attribute.
Attributes inherited from Method
#abstract, #class_method, #final, #implementation, #overridable, #override, #parameters, #return_type, #type_parameters
Attributes inherited from RbiObject
Attributes inherited from TypedObject
#comments, #generated_by, #name
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Returns true if this instance is equal to another attribute.
- #describe_attrs ⇒ Object
- #generalize_from_rbi! ⇒ Object
-
#initialize(generator, name, kind, type, class_attribute: false, &block) ⇒ void
constructor
Creates a new attribute.
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
You should use Namespace#create_attribute rather than this directly.
Creates a new attribute.
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_attribute ⇒ Object (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 |
#kind ⇒ Symbol (readonly)
The kind of attribute this is; one of :writer
, :reader
, or :accessor
.
52 53 54 |
# File 'lib/parlour/rbi_generator/attribute.rb', line 52 def kind @kind end |
#type ⇒ Object (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.
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_attrs ⇒ Object
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 |