Class: RBI::File

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/rbi/model.rb,
lib/rbi/printer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strictness: nil, comments: [], &block) ⇒ File

Returns a new instance of File.



152
153
154
155
156
157
# File 'lib/rbi/model.rb', line 152

def initialize(strictness: nil, comments: [], &block)
  @root = T.let(Tree.new, Tree)
  @strictness = strictness
  @comments = comments
  block&.call(self)
end

Instance Attribute Details

#commentsObject

Returns the value of attribute comments.



143
144
145
# File 'lib/rbi/model.rb', line 143

def comments
  @comments
end

#rootObject

Returns the value of attribute root.



137
138
139
# File 'lib/rbi/model.rb', line 137

def root
  @root
end

#strictnessObject

Returns the value of attribute strictness.



140
141
142
# File 'lib/rbi/model.rb', line 140

def strictness
  @strictness
end

Instance Method Details

#<<(node) ⇒ Object



160
161
162
# File 'lib/rbi/model.rb', line 160

def <<(node)
  @root << node
end

#accept_printer(v) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rbi/printer.rb', line 90

def accept_printer(v)
  strictness = self.strictness
  if strictness
    v.printl("# typed: #{strictness}")
  end
  unless comments.empty?
    v.printn if strictness
    v.visit_all(comments)
  end

  unless root.empty? && root.comments.empty?
    v.printn if strictness || !comments.empty?
    v.visit(root)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/rbi/model.rb', line 165

def empty?
  @root.empty?
end


107
108
109
110
# File 'lib/rbi/printer.rb', line 107

def print(out: $stdout, indent: 0, print_locs: false)
  p = Printer.new(out: out, indent: indent, print_locs: print_locs)
  p.visit_file(self)
end

#string(indent: 0, print_locs: false) ⇒ Object



113
114
115
116
117
# File 'lib/rbi/printer.rb', line 113

def string(indent: 0, print_locs: false)
  out = StringIO.new
  print(out: out, indent: indent, print_locs: print_locs)
  out.string
end