Class: YARD::Handlers::Chef::AttributeHandler

Inherits:
Base
  • Object
show all
Defined in:
lib/yard-chef/handlers/attribute.rb

Overview

Handles “attributes” in cookbook metadata and lightweight resource.

Constant Summary collapse

MATCH =
/^\s*(default|force_default|normal|override|force_override)(\[.+?\])\s*=\s*(.+)/m

Instance Method Summary collapse

Methods inherited from Base

#cookbook, #lwrp, #name

Instance Method Details

#docstringYARD::Docstring

Get the docstring related to the attributes. The docstring is obtained from the “:description” field in the attribute.

Returns:

  • (YARD::Docstring)

    docstring for the attribute



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/yard-chef/handlers/attribute.rb', line 67

def docstring
  description = ''
  path_array = parser.file.to_s.split('/')
  # Parse docstring
  if path_array.include?('metadata.rb')
    # Suppose :description string have concatenation operator '+' then
    # YARD builds an abstract syntax tree (AST). We need to traverse the
    # tree to get the whole description string
    statement.parameters[1].children.each do |ast_node|
      next unless ast_node.jump(:ident).source == 'description'
      ast_node.traverse do |child|
        description << child.jump(:string_content).source if child.type == :string_content
      end
    end
  else
    description = statement.comments
  end
  attrib_obj.docstring = YARD::DocstringParser.new.parse(description).to_docstring
  is_kind_of = ''
  is_default = ''
  if path_array.include? 'attributes'
    statement.source =~ MATCH
    is_default = Regexp.last_match(3)
  else
    statement.parameters.each do |n|
      next unless (n.is_a? YARD::Parser::Ruby::AstNode) && (n.source =~ /(default|kind_of)/)
      n.each do |node|
        if node.source =~ /default/
          m = node.source.match(/\W+?\s(.*)/)
          is_default = m[1] if m
        end
        if node.source =~ /kind_of/
          m = node.source.match(/\W+?\s(.*)/)
          is_kind_of = m[1] if m
        end
      end
    end
  end
  attrib_obj.kind_of = is_kind_of
  attrib_obj.default = is_default.split("\n").map { |s| "    #{s}" }.join("\n")
end

#processObject

Process “attribute” keyword.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/yard-chef/handlers/attribute.rb', line 35

def process
  path_array = parser.file.to_s.split('/')
  # If file path includes metadata then handle cookbook attributes
  # else handle resource attributes
  if path_array.include?('metadata.rb') || path_array.include?('attributes')
    namespace = cookbook
  else
    namespace = lwrp
    namespace.add_file(statement.file)

    cookbook_obj = cookbook
    unless cookbook_obj.resources.include?(namespace)
      cookbook_obj.resources.push(namespace)
    end
  end

  # Register attribute if not already registered
  if path_array.include? 'attributes'
    statement.source =~ MATCH
    attrib_obj = ChefObject.register(namespace, "#{Regexp.last_match(1)}#{Regexp.last_match(2)}", :attribute)
  else
    attrib_obj = ChefObject.register(namespace, name, :attribute)
  end
  attrib_obj.source = statement.source
  attrib_obj.add_file(statement.file, statement.line)
end