Class: Parlour::TypedObject
- Inherits:
-
Object
- Object
- Parlour::TypedObject
- Extended by:
- T::Helpers, T::Sig
- Defined in:
- lib/parlour/typed_object.rb
Overview
A generic superclass of all objects which form part of type definitions in, specific formats, such as RbiObject and RbsObject.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#comments ⇒ Array<String>
readonly
An array of comments which will be placed above the object in the RBS file.
-
#generated_by ⇒ Plugin?
readonly
The Plugin which was controlling the generator when this object was created.
-
#name ⇒ String
readonly
The name of this object.
Instance Method Summary collapse
-
#add_comment(comment) ⇒ void
(also: #add_comments)
Adds one or more comments to this RBS object.
-
#describe ⇒ String
(also: #inspect, #to_s)
Returns a human-readable brief string description of this object.
-
#describe_tree(tree: nil) ⇒ String
Returns a human-readable multi-line string description of this object and its children recursively.
-
#initialize(name) ⇒ TypedObject
constructor
Create a new typed object.
Constructor Details
#initialize(name) ⇒ TypedObject
Create a new typed object.
12 13 14 15 |
# File 'lib/parlour/typed_object.rb', line 12 def initialize(name) @name = name @comments = [] end |
Instance Attribute Details
#comments ⇒ Array<String> (readonly)
An array of comments which will be placed above the object in the RBS file.
32 33 34 |
# File 'lib/parlour/typed_object.rb', line 32 def comments @comments end |
#generated_by ⇒ Plugin? (readonly)
The Plugin which was controlling the generator when this object was created.
21 22 23 |
# File 'lib/parlour/typed_object.rb', line 21 def generated_by @generated_by end |
#name ⇒ String (readonly)
The name of this object.
26 27 28 |
# File 'lib/parlour/typed_object.rb', line 26 def name @name end |
Instance Method Details
#add_comment(comment) ⇒ void Also known as: add_comments
This method returns an undefined value.
Adds one or more comments to this RBS object. Comments always go above the definition for this object, not in the definition’s body.
50 51 52 53 54 55 56 |
# File 'lib/parlour/typed_object.rb', line 50 def add_comment(comment) if comment.is_a?(String) comments << comment elsif comment.is_a?(Array) comments.concat(comment) end end |
#describe ⇒ String Also known as: inspect, to_s
Returns a human-readable brief string description of this object. This is displayed during manual conflict resolution with the parlour
CLI.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/parlour/typed_object.rb', line 65 def describe if is_a?(RbiGenerator::RbiObject) type_system = 'RBI' elsif is_a?(RbsGenerator::RbsObject) type_system = 'RBS' else raise 'unknown type system' end attr_strings = describe_attrs.map do |a| case a when Symbol key = a value = send(a) case value when Array, Hash value = value.length next nil if value == 0 when String value = value.inspect when Parlour::Types::Type value = value.describe when true next key when false next nil end when Hash raise 'describe_attrs Hash must have one key' unless a.length == 1 key = a.keys[0] value = a.values[0] end "#{key}=#{value}" end.compact class_name = T.must(self.class.name).split('::').last if attr_strings.empty? "<#{type_system}:#{class_name}:#{name}>" else "<#{type_system}:#{class_name}:#{name} #{attr_strings.join(" ")}>" end end |
#describe_tree(tree: nil) ⇒ String
Returns a human-readable multi-line string description of this object and its children recursively.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/parlour/typed_object.rb', line 116 def describe_tree(tree: nil) if tree.nil? tree = Debugging::Tree.new result = "#{describe}\n" else result = "" end if is_a?(RbiGenerator::Namespace) || is_a?(RbsGenerator::Namespace) children.each do |child| result += "#{tree.begin(child.describe)}\n" result += child.describe_tree(tree: tree) tree.indent!(-1) end else "#{describe}\n" end result end |