Class: Parlour::RbsGenerator::Attribute
- Inherits:
-
Method
- Object
- TypedObject
- RbsObject
- Method
- Parlour::RbsGenerator::Attribute
- Extended by:
- T::Sig
- Defined in:
- lib/parlour/rbs_generator/attribute.rb
Overview
Represents an attribute reader, writer or accessor.
Instance Attribute Summary collapse
-
#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
Attributes inherited from RbsObject
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
-
#generate_rbs(indent_level, options) ⇒ Array<String>
Generates the RBS lines for this arbstrary code.
-
#initialize(generator, name, kind, type, &block) ⇒ void
constructor
Creates a new attribute.
Methods inherited from Method
Methods inherited from RbsObject
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.
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
#kind ⇒ Symbol (readonly)
The kind of attribute this is; one of :writer
, :reader
, or :accessor
.
45 46 47 |
# File 'lib/parlour/rbs_generator/attribute.rb', line 45 def kind @kind end |
#type ⇒ Object (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.
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_attrs ⇒ Object
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.
62 63 64 65 66 67 |
# File 'lib/parlour/rbs_generator/attribute.rb', line 62 def generate_rbs(indent_level, ) generate_comments(indent_level, ) + [.indented( indent_level, "attr_#{kind} #{name}: #{String === @type ? @type : @type.generate_rbs}" )] end |