class KnifeCookbookDoc::ResourceModel

Attributes

native_resource[R]

Public Class Methods

new(cookbook_name, file) click to toggle source
# File lib/knife_cookbook_doc/resource_model.rb, line 7
def initialize(cookbook_name, file)
  @native_resource = build_native_from_file(cookbook_name, file)
  load_descriptions
end

Public Instance Methods

action_description(action) click to toggle source
# File lib/knife_cookbook_doc/resource_model.rb, line 37
def action_description(action)
  action_descriptions[action.to_s]
end
action_descriptions() click to toggle source
# File lib/knife_cookbook_doc/resource_model.rb, line 41
def action_descriptions
  @action_descriptions ||= {}
end
actions() click to toggle source

Return the unique set of actions, with the default one first, if there is a single default The :nothing action will show up only if it is the only one, or it is explicitly documented

# File lib/knife_cookbook_doc/resource_model.rb, line 18
def actions
  return @actions unless @actions.nil?

  if default_action.is_a?(Array)
    @actions = @native_resource.actions
  else
    @actions = [default_action].compact + @native_resource.actions.sort.uniq.select { |a| a != default_action }
  end

  @actions.delete(:nothing) if @actions != [:nothing] && action_descriptions[:nothing].nil?
  @actions
end
attribute_default_value(attribute) click to toggle source
# File lib/knife_cookbook_doc/resource_model.rb, line 58
def attribute_default_value(attribute)
  default = if attribute_has_default_value?(attribute)
              @native_resource.attribute_specifications[attribute][:default]
            end

  if default.is_a?(Chef::DelayedEvaluator)
    'lazy { ... }'
  else
    default.inspect
  end
end
attribute_description(attribute) click to toggle source
# File lib/knife_cookbook_doc/resource_model.rb, line 49
def attribute_description(attribute)
  attribute_descriptions[attribute.to_s]
end
attribute_descriptions() click to toggle source
# File lib/knife_cookbook_doc/resource_model.rb, line 70
def attribute_descriptions
  @attribute_descriptions ||= {}
end
attribute_has_default_value?(attribute) click to toggle source
# File lib/knife_cookbook_doc/resource_model.rb, line 53
def attribute_has_default_value?(attribute)
  specification = @native_resource.attribute_specifications[attribute]
  specification && specification.key?(:default)
end
attributes() click to toggle source
# File lib/knife_cookbook_doc/resource_model.rb, line 45
def attributes
  @native_resource.attribute_specifications.keys
end
default_action() click to toggle source
# File lib/knife_cookbook_doc/resource_model.rb, line 31
def default_action
  action = @native_resource.default_action
  return action.first if action.is_a?(Array) && action.length == 1
  action
end
name() click to toggle source
# File lib/knife_cookbook_doc/resource_model.rb, line 12
def name
  @native_resource.resource_name
end

Private Instance Methods

build_native_from_file(cookbook_name, filename) click to toggle source
# File lib/knife_cookbook_doc/resource_model.rb, line 95
def build_native_from_file(cookbook_name, filename)
  resource_class = Class.new(DocumentingLWRPBase)

  resource_class.resource_name = filename_to_qualified_string(cookbook_name, filename)
  resource_class.run_context = nil
  resource_data = IO.read(filename)
  resource_data = resource_data.gsub(/^=begin *\n *\# ?\<\n(.*?)^ *\# ?\>\n=end *\n/m) do
    "desc <<DOCO\n#{$1}\nDOCO\n"
  end
  resource_data = resource_data.gsub(/^ *\# ?\<\n(.*?)^ *\# ?\>\n/m) do
    "desc <<DOCO\n#{$1.gsub(/^ *\# ?/, '')}\nDOCO\n"
  end
  resource_data = resource_data.gsub(/^ *\# ?\<\> (.*?)$/) do
    "desc #{$1.inspect}\n"
  end

  resource_class.class_eval(resource_data, filename, 1)

  resource_class
end
load_descriptions() click to toggle source
# File lib/knife_cookbook_doc/resource_model.rb, line 76
def load_descriptions
  current_section = 'main'
  @native_resource.description.each_line do |line|
    if /^ *\@action *([^ ]*) (.*)$/ =~ line
      action_descriptions[$1] = $2.strip
    elsif /^ *(?:\@attribute|\@property) *([^ ]*) (.*)$/ =~ line
      attribute_descriptions[$1] = $2.strip
    elsif /^ *\@section (.*)$/ =~ line
      current_section = $1.strip
    else
      lines = (top_level_descriptions[current_section] || [])
      lines << line.gsub("\n",'')
      top_level_descriptions[current_section] = lines
    end
  end
end