Class: Yoda::Parsing::NodeObjects::ArgsNode

Inherits:
Object
  • Object
show all
Defined in:
lib/yoda/parsing/node_objects/args_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ ArgsNode

Returns a new instance of ArgsNode.

Parameters:

  • node (::AST::Node)


10
11
12
13
# File 'lib/yoda/parsing/node_objects/args_node.rb', line 10

def initialize(node)
  fail ArgumentError, node unless node.is_a?(::AST::Node) && node.type == :args
  @node = node
end

Instance Attribute Details

#nodeObject (readonly)

Parameters:

  • node (::AST::Node)


7
8
9
# File 'lib/yoda/parsing/node_objects/args_node.rb', line 7

def node
  @node
end

Instance Method Details

#block_parameter_node::AST::Node?

Returns:

  • (::AST::Node, nil)


53
54
55
# File 'lib/yoda/parsing/node_objects/args_node.rb', line 53

def block_parameter_node
  @block_parameter_node ||= node.children.find { |arg_node| arg_node.type == :blockarg }
end

#keyword_parameter_nodesArray<::AST::Node>

Returns:

  • (Array<::AST::Node>)


43
44
45
# File 'lib/yoda/parsing/node_objects/args_node.rb', line 43

def keyword_parameter_nodes
  @keyword_parameter_nodes ||= node.children.select { |arg_node| %i(kwarg kwoptarg).include?(arg_node.type) }
end

#keyword_rest_parameter_node::AST::Node?

Returns:

  • (::AST::Node, nil)


48
49
50
# File 'lib/yoda/parsing/node_objects/args_node.rb', line 48

def keyword_rest_parameter_node
  @keyword_rest_parameter_node ||= node.children.find { |arg_node| arg_node.type == :kwrestarg }
end

#parameterModel::Parameters::Multiple



16
17
18
19
20
21
22
23
24
25
# File 'lib/yoda/parsing/node_objects/args_node.rb', line 16

def parameter
  Model::Parameters::Multiple.new(
    parameters: parameter_nodes.map(&method(:parse_arg_node)),
    rest_parameter: rest_parameter_node && parse_single_arg_node(rest_parameter_node),
    post_parameters: post_parameter_nodes.map(&method(:parse_arg_node)),
    keyword_parameters: keyword_parameter_nodes.map { |arg_node| Model::Parameters::Named.new(arg_node.children.first) },
    keyword_rest_parameter: keyword_rest_parameter_node && parse_single_arg_node(keyword_rest_parameter_node),
    block_parameter: block_parameter_node && parse_single_arg_node(block_parameter_node),
  )
end

#parameter_nodesArray<::AST::Node>

Returns:

  • (Array<::AST::Node>)


28
29
30
# File 'lib/yoda/parsing/node_objects/args_node.rb', line 28

def parameter_nodes
  @parameter_nodes ||= node.children.take_while { |arg_node| %i(arg optarg mlhs).include?(arg_node.type) }
end

#post_parameter_nodesArray<::AST::Node>

Returns:

  • (Array<::AST::Node>)


38
39
40
# File 'lib/yoda/parsing/node_objects/args_node.rb', line 38

def post_parameter_nodes
  @post_parameter_nodes ||= node.children.drop_while { |arg_node| %i(arg optarg mlhs).include?(arg_node.type) }.select { |arg_node| %i(arg optarg mlhs).include?(arg_node.type) }
end

#rest_parameter_node::AST::Node?

Returns:

  • (::AST::Node, nil)


33
34
35
# File 'lib/yoda/parsing/node_objects/args_node.rb', line 33

def rest_parameter_node
  @rest_parameter_node ||= node.children.find { |arg_node| arg_node.type == :restarg }
end