Sha256: 1d08db06c297762e51197aec0a4c27d699432ca57d2c24dfa9d6016edb1ee9b2

Contents?: true

Size: 1.89 KB

Versions: 48

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true
# Handles any constant assignment
class YARD::Handlers::Ruby::ConstantHandler < YARD::Handlers::Ruby::Base
  include YARD::Handlers::Ruby::StructHandlerMethods
  handles :assign
  namespace_only

  process do
    if statement[1].call? && statement[1][0][0] == s(:const, "Struct") &&
       statement[1][2] == s(:ident, "new")
      process_structclass(statement)
    elsif statement[0].type == :var_field && statement[0][0].type == :const
      process_constant(statement)
    elsif statement[0].type == :const_path_field
      process_constant(statement)
    end
  end

  private

  def process_constant(statement)
    name = statement[0].source
    value = statement[1].source
    obj = P(namespace, name)
    if obj.is_a?(NamespaceObject) && obj.namespace == namespace
      raise YARD::Parser::UndocumentableError, "constant for existing #{obj.type} #{obj}"
    else
      ensure_loaded! obj.parent
      register ConstantObject.new(namespace, name) {|o| o.source = statement; o.value = value.strip }
    end
  end

  def process_structclass(statement)
    lhs = statement[0][0]
    if lhs.type == :const
      klass = create_class(lhs[0], P(:Struct))
      create_attributes(klass, extract_parameters(statement[1]))
      parse_block(statement[1].block[1], :namespace => klass) unless statement[1].block.nil?
    else
      raise YARD::Parser::UndocumentableError, "Struct assignment to #{statement[0].source}"
    end
  end

  # Extract the parameters from the Struct.new AST node, returning them as a list
  # of strings
  #
  # @param [MethodCallNode] superclass the AST node for the Struct.new call
  # @return [Array<String>] the member names to generate methods for
  def extract_parameters(superclass)
    return [] unless superclass.parameters
    members = superclass.parameters.select {|x| x && x.type == :symbol_literal }
    members.map! {|x| x.source.strip[1..-1] }
    members
  end
end

Version data entries

48 entries across 48 versions & 6 rubygems

Version Path
direct7-0.0.18 vendor/bundle/ruby/2.7.0/gems/yard-0.9.34/lib/yard/handlers/ruby/constant_handler.rb
direct7-0.0.17 vendor/bundle/ruby/2.7.0/gems/yard-0.9.34/lib/yard/handlers/ruby/constant_handler.rb
direct7-0.0.16 vendor/bundle/ruby/2.7.0/gems/yard-0.9.34/lib/yard/handlers/ruby/constant_handler.rb
yard-0.9.37 lib/yard/handlers/ruby/constant_handler.rb
kinetic_sdk-5.0.27 gems/yard-0.9.36/lib/yard/handlers/ruby/constant_handler.rb
kinetic_sdk-5.0.26 gems/yard-0.9.36/lib/yard/handlers/ruby/constant_handler.rb
kinetic_sdk-5.0.25 gems/yard-0.9.25/lib/yard/handlers/ruby/constant_handler.rb
kinetic_sdk-5.0.24 gems/yard-0.9.25/lib/yard/handlers/ruby/constant_handler.rb
direct7-0.0.13 vendor/bundle/ruby/2.7.0/gems/yard-0.9.34/lib/yard/handlers/ruby/constant_handler.rb
yard-0.9.36 lib/yard/handlers/ruby/constant_handler.rb
yard-0.9.35 lib/yard/handlers/ruby/constant_handler.rb
direct7-0.0.12 vendor/bundle/ruby/2.7.0/gems/yard-0.9.34/lib/yard/handlers/ruby/constant_handler.rb
kinetic_sdk-5.0.23 gems/yard-0.9.25/lib/yard/handlers/ruby/constant_handler.rb
getargv-0.3.3-universal-darwin vendor/bundle/ruby/3.3.0/gems/yard-0.9.34/lib/yard/handlers/ruby/constant_handler.rb
direct7-0.0.11 vendor/bundle/ruby/2.7.0/gems/yard-0.9.34/lib/yard/handlers/ruby/constant_handler.rb
kinetic_sdk-5.0.22 gems/yard-0.9.25/lib/yard/handlers/ruby/constant_handler.rb
kinetic_sdk-5.0.21 gems/yard-0.9.25/lib/yard/handlers/ruby/constant_handler.rb
yard-0.9.34 lib/yard/handlers/ruby/constant_handler.rb
yard-0.9.33 lib/yard/handlers/ruby/constant_handler.rb
yard-0.9.32 lib/yard/handlers/ruby/constant_handler.rb