Class: YARD::CodeObjects::Chef::ChefObject

Inherits:
NamespaceObject
  • Object
show all
Defined in:
lib/yard-chefdoc/code_objects/chef.rb

Overview

The chef object will be the root of your namespace

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace, name) ⇒ ChefObject

Creates a new ChefObject object.

Parameters:

  • namespace (NamespaceObject)

    namespace to which the object belongs

  • name (String)

    name of the ChefObject



17
18
19
# File 'lib/yard-chefdoc/code_objects/chef.rb', line 17

def initialize(namespace, name)
  super(namespace, name)
end

Instance Attribute Details

#fileObject

The file the object appears in



7
8
9
# File 'lib/yard-chefdoc/code_objects/chef.rb', line 7

def file
  @file
end

#headerObject

The header found in the source file



8
9
10
# File 'lib/yard-chefdoc/code_objects/chef.rb', line 8

def header
  @header
end

Class Method Details

.register(name, type, file) ⇒ <type>Object

Factory for creating and registering chef element object in YARD::Registry.

belong

Parameters:

  • namespace (NamespaceObject)

    namespace to which the object must

  • name (String)

    name of the chef element

  • type (Symbol, String)

    type of the chef element

Returns:

  • (<type>Object)

    the element object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/yard-chefdoc/code_objects/chef.rb', line 40

def self.register(name, type, file)
  element = @@chef_elements[type]
  raise "Invalid chef element type #{type}" unless element
  element_obj = YARD::Registry.resolve(:root, "#{type}::#{name}")
  if element_obj.nil?
    element_obj = element.new(:root, "#{type}::#{name}")
    log.info "Created [#{type.to_s.capitalize}] #{element_obj.name} => #{element_obj.namespace}"
    element_obj.chef_init(file)
  end
  element_obj
end

.register_element(element) ⇒ Object

Register a chef element class.

Parameters:

  • element (Class)

    chef element class



25
26
27
28
# File 'lib/yard-chefdoc/code_objects/chef.rb', line 25

def self.register_element(element)
  @@chef_elements ||= {}
  @@chef_elements[element] = self
end

Instance Method Details

#chef_init(file) ⇒ ChefObject

Does chef specific initalization tasks

Parameters:

  • file (String)

    The file of the object thas is being initialized

Returns:



69
70
71
72
73
74
# File 'lib/yard-chefdoc/code_objects/chef.rb', line 69

def chef_init(file)
  self.file = file
  self.source = IO.read(File.expand_path(file))
  self.header = find_header_in(source)
  self.docstring = find_description_in(header)
end

#children_by_type(type) ⇒ Array<ChefObject>

Returns children of an object of a particular type.

Parameters:

  • type (Symbol)

    type of ChefObject to be selected

Returns:



58
59
60
61
# File 'lib/yard-chefdoc/code_objects/chef.rb', line 58

def children_by_type(type)
  children = YARD::Registry.all(type)
  children.select { |child| child.parent == self }
end

#find_description_in(header) ⇒ String

Gets the description from the file header. Currently the docstring of recipes, attributes etc. is looked up in the file header and starts with a single line containing the keyword “Description”.

Returns:

  • (String)

    The description



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/yard-chefdoc/code_objects/chef.rb', line 101

def find_description_in(header)
  desc_found = false
  docstring = []
  header.each_line do |line|
    if desc_found
      docstring.push line
    elsif line.chomp == 'Description'
      desc_found = true
    end
  end

  docstring.join("\n")
end

#find_header_in(src) ⇒ String

Gets the file header if available The header is defined by a every comment line starting at the beginning of the file Until the first blank line. If no blank line is found then the comment is considered the following resource’s/blocks docstring.

Returns:

  • (String)

    The header of the file. Empty if no header is found



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/yard-chefdoc/code_objects/chef.rb', line 83

def find_header_in(src)
  h = []
  src.each_line do |line|
    comment = line[/^\s*#\s?(.*)$/, 1]
    break if comment.nil?

    h.push comment
  end

  h.join("\n")
end