class PuppetStrings::Yard::Handlers::Ruby::RsapiHandler

Implements the handler for Puppet resource types written in Ruby.

Constants

DEFAULT_ENSURABLE_DOCSTRING

The default docstring when ensurable is used without given a docstring.

Private Instance Methods

array_from_node(node) click to toggle source
# File lib/puppet-strings/yard/handlers/ruby/rsapi_handler.rb, line 88
def array_from_node(node)
  return nil unless node

  node.children.map do |assoc|
    value_from_node(assoc.children[0])
  end
end
create_parameter(name, definition) click to toggle source
# File lib/puppet-strings/yard/handlers/ruby/rsapi_handler.rb, line 140
def create_parameter(name, definition)
  parameter = PuppetStrings::Yard::CodeObjects::Type::Parameter.new(name, definition['desc'])
  set_values(definition, parameter)
  parameter
end
create_property(name, definition) click to toggle source
# File lib/puppet-strings/yard/handlers/ruby/rsapi_handler.rb, line 146
def create_property(name, definition)
  property = PuppetStrings::Yard::CodeObjects::Type::Property.new(name, definition['desc'])
  set_values(definition, property)
  property
end
extract_schema() click to toggle source
# File lib/puppet-strings/yard/handlers/ruby/rsapi_handler.rb, line 62
def extract_schema
  raise_parse_error('Expected list of key/value pairs as argument') unless kv_arg_list?(statement.parameters)
  hash_from_node(statement.parameters.children.first)
end
hash_from_node(node) click to toggle source
# File lib/puppet-strings/yard/handlers/ruby/rsapi_handler.rb, line 96
def hash_from_node(node)
  return nil unless node

  # puts "hash from #{node.inspect}"

  kv_pairs = node.children.map do |assoc|
    [value_from_node(assoc.children[0]), value_from_node(assoc.children[1])]
  end
  kv_pairs.to_h
end
kv_arg_list?(params) click to toggle source

check that the params of the register_type call are key/value pairs.

# File lib/puppet-strings/yard/handlers/ruby/rsapi_handler.rb, line 54
def kv_arg_list?(params)
  params.type == :list &&
    params.children.count.positive? &&
    params.children.first.type == :list &&
    params.children.first.children.count.positive? &&
    statement.parameters.children.first.children.first.type == :assoc
end
populate_type_data(object, schema) click to toggle source
# File lib/puppet-strings/yard/handlers/ruby/rsapi_handler.rb, line 127
def populate_type_data(object, schema)
  return if schema['attributes'].nil?

  schema['attributes'].each do |name, definition|
    # puts "Processing #{name}: #{definition.inspect}"
    if %w[parameter namevar].include? definition['behaviour']
      object.add_parameter(create_parameter(name, definition))
    else
      object.add_property(create_property(name, definition))
    end
  end
end
raise_parse_error(msg, location = statement) click to toggle source
# File lib/puppet-strings/yard/handlers/ruby/rsapi_handler.rb, line 49
def raise_parse_error(msg, location = statement)
  raise YARD::Parser::UndocumentableError, "#{msg} at #{location.file}:#{location.line}."
end
set_values(definition, object) click to toggle source
# File lib/puppet-strings/yard/handlers/ruby/rsapi_handler.rb, line 152
def set_values(definition, object)
  object.data_type = definition['type'] if definition.key? 'type'
  object.default = definition['default'] if definition.key? 'default'
  object.isnamevar = definition.key?('behaviour') && definition['behaviour'] == 'namevar'
end
value_from_node(node) click to toggle source
# File lib/puppet-strings/yard/handlers/ruby/rsapi_handler.rb, line 67
def value_from_node(node)
  return nil unless node

  # puts "value from #{node.inspect}"

  case node.type
  when :int
    node.source.to_i
  when :hash
    hash_from_node(node)
  when :array
    array_from_node(node)
  when :var_ref
    var_ref_from_node(node)
  when :symbol, :symbol_literal, :label, :dyna_symbol, :string_literal, :regexp_literal
    node_as_string(node)
  else
    raise_parse_error("unexpected construct #{node.type}")
  end
end
var_ref_from_node(node) click to toggle source
# File lib/puppet-strings/yard/handlers/ruby/rsapi_handler.rb, line 107
def var_ref_from_node(node)
  return nil unless node

  # puts "var_ref from #{node.inspect}"

  if node.children.first.type == :kw
    case node.children.first.source
    when 'false'
      return false
    when 'true'
      return true
    when 'nil'
      return nil
    else
      raise_parse_error("unexpected keyword '#{node.children.first.source}'")
    end
  end
  raise_parse_error('unexpected variable')
end