Sha256: 3871918dac735d37ec5629c074fa7a653fbf2d06c2d90793a84c05eac59f0f32

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 KB

Contents

class YARD::Handlers::Ruby::ConstantHandler < YARD::Handlers::Ruby::Base
  namespace_only
  handles :assign
  
  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)
    end
  end
  
  private
  
  def process_constant(statement)
    name = statement[0][0][0]
    value = statement[1].source
    register ConstantObject.new(namespace, name) {|o| o.source = statement; o.value = value.strip }
  end
  
  def process_structclass(statement)
    lhs = statement[0][0]
    if lhs.type == :const
      klass = register ClassObject.new(namespace, lhs[0])
      klass.superclass = P(:Struct)
      parse_attributes(klass, statement[1].parameters)
    else
      raise YARD::Parser::UndocumentableError, "Struct assignment to #{statement[0].source}"
    end
  end
  
  def parse_attributes(klass, attributes)
    return unless attributes
    
    scope = :instance
    attributes.each do |node|
      next if !node.respond_to?(:type) || node.type != :symbol_literal
      name = node.jump(:ident).source
      klass.attributes[scope][name] = SymbolHash[:read => nil, :write => nil]
      {read: name, write: "#{name}="}.each do |type, meth|
        klass.attributes[scope][name][type] = register MethodObject.new(klass, meth, scope)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
yard-0.5.8 lib/yard/handlers/ruby/constant_handler.rb
yard-0.5.7 lib/yard/handlers/ruby/constant_handler.rb
yard-0.5.6 lib/yard/handlers/ruby/constant_handler.rb
yard-0.5.5 lib/yard/handlers/ruby/constant_handler.rb