Class: SyntaxTree::EndContent

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

EndContent represents the use of __END__ syntax, which allows individual scripts to keep content after the main ruby code that can be read through the DATA constant.

puts DATA.read

__END__

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Constructor Details

#initialize(value:, location:, comments: []) ⇒ EndContent

Returns a new instance of EndContent.



309
310
311
312
313
# File 'lib/syntax_tree/node.rb', line 309

def initialize(value:, location:, comments: [])
  @value = value
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



307
308
309
# File 'lib/syntax_tree/node.rb', line 307

def comments
  @comments
end

#valueObject (readonly)

String

the content after the script



304
305
306
# File 'lib/syntax_tree/node.rb', line 304

def value
  @value
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



315
316
317
# File 'lib/syntax_tree/node.rb', line 315

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



321
322
323
# File 'lib/syntax_tree/node.rb', line 321

def deconstruct_keys(keys)
  { value: value, location: location, comments: comments }
end

#format(q) ⇒ Object



325
326
327
328
329
330
331
# File 'lib/syntax_tree/node.rb', line 325

def format(q)
  q.text("__END__")
  q.breakable(force: true)

  separator = -> { q.breakable(indent: false, force: true) }
  q.seplist(value.split(/\r?\n/, -1), separator) { |line| q.text(line) }
end

#pretty_print(q) ⇒ Object



333
334
335
336
337
338
339
340
341
342
# File 'lib/syntax_tree/node.rb', line 333

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("__end__")

    q.breakable
    q.pp(value)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



344
345
346
347
348
# File 'lib/syntax_tree/node.rb', line 344

def to_json(*opts)
  { type: :__end__, value: value, loc: location, cmts: comments }.to_json(
    *opts
  )
end