Class: YARD::CodeObjects::Chef::ChefObject
- Inherits:
-
NamespaceObject
- Object
- NamespaceObject
- YARD::CodeObjects::Chef::ChefObject
- Defined in:
- lib/yard-chefdoc/code_objects/chef.rb
Overview
The chef object will be the root of your namespace
Direct Known Subclasses
AttributeObject, CookbookObject, RecipeObject, ResourceObject
Instance Attribute Summary collapse
-
#file ⇒ Object
The file the object appears in.
-
#header ⇒ Object
The header found in the source file.
Class Method Summary collapse
-
.register(name, type, file) ⇒ <type>Object
Factory for creating and registering chef element object in YARD::Registry.
-
.register_element(element) ⇒ Object
Register a chef element class.
Instance Method Summary collapse
-
#chef_init(file) ⇒ ChefObject
Does chef specific initalization tasks.
-
#children_by_type(type) ⇒ Array<ChefObject>
Returns children of an object of a particular type.
-
#find_description_in(header) ⇒ String
Gets the description from the file header.
-
#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.
-
#initialize(namespace, name) ⇒ ChefObject
constructor
Creates a new ChefObject object.
Constructor Details
#initialize(namespace, name) ⇒ ChefObject
Creates a new ChefObject object.
17 18 19 |
# File 'lib/yard-chefdoc/code_objects/chef.rb', line 17 def initialize(namespace, name) super(namespace, name) end |
Instance Attribute Details
#file ⇒ Object
The file the object appears in
7 8 9 |
# File 'lib/yard-chefdoc/code_objects/chef.rb', line 7 def file @file end |
#header ⇒ Object
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
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.
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
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.(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.
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”.
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.
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 |