Class: Parlour::TypedObject

Inherits:
Object
  • Object
show all
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.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#commentsArray<String> (readonly)

An array of comments which will be placed above the object in the RBS file.

Returns:

  • (Array<String>)


32
33
34
# File 'lib/parlour/typed_object.rb', line 32

def comments
  @comments
end

#generated_byPlugin? (readonly)

The Plugin which was controlling the generator when this object was created.

Returns:



21
22
23
# File 'lib/parlour/typed_object.rb', line 21

def generated_by
  @generated_by
end

#nameString (readonly)

The name of this object.

Returns:

  • (String)


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.

Examples:

Creating a module with a comment.

namespace.create_module('M') do |m|
  m.add_comment('This is a module')
end

Creating a class with a multi-line comment.

namespace.create_class('C') do |c|
  c.add_comment(['This is a multi-line comment!', 'It can be as long as you want!'])
end

Parameters:

  • comment (String, Array<String>)

    The new comment(s).



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

#describeString 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.

Returns:

  • (String)


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.

Returns:

  • (String)


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