From 005a8521bd1467c5c0699bec4ee2e686cc020929 Mon Sep 17 00:00:00 2001 From: Charlie Sharpsteen Date: Wed, 28 May 2014 23:53:12 -0700 Subject: [PATCH] Extract class parameters from parser output Re-worked the YARD transformer and handlers to attach parameters to the resulting code objects. Some architectural debt was incurred in the interests of producing a working prototype quickly. --- lib/puppetx/yardoc/pops/yard_statement.rb | 10 +++++++++- lib/puppetx/yardoc/pops/yard_transformer.rb | 19 ++++++++++++++----- .../yard/code_objects/host_class_object.rb | 4 ++++ .../yard/handlers/host_class_handler.rb | 7 ++++++- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/lib/puppetx/yardoc/pops/yard_statement.rb b/lib/puppetx/yardoc/pops/yard_statement.rb index 0df1773..9c6b030 100644 --- a/lib/puppetx/yardoc/pops/yard_statement.rb +++ b/lib/puppetx/yardoc/pops/yard_statement.rb @@ -1,3 +1,4 @@ +require 'ostruct' require 'puppet/pops' require_relative '../../yardoc' @@ -5,10 +6,17 @@ require_relative '../../yardoc' module Puppetx::Yardoc::Pops # An adapter class that conforms a Pops model instance + adapters to the # interface expected by YARD handlers. - class YARDStatement + # + # FIXME: Inhertiting from OpenStruct is a bit of a hack. It allows attributes + # to be declared as needed but in the long run understandibility of the code + # would be improved by having a concrete model. + class YARDStatement < OpenStruct attr_reader :pops_obj, :comments def initialize(pops_obj) + # Initialize OpenStruct + super({}) + unless pops_obj.is_a? Puppet::Pops::Model::PopsObject raise ArgumentError, "A YARDStatement can only be initialized from a PopsObject. Got a: #{pops_obj.class}" end diff --git a/lib/puppetx/yardoc/pops/yard_transformer.rb b/lib/puppetx/yardoc/pops/yard_transformer.rb index ebf4446..a5f596e 100644 --- a/lib/puppetx/yardoc/pops/yard_transformer.rb +++ b/lib/puppetx/yardoc/pops/yard_transformer.rb @@ -29,12 +29,21 @@ module Puppetx::Yardoc::Pops o.definitions.map{|d| transform(d)} end - # Extract comments from "Definition" objects. That is: nodes definitions, - # type definitions and class definitions. Wrap them into YARDStatement - # objects that provide an interface for YARD handlers. - def transform_Definition(o) - YARDStatement.new(o) + # Extract comments from type definitions and class definitions. Wrap them + # into YARDStatement objects that provide an interface for YARD handlers. + def transform_NamedDefinition(o) + obj = YARDStatement.new(o) + obj.parameters = o.parameters.map do |p| + param_tuple = [transform(p)] + param_tuple << ( p.value.nil? ? nil : transform(p.value) ) + end + + obj end + # Catch-all visitor. + def transform_Positioned(o) + YARDStatement.new(o) + end end end diff --git a/lib/puppetx/yardoc/yard/code_objects/host_class_object.rb b/lib/puppetx/yardoc/yard/code_objects/host_class_object.rb index ea7c102..bb85b67 100644 --- a/lib/puppetx/yardoc/yard/code_objects/host_class_object.rb +++ b/lib/puppetx/yardoc/yard/code_objects/host_class_object.rb @@ -9,6 +9,10 @@ module Puppetx::Yardoc::YARD::CodeObjects # @return [HostClassObject, Proxy, nil] attr_accessor :parent_class + # A list of parameters attached to this class. + # @return [Array] + attr_accessor :parameters + # The `YARD::Codeobjects::Base` class pulls a bunch of shenanigans to # insert proxy namespaces. Unfortunately, said shenanigans pick up on the # `::` in Puppet names and start to mangle things based on rules for the diff --git a/lib/puppetx/yardoc/yard/handlers/host_class_handler.rb b/lib/puppetx/yardoc/yard/handlers/host_class_handler.rb index 11be5d5..12c1b2c 100644 --- a/lib/puppetx/yardoc/yard/handlers/host_class_handler.rb +++ b/lib/puppetx/yardoc/yard/handlers/host_class_handler.rb @@ -5,7 +5,12 @@ module Puppetx::Yardoc::YARD::Handlers handles HostClassDefinition process do - obj = HostClassObject.new(:root, statement.pops_obj.name) + obj = HostClassObject.new(:root, statement.pops_obj.name) do |o| + o.parameters = statement.parameters.map do |a| + param_tuple = [a[0].pops_obj.name] + param_tuple << ( a[1].nil? ? nil : a[1].source ) + end + end statement.pops_obj.tap do |o| if o.parent_class