Sha256: ea4f5c52833eb2b7f962d50fe1af780f2453576a5edcadbdb9d39a994a533434

Contents?: true

Size: 1.46 KB

Versions: 99

Compression:

Stored size: 1.46 KB

Contents

# 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)
    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 = create_class(lhs[0], P(:Struct))
      create_attributes(klass, extract_parameters(statement[1]))
    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

99 entries across 80 versions & 9 rubygems

Version Path
yard-0.9.0 lib/yard/handlers/ruby/constant_handler.rb
yard-0.8.7.6 lib/yard/handlers/ruby/constant_handler.rb
yard-0.8.7.5 lib/yard/handlers/ruby/constant_handler.rb
climine-0.0.7 vendor/bundle/ruby/2.1.0/gems/yard-0.8.7.3/lib/yard/handlers/ruby/constant_handler.rb
climine-0.0.7 vendor/bundle/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/handlers/ruby/constant_handler.rb
climine-0.0.6 vendor/bundle/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/handlers/ruby/constant_handler.rb
climine-0.0.5 vendor/bundle/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/handlers/ruby/constant_handler.rb
yard-0.8.7.4 lib/yard/handlers/ruby/constant_handler.rb
climine-0.0.4 vendor/bundle/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/handlers/ruby/constant_handler.rb
climine-0.0.3 vendor/bundle/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/handlers/ruby/constant_handler.rb
mango-0.8.0 vendor/bundler/ruby/2.1.0/gems/yard-0.8.7.3/lib/yard/handlers/ruby/constant_handler.rb
mango-0.7.1 vendor/bundler/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/handlers/ruby/constant_handler.rb
mango-0.7.0 vendor/bundler/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/handlers/ruby/constant_handler.rb
climine-0.0.2 vendor/bundle/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/handlers/ruby/constant_handler.rb
climine-0.0.1 vendor/bundle/ruby/2.0.0/gems/yard-0.8.7.3/lib/yard/handlers/ruby/constant_handler.rb
yard-0.8.7.3 lib/yard/handlers/ruby/constant_handler.rb
yard-0.8.7.2 lib/yard/handlers/ruby/constant_handler.rb
candlepin-api-0.4.0 bundle/ruby/1.9.1/gems/yard-0.8.7/lib/yard/handlers/ruby/constant_handler.rb
candlepin-api-0.4.0 bundle/ruby/1.8/gems/yard-0.8.7/lib/yard/handlers/ruby/constant_handler.rb
candlepin-api-0.4.0 bundle/ruby/gems/yard-0.8.7/lib/yard/handlers/ruby/constant_handler.rb