class YARD::CodeObjects::Chef::ChefObject
The chef object will be the root of your namespace
Attributes
Public Class Methods
Creates a new ChefObject
object.
@param namespace [NamespaceObject] namespace to which the object belongs @param name [String] name of the ChefObject
@return [ChefObject] the newly created ChefObject
# File lib/yard-chefdoc/code_objects/chef.rb, line 17 def initialize(namespace, name) super(namespace, name) end
Factory for creating and registering chef element object in YARD::Registry.
@param namespace [NamespaceObject] namespace to which the object must belong @param name [String] name of the chef element @param type [Symbol, String] type of the chef element
@return [<type>Object] the element object
# 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 a chef element class.
@param element [Class] chef element class
# File lib/yard-chefdoc/code_objects/chef.rb, line 25 def self.register_element(element) @@chef_elements ||= {} @@chef_elements[element] = self end
Public Instance Methods
Does chef specific initalization tasks
@param file [String] The file of the object thas is being initialized
@return [ChefObject] The (chef) initialized object
# 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
Returns children of an object of a particular type.
@param type [Symbol] type of ChefObject
to be selected
@return [Array<ChefObject>] list of ChefObjects
# 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
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”.
@return [String] The description
# 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
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.
@return [String] The header of the file. Empty if no header is found
# 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