class PuppetX::PuppetLabs::Strings::YARD::Handlers::PuppetTypeHandler

Handles `dispatch` calls within a future parser function declaration. For now, it just treats any docstring as an `@overlaod` tag and attaches the overload to the parent function.

Public Instance Methods

fetch_default(node) click to toggle source
# File lib/puppet_x/puppetlabs/strings/yard/handlers/type_handler.rb, line 268
def fetch_default node
  do_block = node.jump(:do_block)
  do_block.traverse do |s|
    if is_a_func_call_named? 'defaultto', s
      return s[-1].source
    end
  end
  nil
end
fetch_description(fcall) click to toggle source
# File lib/puppet_x/puppetlabs/strings/yard/handlers/type_handler.rb, line 278
def fetch_description(fcall)
  fcall.traverse do |node|
    if is_a_func_call_named? 'desc', node
      content = node.jump(:string_content)
      if content != node
        @heredoc_helper = HereDocHelper.new
        if @heredoc_helper.is_heredoc? content.source
          docstring = @heredoc_helper.process_heredoc content.source
        else
          docstring = content.source
        end
        return docstring
      end
    end
  end
  return nil
end
get_feature(node) click to toggle source
# File lib/puppet_x/puppetlabs/strings/yard/handlers/type_handler.rb, line 213
def get_feature node
  name = node[1].jump(:ident).source
  desc = node[1].jump(:tstring_content).source
  methods = []
  if node[1].length == 4 and node.children[1][2].jump(:ident).source == 'methods'
    arr = node[1][2].jump(:array)
    if arr != node[1][2]
      arr.traverse do |s|
        if s.type == :ident
          methods << s.source
        end
      end
    end
  end
  {
    :name => name,
    :desc => desc,
    :methods => methods != [] ? methods : nil,
  }
end
get_parameter_allowed_values(node) click to toggle source
# File lib/puppet_x/puppetlabs/strings/yard/handlers/type_handler.rb, line 234
def get_parameter_allowed_values node
  vals = []
  node.traverse do |s|
    if is_a_func_call_named? 'newvalues', s
      list = s.jump(:list)
      if list != s
        vals += list.map { |item| [item.source] if YARD::Parser::Ruby::AstNode === item }
      end
    end
  end
  vals.compact
end
get_property_allowed_values(node) click to toggle source

Calls to newvalue only apply to properties, according to Dan & Nan's “Puppet Types and Providers”, page 30.

# File lib/puppet_x/puppetlabs/strings/yard/handlers/type_handler.rb, line 249
def get_property_allowed_values node
  vals = get_parameter_allowed_values node
  node.traverse do |s|
    if is_a_func_call_named? 'newvalue', s
      required_features = nil
      s.traverse do |ss|
        if ss.type == :assoc and ss[0].source == ':required_features'
          required_features = ss[1].source
        end
      end
      list = s.jump(:list)
      if list != s
        vals << [list[0].source, required_features].compact
      end
    end
  end
  vals
end
is_a_func_call_named?(name, node) click to toggle source
# File lib/puppet_x/puppetlabs/strings/yard/handlers/type_handler.rb, line 209
def is_a_func_call_named? name, node
  (node.type == :fcall or node.type == :command or node.type == :vcall) and node.children.first.source == name
end
is_feature?(node) click to toggle source
# File lib/puppet_x/puppetlabs/strings/yard/handlers/type_handler.rb, line 205
def is_feature? node
  is_a_func_call_named? 'feature', node
end
is_namevar?(node, param_name, type_name) click to toggle source

See: docs.puppetlabs.com/guides/custom_types.html#namevar node should be a parameter

# File lib/puppet_x/puppetlabs/strings/yard/handlers/type_handler.rb, line 162
def is_namevar? node, param_name, type_name
  # Option 1:
  # Puppet::Type.newtype(:name) do
  # ...
  # newparam(:name) do
  #   ...
  # end
  if type_name == param_name
    return true
  end
  # Option 2:
  # newparam(:path, :namevar => true) do
  #   ...
  # end
  if node.children.length >= 2
    node.traverse do |s|
      if s.type == :assoc and s.jump(:ident).source == 'namevar' and s.jump(:kw).source == 'true'
        return true
      end
    end
  end
  # Option 3:
  # newparam(:path) do
  #   isnamevar
  #   ...
  # end
  do_block = node.jump(:do_block).traverse do |s|
    if is_a_func_call_named? 'isnamevar', s
      return true
    end
  end
  # Crazy implementations of types may just call #isnamevar directly on the object.
  # We don't handle this today.
  return false
end
is_param?(node) click to toggle source
# File lib/puppet_x/puppetlabs/strings/yard/handlers/type_handler.rb, line 198
def is_param? node
  is_a_func_call_named? 'newparam', node
end
is_prop?(node) click to toggle source
# File lib/puppet_x/puppetlabs/strings/yard/handlers/type_handler.rb, line 201
def is_prop? node
  is_a_func_call_named? 'newproperty', node
end