Class: Solargraph::Parser::ParserGem::NodeProcessors::StructDefinition::StructDefintionNode

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/parser/parser_gem/node_processors/struct_definition/struct_definition_node.rb

Overview

A node wrapper for a Struct definition via inheritance.

Examples:

class MyStruct < Struct.new(:bar, :baz)
  def foo
  end
end

Direct Known Subclasses

StructAssignmentNode

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ StructDefintionNode

Returns a new instance of StructDefintionNode.

Parameters:

  • node (Parser::AST::Node)


54
55
56
# File 'lib/solargraph/parser/parser_gem/node_processors/struct_definition/struct_definition_node.rb', line 54

def initialize node
  @node = node
end

Class Method Details

.match?(node) ⇒ Boolean

Examples:

s(:class,
  s(:const, nil, :Foo),
  s(:send,
    s(:const, nil, :Struct), :new,
    s(:sym, :bar),
    s(:sym, :baz)),
    s(:hash,
      s(:pair,
        s(:sym, :keyword_init),
        s(:true)))),
  s(:def, :foo,
    s(:args),
    s(:send, nil, :bar)))

Parameters:

  • node (Parser::AST::Node)

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/solargraph/parser/parser_gem/node_processors/struct_definition/struct_definition_node.rb', line 32

def match? node
  return false unless node&.type == :class

  struct_definition_node?(node.children[1])
end

Instance Method Details

#attributes ⇒ Array<Array(Parser::AST::Node, String)>

Returns:

  • (Array<Array(Parser::AST::Node, String)>)


64
65
66
67
68
69
# File 'lib/solargraph/parser/parser_gem/node_processors/struct_definition/struct_definition_node.rb', line 64

def attributes
  struct_attribute_nodes.map do |struct_def_param|
    next unless struct_def_param.type == :sym
    [struct_def_param, struct_def_param.children[0].to_s]
  end.compact
end

#body_node ⇒ Parser::AST::Node

Returns:

  • (Parser::AST::Node)


84
85
86
# File 'lib/solargraph/parser/parser_gem/node_processors/struct_definition/struct_definition_node.rb', line 84

def body_node
  node.children[2]
end

#class_name ⇒ String

Returns:

  • (String)


59
60
61
# File 'lib/solargraph/parser/parser_gem/node_processors/struct_definition/struct_definition_node.rb', line 59

def class_name
  Parser::NodeMethods.unpack_name(node)
end

#keyword_init? ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
79
80
81
# File 'lib/solargraph/parser/parser_gem/node_processors/struct_definition/struct_definition_node.rb', line 71

def keyword_init?
  keyword_init_param = struct_attribute_nodes.find do |struct_def_param|
    struct_def_param.type == :hash && struct_def_param.children[0].type == :pair &&
      struct_def_param.children[0].children[0].children[0] == :keyword_init
  end

  return false if keyword_init_param.nil?

  # rubocop:disable-next Lint/BooleanSymbol
  keyword_init_param.children[0].children[1].type == :true
end